httpClient的post方法使用form格式數據調用接口


Http使用post調用接口,接口只接受form表單數據格式問題?

這個問題的關鍵是form表單提交的是數據格式是什么樣子的,使用httpClient工具類時Header信息如何寫。

會影響解決問題思路的因素:

1、         一致以來都是使用json數據格式調用接口的,form表單是什么格式一時向不起來。

2、         使用form表單數據情況,多是在前台頁面使用form表單提交,或使用JavaScript中的FormData對象處理提交。如果是后台httpClient工具接口如何提交

解決思路:

1、先百度看網上怎么說的,找到一個有用的帖子:https://www.cnblogs.com/zhang-can/p/7631262.html 中有一句:

ajax發送的data是個字典,是鍵值對的形式,在http的post請求過程中,把這種鍵值對轉換成

k1=xxx&k2=xxx這種格式,並且會帶上一個請求頭:

content-type : application/x-www-form-urlencoded

2、 前台訪問后台實際實現也是http協議,那使用谷歌的調試工具,模擬一個form表單提交看看請求header和Form Data的情況:

 

點擊“view source”顯示的格式:

 

這是我們熟悉的格式。

根據上面的測試,修改httpClient的post工具類:

關鍵的地方是:post請求的Header設置

httpPost.addHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");

在post中的參數格式為:param1=小明&param2=12

調用時實例:

HttpRequestUtil.post(url地址
        , “param1=小明&param2=12”);

 

測試結果:

返回參數:{"data":{},"code":"0","msg":"處理成功"}

工具類代碼:

  1 import org.apache.http.client.config.RequestConfig;
  2 import org.apache.http.client.methods.CloseableHttpResponse;
  3 import org.apache.http.client.methods.HttpGet;
  4 import org.apache.http.client.methods.HttpPost;
  5 import org.apache.http.entity.StringEntity;
  6 import org.apache.http.impl.client.CloseableHttpClient;
  7 import org.apache.http.impl.client.HttpClients;
  8 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
  9 
 10 import java.io.BufferedReader;
 11 import java.io.IOException;
 12 import java.io.InputStreamReader;
 13 import java.nio.charset.Charset;
 14 
 15 /**
 16  * Created by guoyanan on 2018/8/7 0007.
 17  * 接口調用工具類
 18  */
 19 public class HttpRequestUtil {
 20     private static CloseableHttpClient httpClient;
 21 
 22     static {
 23         PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
 24         cm.setMaxTotal(100);
 25         cm.setDefaultMaxPerRoute(20);
 26         cm.setDefaultMaxPerRoute(50);
 27         httpClient = HttpClients.custom().setConnectionManager(cm).build();
 28     }
 29 
 30     public static String get(String url) {
 31         CloseableHttpResponse response = null;
 32         BufferedReader in = null;
 33         String result = "";
 34         try {
 35             HttpGet httpGet = new HttpGet(url);
 36             RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
 37             httpGet.setConfig(requestConfig);
 38             httpGet.setConfig(requestConfig);
 39             httpGet.addHeader("Content-type", "application/json; charset=utf-8");
 40             httpGet.setHeader("Accept", "application/json");
 41             response = httpClient.execute(httpGet);
 42             in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
 43             StringBuffer sb = new StringBuffer("");
 44             String line = "";
 45             String NL = System.getProperty("line.separator");
 46             while ((line = in.readLine()) != null) {
 47                 sb.append(line + NL);
 48             }
 49             in.close();
 50             result = sb.toString();
 51         } catch (IOException e) {
 52             e.printStackTrace();
 53         } finally {
 54             try {
 55                 if (null != response) {
 56                     response.close();
 57                 }
 58             } catch (IOException e) {
 59                 e.printStackTrace();
 60             }
 61         }
 62         return result;
 63     }
 64 
 65     public static String post(String url, String jsonString) {
 66         CloseableHttpResponse response = null;
 67         BufferedReader in = null;
 68         String result = "";
 69         try {
 70             HttpPost httpPost = new HttpPost(url);
 71             RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
 72             httpPost.setConfig(requestConfig);
 73             httpPost.setConfig(requestConfig);
 74             httpPost.addHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
 75             httpPost.setHeader("Accept", "application/json");
 76             httpPost.setEntity(new StringEntity(jsonString, Charset.forName("UTF-8")));
 77 
 78             response = httpClient.execute(httpPost);
 79             in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
 80             StringBuffer sb = new StringBuffer("");
 81             String line = "";
 82             String NL = System.getProperty("line.separator");
 83             while ((line = in.readLine()) != null) {
 84                 sb.append(line + NL);
 85             }
 86             in.close();
 87             result = sb.toString();
 88         } catch (IOException e) {
 89             e.printStackTrace();
 90         } finally {
 91             try {
 92                 if (null != response) {
 93                     response.close();
 94                 }
 95             } catch (IOException e) {
 96                 e.printStackTrace();
 97             }
 98         }
 99         return result;
100     }
101 
102 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM