Java工具类——发送GET/POST请求工具

  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.io.InputStreamReader;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.URL;  
  7. import java.nio.charset.Charset;  
  8. import java.util.Map;  
  9. import java.util.Vector;  
  10.   
  11. /** 
  12.  * Java HTTP请求对象 发送GET/POST请求工具类 
  13.  */  
  14. public class HttpRequester {  
  15.   
  16.     private String defaultContentEncoding;  
  17.   
  18.     public HttpRequester() {  
  19.         this.defaultContentEncoding = Charset.defaultCharset().name();  
  20.     }  
  21.   
  22.     /** 
  23.      * 发送GET请求 
  24.      *  
  25.      * @param urlString  URL地址 
  26.      * @return 响应对象 
  27.      * @throws IOException 
  28.      */  
  29.     public HttpRespons sendGet(String urlString) throws IOException {  
  30.         return this.send(urlString, "GET"nullnull);  
  31.     }  
  32.   
  33.     /** 
  34.      * 发送GET请求 
  35.      *  
  36.      * @param urlString  URL地址 
  37.      * @param params 参数集合 
  38.      * @return 响应对象 
  39.      * @throws IOException 
  40.      */  
  41.     public HttpRespons sendGet(String urlString, Map<String, String> params) throws IOException {  
  42.         return this.send(urlString, "GET", params, null);  
  43.     }  
  44.   
  45.     /** 
  46.      * 发送GET请求 
  47.      *  
  48.      * @param urlString URL地址 
  49.      * @param params 参数集合 
  50.      * @param propertys 请求属性 
  51.      * @return 响应对象 
  52.      * @throws IOException 
  53.      */  
  54.     public HttpRespons sendGet(String urlString, Map<String, String> params, Map<String, String> propertys)  
  55.             throws IOException {  
  56.         return this.send(urlString, "GET", params, propertys);  
  57.     }  
  58.   
  59.     /** 
  60.      * 发送POST请求 
  61.      *  
  62.      * @param urlString URL地址 
  63.      * @return 响应对象 
  64.      * @throws IOException 
  65.      */  
  66.     public HttpRespons sendPost(String urlString) throws IOException {  
  67.         return this.send(urlString, "POST"nullnull);  
  68.     }  
  69.   
  70.     /** 
  71.      * 发送POST请求 
  72.      *  
  73.      * @param urlString URL地址 
  74.      * @param params 参数集合 
  75.      * @return 响应对象 
  76.      * @throws IOException 
  77.      */  
  78.     public HttpRespons sendPost(String urlString, Map<String, String> params) throws IOException {  
  79.         return this.send(urlString, "POST", params, null);  
  80.     }  
  81.   
  82.     /** 
  83.      * 发送POST请求 
  84.      *  
  85.      * @param urlString URL地址 
  86.      * @param params 参数集合 
  87.      * @param propertys 请求属性 
  88.      * @return 响应对象 
  89.      * @throws IOException 
  90.      */  
  91.     public HttpRespons sendPost(String urlString, Map<String, String> params, Map<String, String> propertys)  
  92.             throws IOException {  
  93.         return this.send(urlString, "POST", params, propertys);  
  94.     }  
  95.   
  96.     /** 
  97.      * 发送HTTP请求 
  98.      *  
  99.      * @param urlString 地址 
  100.      * @param method  get/post 
  101.      * @param parameters  添加由键值对指定的请求参数 
  102.      * @param propertys  添加由键值对指定的一般请求属性 
  103.      * @return 响映对象 
  104.      * @throws IOException 
  105.      */  
  106.     private HttpRespons send(String urlString, String method, Map<String, String> parameters,  
  107.             Map<String, String> propertys) throws IOException {  
  108.         HttpURLConnection urlConnection = null;  
  109.   
  110.         if (method.equalsIgnoreCase("GET") && parameters != null) {  
  111.             StringBuffer param = new StringBuffer();  
  112.             int i = 0;  
  113.             for (String key : parameters.keySet()) {  
  114.                 if (i == 0)  
  115.                     param.append("?");  
  116.                 else  
  117.                     param.append("&");  
  118.                 param.append(key).append("=").append(parameters.get(key));  
  119.                 i++;  
  120.             }  
  121.             urlString += param;  
  122.         }  
  123.   
  124.         URL url = new URL(urlString);  
  125.         urlConnection = (HttpURLConnection) url.openConnection();  
  126.         urlConnection.setRequestMethod(method);  
  127.         urlConnection.setDoOutput(true);  
  128.         urlConnection.setDoInput(true);  
  129.         urlConnection.setUseCaches(false);  
  130.   
  131.         if (propertys != null)  
  132.             for (String key : propertys.keySet()) {  
  133.                 urlConnection.addRequestProperty(key, propertys.get(key));  
  134.             }  
  135.   
  136.         if (method.equalsIgnoreCase("POST") && parameters != null) {  
  137.             StringBuffer param = new StringBuffer();  
  138.             for (String key : parameters.keySet()) {  
  139.                 param.append("&");  
  140.                 param.append(key).append("=").append(parameters.get(key));  
  141.             }  
  142.             urlConnection.getOutputStream().write(param.toString().getBytes());  
  143.             urlConnection.getOutputStream().flush();  
  144.             urlConnection.getOutputStream().close();  
  145.         }  
  146.         return this.makeContent(urlString, urlConnection);  
  147.     }  
  148.   
  149.     /** 
  150.      * 得到响应对象 
  151.      *  
  152.      * @param urlConnection 
  153.      * @return 响应对象 
  154.      * @throws IOException 
  155.      */  
  156.     private HttpRespons makeContent(String urlString, HttpURLConnection urlConnection) throws IOException {  
  157.         HttpRespons httpResponser = new HttpRespons();  
  158.         try {  
  159.             InputStream in = urlConnection.getInputStream();  
  160.             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));  
  161.             httpResponser.contentCollection = new Vector<String>();  
  162.             StringBuffer temp = new StringBuffer();  
  163.             String line = bufferedReader.readLine();  
  164.             while (line != null) {  
  165.                 httpResponser.contentCollection.add(line);  
  166.                 temp.append(line).append("\r\n");  
  167.                 line = bufferedReader.readLine();  
  168.             }  
  169.             bufferedReader.close();  
  170.             String ecod = urlConnection.getContentEncoding();  
  171.             if (ecod == null)  
  172.                 ecod = this.defaultContentEncoding;  
  173.             httpResponser.urlString = urlString;  
  174.             httpResponser.defaultPort = urlConnection.getURL().getDefaultPort();  
  175.             httpResponser.file = urlConnection.getURL().getFile();  
  176.             httpResponser.host = urlConnection.getURL().getHost();  
  177.             httpResponser.path = urlConnection.getURL().getPath();  
  178.             httpResponser.port = urlConnection.getURL().getPort();  
  179.             httpResponser.protocol = urlConnection.getURL().getProtocol();  
  180.             httpResponser.query = urlConnection.getURL().getQuery();  
  181.             httpResponser.ref = urlConnection.getURL().getRef();  
  182.             httpResponser.userInfo = urlConnection.getURL().getUserInfo();  
  183.             httpResponser.content = new String(temp.toString().getBytes(), ecod);  
  184.             httpResponser.contentEncoding = ecod;  
  185.             httpResponser.code = urlConnection.getResponseCode();  
  186.             httpResponser.message = urlConnection.getResponseMessage();  
  187.             httpResponser.contentType = urlConnection.getContentType();  
  188.             httpResponser.method = urlConnection.getRequestMethod();  
  189.             httpResponser.connectTimeout = urlConnection.getConnectTimeout();  
  190.             httpResponser.readTimeout = urlConnection.getReadTimeout();  
  191.             return httpResponser;  
  192.         } catch (IOException e) {  
  193.             throw e;  
  194.         } finally {  
  195.             if (urlConnection != null)  
  196.                 urlConnection.disconnect();  
  197.         }  
  198.     }  
  199.   
  200.     /** 
  201.      * 默认的响应字符集 
  202.      */  
  203.     public String getDefaultContentEncoding() {  
  204.         return this.defaultContentEncoding;  
  205.     }  
  206.   
  207.     /** 
  208.      * 设置默认的响应字符集 
  209.      */  
  210.     public void setDefaultContentEncoding(String defaultContentEncoding) {  
  211.         this.defaultContentEncoding = defaultContentEncoding;  
  212.     }  
  213. }  

转自【http://www.open-open.com/lib/view/open1374585832808.html】
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值