JAVA POST请求远程HTTP接口

  1. package com.oemp.common;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.io.OutputStreamWriter;  
  7. import java.io.PrintWriter;  
  8. import java.net.MalformedURLException;  
  9. import java.net.URL;  
  10. import java.net.URLConnection;  
  11. import java.util.HashMap;  
  12. import java.util.Iterator;  
  13. import java.util.Map;  
  14.   
  15. /** 
  16.  * @author Post Method 
  17.  */  
  18. public class HttpPostUrl {  
  19.   
  20.     /** 
  21.      * 向指定URL发送POST请求 
  22.      * @param url 
  23.      * @param paramMap 
  24.      * @return 响应结果 
  25.      */  
  26.     public static String sendPost(String url, Map<String, String> paramMap) {  
  27.         PrintWriter out = null;  
  28.         BufferedReader in = null;  
  29.         String result = "";  
  30.         try {  
  31.             URL realUrl = new URL(url);  
  32.             // 打开和URL之间的连接  
  33.             URLConnection conn = realUrl.openConnection();  
  34.             // 设置通用的请求属性  
  35.             conn.setRequestProperty("accept", "*/*");  
  36.             conn.setRequestProperty("connection", "Keep-Alive");  
  37.             conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  
  38.             // conn.setRequestProperty("Charset", "UTF-8");  
  39.             // 发送POST请求必须设置如下两行  
  40.             conn.setDoOutput(true);  
  41.             conn.setDoInput(true);  
  42.             // 获取URLConnection对象对应的输出流  
  43.             out = new PrintWriter(conn.getOutputStream());  
  44.   
  45.             // 设置请求属性  
  46.             String param = "";  
  47.             if (paramMap != null && paramMap.size() > 0) {  
  48.                 Iterator<String> ite = paramMap.keySet().iterator();  
  49.                 while (ite.hasNext()) {  
  50.                     String key = ite.next();// key  
  51.                     String value = paramMap.get(key);  
  52.                     param += key + "=" + value + "&";  
  53.                 }  
  54.                 param = param.substring(0, param.length() - 1);  
  55.             }  
  56.   
  57.             // 发送请求参数  
  58.             out.print(param);  
  59.             // flush输出流的缓冲  
  60.             out.flush();  
  61.             // 定义BufferedReader输入流来读取URL的响应  
  62.             in = new BufferedReader(  
  63.                     new InputStreamReader(conn.getInputStream()));  
  64.             String line;  
  65.             while ((line = in.readLine()) != null) {  
  66.                 result += line;  
  67.             }  
  68.         } catch (Exception e) {  
  69.             System.err.println("发送 POST 请求出现异常!" + e);  
  70.             e.printStackTrace();  
  71.         }  
  72.         // 使用finally块来关闭输出流、输入流  
  73.         finally {  
  74.             try {  
  75.                 if (out != null) {  
  76.                     out.close();  
  77.                 }  
  78.                 if (in != null) {  
  79.                     in.close();  
  80.                 }  
  81.             } catch (IOException ex) {  
  82.                 ex.printStackTrace();  
  83.             }  
  84.         }  
  85.         return result;  
  86.     }  
  87.       
  88.     /**  
  89.      * 数据流post请求  
  90.      * @param urlStr  
  91.      * @param xmlInfo  
  92.      */  
  93.     public static String doPost(String urlStr, String strInfo) {  
  94.         String reStr="";  
  95.         try {  
  96.             URL url = new URL(urlStr);  
  97.             URLConnection con = url.openConnection();  
  98.             con.setDoOutput(true);  
  99.             con.setRequestProperty("Pragma:", "no-cache");  
  100.             con.setRequestProperty("Cache-Control", "no-cache");  
  101.             con.setRequestProperty("Content-Type", "text/xml");  
  102.             OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());  
  103.             out.write(new String(strInfo.getBytes("utf-8")));  
  104.             out.flush();  
  105.             out.close();  
  106.             BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));  
  107.             String line = "";  
  108.             for (line = br.readLine(); line != null; line = br.readLine()) {  
  109.                 reStr += line;  
  110.             }  
  111.         } catch (MalformedURLException e) {  
  112.             e.printStackTrace();  
  113.         } catch (IOException e) {  
  114.             e.printStackTrace();  
  115.         }  
  116.         return reStr;  
  117.     }  
  118.       
  119.   
  120.     /** 
  121.      * 测试主方法 
  122.      * @param args 
  123.      */  
  124.     public static void main(String[] args) {  
  125.         Map<String, String> mapParam = new HashMap<String, String>();  
  126.         mapParam.put("name", "张三");  
  127.         mapParam.put("validation","test");  
  128.         String pathUrl = "http://localhost/testPost.action";  
  129.         String result = sendPost(pathUrl, mapParam);  
  130.         System.out.println(result);  
  131.   
  132.     }  
  133.   
  134. }  
posted @ 2017-10-12 16:54  偏花逐流水  阅读(1228)  评论(0编辑  收藏  举报