Chinaunix首页 | 论坛 | 博客
  • 博客访问: 18101
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-12 11:48
  • 认证徽章:
文章分类
文章存档

2016年(48)

我的朋友

分类: Java

2016-11-12 15:14:40

 URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接。程序可以通过URLConnection实例向该URL发送请求、读取URL引用的资源。

通常创建一个和 URL 的连接,并发送请求、读取此 URL 引用的资源需要如下几个步骤:
(1)通过调用URL对象openConnection()方法来创建URLConnection对象。
(2)设置URLConnection的参数和普通请求属性。
(3)如果只是发送GET方式请求,使用connect方法建立和远程资源之间的实际连接即可;如果需要发送POST方式的请求,需要获取URLConnection实例对应的输出流来发送请求参数。
(4)远程资源变为可用,程序可以访问远程资源的头字段、或通过输入流读取远程资源的数据。

 

点击(此处)折叠或打开

  1. package cn.edu.zzu.wemall.net;

  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7. import java.util.List;
  8. import java.util.Map;

  9. import cn.edu.zzu.wemall.config.MyConfig;

  10. public class HttpRequest {
  11.     /**
  12.      * 向指定URL发送GET方法的请求
  13.      *
  14.      * @param url
  15.      * 发送请求的URL
  16.      * @param param
  17.      * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  18.      * @return URL 所代表远程资源的响应结果
  19.      * @throws Exception
  20.      *
  21.      * modify by liudewei
  22.      */
  23.     @SuppressWarnings("unused")
  24.     public static String sendGetWithParameter(String url, String param) throws Exception {
  25.         String result = "";
  26.         BufferedReader in = null;
  27.         try {
  28.             String urlNameString = url + "?" + param;
  29.             URL realUrl = new URL(urlNameString);
  30.             // 打开和URL之间的连接
  31.             URLConnection connection = realUrl.openConnection();
  32.             // 设置通用的请求属性

  33.             connection.setRequestProperty("user-agent",
  34.                     MyConfig.ClientUserAgent);
  35.             connection.setConnectTimeout(5000);
  36.             // 建立实际的连接
  37.             connection.connect();
  38.             // 获取所有响应头字段
  39.             Map<String, List<String>> map = connection.getHeaderFields();
  40.             // 遍历所有的响应头字段
  41.             for (String key : map.keySet()) {
  42.                // System.out.println(key + "--->" + map.get(key)); //关闭响应头的输出
  43.             }
  44.             // 定义 BufferedReader输入流来读取URL的响应
  45.             in = new BufferedReader(new InputStreamReader(
  46.                     connection.getInputStream()));
  47.             String line;
  48.             while ((line = in.readLine()) != null) {
  49.                 result += line;
  50.             }
  51.         } catch (Exception e) {
  52.      throw e;
  53.         }
  54.         // 使用finally块来关闭输入流
  55.         finally {
  56.             try {
  57.                 if (in != null) {
  58.                     in.close();
  59.                 }
  60.             } catch (Exception e2) {
  61.                  throw e2;
  62.             }
  63.         }
  64.         return result;
  65.     }
  66.     
  67.     
  68.     
  69.     @SuppressWarnings("unused")
  70.     public static String sendGet(String url) throws Exception {
  71.         String result = "";
  72.         BufferedReader in = null;
  73.         try {
  74.             
  75.             URL realUrl = new URL(url);
  76.             // 打开和URL之间的连接
  77.             URLConnection connection = realUrl.openConnection();
  78.             // 设置通用的请求属性
  79.             connection.setRequestProperty("accept", "*/*");
  80.             connection.setRequestProperty("connection", "Keep-Alive");
  81.             connection.setRequestProperty("user-agent",
  82.                     MyConfig.ClientUserAgent);
  83.             connection.setConnectTimeout(5000);
  84.             // 建立实际的连接
  85.             connection.connect();
  86.             // 获取所有响应头字段
  87.             Map<String, List<String>> map = connection.getHeaderFields();
  88.             // 遍历所有的响应头字段
  89.             for (String key : map.keySet()) {
  90.               // System.out.println(key + "--->" + map.get(key));
  91.             }
  92.             // 定义 BufferedReader输入流来读取URL的响应
  93.             in = new BufferedReader(new InputStreamReader(
  94.                     connection.getInputStream()));
  95.             String line;
  96.             while ((line = in.readLine()) != null) {
  97.                 result += line;
  98.             }
  99.         } catch (Exception e) {
  100.             throw e;
  101.         }
  102.         // 使用finally块来关闭输入流
  103.         finally {
  104.             try {
  105.                 if (in != null) {
  106.                     in.close();
  107.                 }
  108.             } catch (Exception e2) {
  109.                  throw e2;
  110.             }
  111.         }
  112.         return result;
  113.     }

  114.     /**
  115.      * 向指定 URL 发送POST方法的请求
  116.      *
  117.      * @param url
  118.      * 发送请求的 URL
  119.      * @param param
  120.      * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  121.      * @return 所代表远程资源的响应结果
  122.      * @throws Exception
  123.      */
  124.     public static String sendPost(String url, String param) throws Exception {
  125.         PrintWriter out = null;
  126.         BufferedReader in = null;
  127.         String result = "";
  128.         try {
  129.             URL realUrl = new URL(url);
  130.             // 打开和URL之间的连接
  131.             URLConnection conn = realUrl.openConnection();
  132.             // 设置通用的请求属性
  133.             conn.setRequestProperty("accept", "*/*");
  134.             conn.setRequestProperty("connection", "Keep-Alive");
  135.             conn.setRequestProperty("user-agent",
  136.                     MyConfig.ClientUserAgent);
  137.             conn.setConnectTimeout(5000);
  138.             // 发送POST请求必须设置如下两行
  139.             conn.setDoOutput(true);
  140.             conn.setDoInput(true);
  141.             // 获取URLConnection对象对应的输出流
  142.             out = new PrintWriter(conn.getOutputStream());
  143.             // 发送请求参数
  144.             out.print(param);
  145.             // flush输出流的缓冲
  146.             out.flush();
  147.             // 定义BufferedReader输入流来读取URL的响应
  148.             in = new BufferedReader(
  149.                     new InputStreamReader(conn.getInputStream()));
  150.             String line;
  151.             while ((line = in.readLine()) != null) {
  152.                 result += line;
  153.             }
  154.         } catch (Exception e) {
  155.                 throw e;
  156.         }
  157.         //使用finally块来关闭输出流、输入流
  158.         finally{
  159.             try{
  160.                 if(out!=null){
  161.                     out.close();
  162.                 }
  163.                 if(in!=null){
  164.                     in.close();
  165.                 }
  166.             }
  167.             catch(Exception ex){
  168.                 throw ex;
  169.             }
  170.         }
  171.         return result;
  172.     }
  173. }

 

原文详情地址:http://git.oschina.net/zzunet/wemall-doraemon/commit/e8f303df5663dc69fe47bb9623222149d40e3956

wemall doraemonAndroid app商城详情地址:http://www.koahub.com/home/product/55

wemall官网地址:http://www.wemallshop.com

wemall 开源微商城 ,微信商城,商城源码,三级分销,微生鲜,微水果,微外卖,微订餐---专业的o2o系统

wemall
阅读(51) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册