- 积分
- 136401
- 注册时间
- 2014-12-27
- 最后登录
- 2026-1-23
- 在线时间
- 605 小时
- 威望
- 562
- 贡献
- 29
- 金币
- 52903
- 钢镚
- 1422
- 交易凭证
- 1
- 分享
- 0
- 精华
- 33
- 帖子
- 2094
- 主题
- 1742
TA的每日心情 | 擦汗 2018-4-10 15:18 |
|---|
签到天数: 447 天 [LV.9]以坛为家II
超级版主
    
- 威望
- 562
- 贡献
- 29
- 金币
- 52903
- 钢镚
- 1422
 
|
FLSAH 开发过程中经常遇到访问别人的WEB服务遇到安全沙箱问题,而又不能强制要求别人放安全策略文件,那么我们可以从后台作为中转,访问服务
——节选自《疯狂Java讲义》
URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接。程序可以通过URLConnection实例向该URL发送请求、读取URL引用的资源。
通常创建一个和 URL 的连接,并发送请求、读取此 URL 引用的资源需要如下几个步骤:
(1)通过调用URL对象openConnection()方法来创建URLConnection对象。
(2)设置URLConnection的参数和普通请求属性。
(3)如果只是发送GET方式请求,使用connect方法建立和远程资源之间的实际连接即可;如果需要发送POST方式的请求,需要获取URLConnection实例对应的输出流来发送请求参数。
(4)远程资源变为可用,程序可以访问远程资源的头字段、或通过输入流读取远程资源的数据。
- public class TestGetPost {
- /**
- * 向指定URL发送GET方法的请求
- * @param url 发送请求的URL
- * @param param 请求参数,请求参数应该是name1=value1&name2=value2的形式
- * @return URL所代表远程资源的响应
- */
- public static String sendGet(String url, String param) {
- String result = "";
- BufferedReader in = null;
- try {
- String urlName = url + "?" + param;
- URL realUrl = new URL(urlName);
- // 打开和URL之间的连接
- URLConnection conn = realUrl.openConnection();
- // 设置通用的请求属性
- conn.setRequestProperty("accept", "*/*");
- conn.setRequestProperty("connection", "Keep-Alive");
- conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
- // 建立实际的连接
- conn.connect();
- // 获取所有响应头字段
- Map<String, List<String>> map = conn.getHeaderFields();
- // 遍历所有的响应头字段
- for (String key : map.keySet()) {
- System.out.println(key + "--->" + map.get(key));
- }
- // 定义BufferedReader输入流来读取URL的响应
- in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String line;
- while ((line = in.readLine()) != null) {
- result += "\n" + line;
- }
- } catch (Exception e) {
- System.out.println("发送GET请求出现异常!" + e);
- e.printStackTrace();
- }
- // 使用finally块来关闭输入流
- finally {
- try {
- if (in != null) {
- in.close();
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- return result;
- }
- /**
- * 向指定URL发送POST方法的请求
- * @param url 发送请求的URL
- * @param param 请求参数,请求参数应该是name1=value1&name2=value2的形式
- * @return URL所代表远程资源的响应
- */
- public static String sendPost(String url, String param) {
- PrintWriter out = null;
- BufferedReader in = null;
- String result = "";
- try {
- URL realUrl = new URL(url);
- // 打开和URL之间的连接
- URLConnection conn = realUrl.openConnection();
- // 设置通用的请求属性
- conn.setRequestProperty("accept", "*/*");
- conn.setRequestProperty("connection", "Keep-Alive");
- conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
-
- conn.setDoOutput(true);// 发送POST请求必须设置如下两行
- conn.setDoInput(true);
-
- out = new PrintWriter(conn.getOutputStream());// 获取URLConnection对象对应的输出流s
- out.print(param);// 发送请求参数
- out.flush();// flush输出流的缓冲
- in = new BufferedReader(new InputStreamReader(conn.getInputStream()));// 定义BufferedReader输入流来读取URL的响应
- String line;
- while ((line = in.readLine()) != null) {
- result += "\n" + line;
- }
- } catch (Exception e) {
- System.out.println("发送POST请求出现异常!" + e);
- e.printStackTrace();
- }
- // 使用finally块来关闭输出流、输入流
- finally {
- try {
- if (out != null) {
- out.close();
- }
- if (in != null) {
- in.close();
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- return result;
- }
- // 提供主方法,测试发送GET请求和POST请求
- public static void main(String args[]) {
- // 发送GET请求
- // String s = TestGetPost.sendGet("http://localhost:8080/xtest/URLresponse.jsp", "msg=888");
- // System.out.println(s);
- // 发送POST请求
- String s1 = TestGetPost.sendPost("http://localhost:8080/xtest/URLresponse.jsp", "user=李刚&pass=abc");
- System.out.println(s1);
- }
- }
复制代码
例子:访问百度apistore 接口:
- package com.service;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.net.URLConnection;
- public class BaiduAPIService {
- private static String APIKEY = "e5844ee56d2dc758e93551b0053be824";
- private static String SERVICE_URL = "http://apis.baidu.com/apistore/";
-
-
- /**
- * 向指定URL发送GET方法的请求
- * @param url 发送请求的URL
- * @param param 请求参数,请求参数应该是name1=value1&name2=value2的形式
- * @return URL所代表远程资源的响应
- */
-
- public static String sendGet(String url) {
- String result = "";
- BufferedReader in = null;
- try {
- String urlName = SERVICE_URL + url;
- URL realUrl = new URL(urlName);
- // 打开和URL之间的连接
- URLConnection conn = realUrl.openConnection();
- // 设置通用的请求属性
- conn.setRequestProperty("apikey", APIKEY);
- // 建立实际的连接
- conn.connect();
-
- // 定义BufferedReader输入流来读取URL的响应
- in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String line;
- while ((line = in.readLine()) != null) {
- result += line;
- }
- } catch (Exception e) {
- System.out.println("发送GET请求出现异常!" + e);
- e.printStackTrace();
- }
- // 使用finally块来关闭输入流
- finally {
- try {
- if (in != null) {
- in.close();
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- return result;
- }
- }
复制代码
参考资料:http://well-lf.iteye.com/blog/982042
|
|