zoukankan      html  css  js  c++  java
  • httpclient发送请求的几种方式

    package asi;
    import org.apache.http.HttpEntity;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    public class TestHttpClient {
        
     public static void main(String[] args){
      RequestConfig requestConfig = RequestConfig.custom()  
                 .setSocketTimeout(150000)  
                 .setConnectTimeout(150000)  
                 .setConnectionRequestTimeout(150000)  
                 .build();  
         CloseableHttpClient httpClient =  HttpClients.createDefault();
         CloseableHttpResponse response = null;  
         HttpEntity httpEntity = null;  
         String responseContent = null; 
         
      String url=...
      String json=...
      HttpPost httpPost=new HttpPost(url);  
         httpPost.addHeader("Content-type","application/json; charset=utf-8");  
      httpPost.setHeader("Accept", "application/json");
         try {  
             StringEntity stringEntity = new StringEntity(json, "UTF-8");  
             stringEntity.setContentType("application/x-www-form-urlencoded");  
             httpPost.setEntity(stringEntity);  
             httpPost.setConfig(requestConfig);         
             response = httpClient.execute(httpPost);  
             httpEntity = response.getEntity();  
             responseContent = EntityUtils.toString(httpEntity, "UTF-8");
         } catch (Exception e) {  
             e.printStackTrace();  
         }  
         System.out.println(responseContent);
     }
     
    }
  • 相关阅读:
    [Algorithms] Counting Sort
    [LeetCode] Sort Colors
    [LeetCode] Contains Duplicate III
    [LeetCode] Contains Duplicate
    [LeetCode] Two Sum II
    [LeetCode] Linked List Cycle II
    [LeetCode] Linked List Cycle
    [LeetCode] Longest Palindromic Substring
    [LeetCode] Two Sum
    [LeetCode] Rectangle Area
  • 原文地址:https://www.cnblogs.com/BonnieWss/p/9225702.html
Copyright © 2011-2022 走看看