java http post json

来源:互联网 发布:淘宝有哪些奇葩商品 编辑:程序博客网 时间:2024/05/17 00:52

转载自:http://zheyiw.iteye.com/blog/1571222


Java代码  收藏代码
  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.io.OutputStreamWriter;  
  4. import java.net.HttpURLConnection;  
  5. import java.net.URL;  
  6.   
  7. public class Copy_2_of_PostDemo {  
  8.   
  9.     final static String url = "";  
  10.     final static String params = "{\"id\":\"12345\"}";  
  11.   
  12.     /** 
  13.      * 发送HttpPost请求 
  14.      *  
  15.      * @param strURL 
  16.      *            服务地址 
  17.      * @param params 
  18.      *            json字符串,例如: "{ \"id\":\"12345\" }" ;其中属性名必须带双引号<br/> 
  19.      * @return 成功:返回json字符串<br/> 
  20.      */  
  21.     public static String post(String strURL, String params) {  
  22.         System.out.println(strURL);  
  23.         System.out.println(params);  
  24.         try {  
  25.             URL url = new URL(strURL);// 创建连接  
  26.             HttpURLConnection connection = (HttpURLConnection) url  
  27.                     .openConnection();  
  28.             connection.setDoOutput(true);  
  29.             connection.setDoInput(true);  
  30.             connection.setUseCaches(false);  
  31.             connection.setInstanceFollowRedirects(true);  
  32.             connection.setRequestMethod("POST"); // 设置请求方式  
  33.             connection.setRequestProperty("Accept""application/json"); // 设置接收数据的格式  
  34.             connection.setRequestProperty("Content-Type""application/json"); // 设置发送数据的格式  
  35.             connection.connect();  
  36.             OutputStreamWriter out = new OutputStreamWriter(  
  37.                     connection.getOutputStream(), "UTF-8"); // utf-8编码  
  38.             out.append(params);  
  39.             out.flush();  
  40.             out.close();  
  41.             // 读取响应  
  42.             int length = (int) connection.getContentLength();// 获取长度  
  43.             InputStream is = connection.getInputStream();  
  44.             if (length != -1) {  
  45.                 byte[] data = new byte[length];  
  46.                 byte[] temp = new byte[512];  
  47.                 int readLen = 0;  
  48.                 int destPos = 0;  
  49.                 while ((readLen = is.read(temp)) > 0) {  
  50.                     System.arraycopy(temp, 0, data, destPos, readLen);  
  51.                     destPos += readLen;  
  52.                 }  
  53.                 String result = new String(data, "UTF-8"); // utf-8编码  
  54.                 System.out.println(result);  
  55.                 return result;  
  56.             }  
  57.         } catch (IOException e) {  
  58.             // TODO Auto-generated catch block  
  59.             e.printStackTrace();  
  60.         }  
  61.         return "error"// 自定义错误信息  
  62.     }  
  63.   
  64.     public static void main(String[] args) {  
  65.         post(url, params);  
  66.     }  
  67.   
  68. }  



备注 

httpUrlConnection.setDoOutput(true);以后就可以使用conn.getOutputStream().write() 
httpUrlConnection.setDoInput(true);以后就可以使用conn.getInputStream().read(); 

get请求用不到conn.getOutputStream(),因为参数直接追加在地址后面,因此默认是false。 
post请求(比如:文件上传)需要往服务区传输大量的数据,这些数据是放在http的body里面的,因此需要在建立连接以后,往服务端写数据。 

因为总是使用conn.getInputStream()获取服务端的响应,因此默认值是true。 

0 0
原创粉丝点击