zoukankan      html  css  js  c++  java
  • java http url post json

    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。

  • 相关阅读:
    Ural 1996 Cipher Message 3 (生成函数+FFT)
    UVA 12633 Super Rooks on Chessboard (生成函数+FFT)
    HDU 5307 He is Flying (生成函数+FFT)
    BZOJ 2039 人员雇佣 (最小割)
    BZOJ 3158 千钧一发 (最大流->二分图带权最大独立集)
    BZOJ 3144 [HNOI2013]切糕 (最大流+巧妙的建图)
    BZOJ 3774 最优选择 (最小割+二分图)
    BZOJ 3876 [AHOI/JSOI2014]支线剧情 (最小费用可行流)
    BZOJ 3771 Triple (FFT+生成函数+容斥)
    洛谷 P3121 【[USACO15FEB]审查(黄金)Censoring (Gold)】
  • 原文地址:https://www.cnblogs.com/silentmuh/p/5319889.html
Copyright © 2011-2022 走看看