码迷,mamicode.com
首页 > 编程语言 > 详细

java发送http请求

时间:2015-06-03 15:58:46      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:package   public   return   import   java   

java发送http请求
  1. package com.stock.show.util;  

  2.   

  3. import java.io.BufferedReader;  

  4. import java.io.IOException;  

  5. import java.io.InputStreamReader;  

  6. import java.io.PrintWriter;  

  7. import java.net.URL;  

  8. import java.net.URLConnection;  

  9. import java.util.List;  

  10. import java.util.Map;  

  11.   

  12. public class HttpRequestUtil {  

  13.   

  14.     /** 

  15.      * 向指定URL发送GET方法的请求 

  16.      * 

  17.      * @param url 

  18.      *            发送请求的URL 

  19.      * @param param 

  20.      *            请求参数 

  21.      * @return URL 所代表远程资源的响应结果 

  22.      */  

  23.     public static String sendGet(String url, Map<String,String> param) {  

  24.         String result = "";  

  25.         BufferedReader in = null;  

  26.         try {  

  27.             String urlNameString = url;  

  28.   

  29.             //增加参数  

  30.             if(param!=null&m.size()>0){  

  31.                 urlNameString = urlNameString+"?";  

  32.                 for(String key : param.keySet()){  

  33.                     urlNameString = urlNameString + key + "=" + param.get(key);  

  34.                 }  

  35.             }  

  36.   

  37.             URL realUrl = new URL(urlNameString);  

  38.             // 打开和URL之间的连接  

  39.             URLConnection connection = realUrl.openConnection();  

  40.             // 设置通用的请求属性  

  41.             connection.setRequestProperty("accept", "*/*");  

  42.             connection.setRequestProperty("connection", "Keep-Alive");  

  43.             connection.setRequestProperty("user-agent",  

  44.                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  

  45.             // 建立实际的连接  

  46.             connection.connect();  

  47.             // 获取所有响应头字段  

  48.             Map<String, List<String>> map = connection.getHeaderFields();  

  49.             // 遍历所有的响应头字段  

  50.             for (String key : map.keySet()) {  

  51.                 System.out.println(key + "--->" + map.get(key));  

  52.             }  

  53.             // 定义 BufferedReader输入流来读取URL的响应  

  54.             in = new BufferedReader(new InputStreamReader(  

  55.                     connection.getInputStream()));  

  56.             String line;  

  57.             while ((line = in.readLine()) != null) {  

  58.                 result += line;  

  59.             }  

  60.         } catch (Exception e) {  

  61.             System.out.println("发送GET请求出现异常!" + e);  

  62.             e.printStackTrace();  

  63.         }  

  64.         // 使用finally块来关闭输入流  

  65.         finally {  

  66.             try {  

  67.                 if (in != null) {  

  68.                     in.close();  

  69.                 }  

  70.             } catch (Exception e2) {  

  71.                 e2.printStackTrace();  

  72.             }  

  73.         }  

  74.         return result;  

  75.     }  

  76.   

  77.     /**  

  78.      * 向指定 URL 发送POST方法的请求  

  79.      *  

  80.      * @param url  

  81.      *            发送请求的 URL  

  82.      * @param param  

  83.      *            请求参数  

  84.      * @return 所代表远程资源的响应结果  

  85.      */  

  86.     public static String sendPost(String url, Map<String,String> param) {  

  87.         PrintWriter out = null;  

  88.         BufferedReader in = null;  

  89.         String result = "";  

  90.         try {  

  91.             URL realUrl = new URL(url);  

  92.             // 打开和URL之间的连接  

  93.             URLConnection conn = realUrl.openConnection();  

  94.             // 设置通用的请求属性  

  95.             conn.setRequestProperty("accept", "*/*");  

  96.             conn.setRequestProperty("connection", "Keep-Alive");  

  97.             conn.setRequestProperty("user-agent",  

  98.                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  

  99.             // 发送POST请求必须设置如下两行  

  100.             conn.setDoOutput(true);  

  101.             conn.setDoInput(true);  

  102.             // 获取URLConnection对象对应的输出流  

  103.             out = new PrintWriter(conn.getOutputStream());  

  104.   

  105.             //处理参数  

  106.             String paramString = "";  

  107.             if(param!=null&m.size()>0){  

  108.                 for(String key : param.keySet()){  

  109.                     paramString = paramString + key + "=" + param.get(key);  

  110.                 }  

  111.             }  

  112.             // 发送请求参数  

  113.             out.print(paramString);  

  114.             // flush输出流的缓冲  

  115.             out.flush();  

  116.             // 定义BufferedReader输入流来读取URL的响应  

  117.             in = new BufferedReader(  

  118.                     new InputStreamReader(conn.getInputStream()));  

  119.             String line;  

  120.             while ((line = in.readLine()) != null) {  

  121.                 result += line;  

  122.             }  

  123.         } catch (Exception e) {  

  124.             System.out.println("发送 POST 请求出现异常!"+e);  

  125.             e.printStackTrace();  

  126.         }  

  127.         //使用finally块来关闭输出流、输入流  

  128.         finally{  

  129.             try{  

  130.                 if(out!=null){  

  131.                     out.close();  

  132.                 }  

  133.                 if(in!=null){  

  134.                     in.close();  

  135.                 }  

  136.             }  

  137.             catch(IOException ex){  

  138.                 ex.printStackTrace();  

  139.             }  

  140.         }  

  141.         return result;  

  142.     }  

  143.   

  144.     public static void main(String[] args) {  

  145.         //发送 GET 请求  

  146.         String s=HttpRequestUtil.sendGet("http://hq.sinajs.cn/list=sh601006", null);  

  147.         System.out.println(s);  

  148.   

  149.   

  150.     }  

  151.   

  152. }  


java发送http请求

标签:package   public   return   import   java   

原文地址:http://wufanxin.blog.51cto.com/7318130/1657879

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!