Java學習之HttpClient的GET與POST請求

來源:互聯網
上載者:User

標籤:dep   end   gen   list   air   create   import   prot   div   

這篇隨筆記錄了HttpClient的GET和POST請求

使用maven構建依賴包,我使用的版本是4.5.3

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency> 

代碼如下:

 1 import org.apache.http.HttpEntity; 2 import org.apache.http.HttpResponse; 3 import org.apache.http.NameValuePair; 4 import org.apache.http.client.entity.UrlEncodedFormEntity; 5 import org.apache.http.client.methods.HttpGet; 6 import org.apache.http.client.methods.HttpPost; 7 import org.apache.http.impl.client.CloseableHttpClient; 8 import org.apache.http.impl.client.HttpClients; 9 import org.apache.http.message.BasicNameValuePair;10 import org.apache.http.protocol.HTTP;11 import org.apache.http.util.EntityUtils;12 13 import java.util.ArrayList;14 import java.util.List;15 import java.util.Map;16 17 public class HttpRequest {18     private static final String USER_AGENT = "Mozilla/5.0";19     private static final String TOKEN = "Bearer adad2342dfbcdcb44232bf6a";20     private static final String CONTENT_TYPE = "application/x-www-form-urlencoded";
//發送GET請求
21 public static String sendGet(String url){22 String result = "";23 CloseableHttpClient httpClient = HttpClients.createDefault();24 HttpGet httpGet = new HttpGet(url);25 httpGet.addHeader("User-Agent", USER_AGENT);26 httpGet.addHeader("Authorization", TOKEN);27 try{28 HttpResponse response = httpClient.execute(httpGet);29 HttpEntity entity = response.getEntity();30 result = EntityUtils.toString(entity);31 }catch (Exception e){32 e.printStackTrace();33 }34 return result;35 }36 //發送POST請求37 public static String sendPost(String url,Map<String,String> mapParam){38 String result = "";39 CloseableHttpClient httpClient = HttpClients.createDefault();40 HttpPost httpPost = new HttpPost(url);41 httpPost.setHeader("Content-Type",CONTENT_TYPE);42 httpPost.addHeader("User-Agent" ,USER_AGENT);43 httpPost.addHeader("Authorization", TOKEN);44 try{45 if (mapParam != null){46 List<NameValuePair> postData = new ArrayList<NameValuePair>();47 for (Map.Entry<String,String> entry : mapParam.entrySet()){48 postData.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));49 }50 httpPost.setEntity(new UrlEncodedFormEntity(postData, HTTP.UTF_8));51 }52 HttpResponse response = httpClient.execute(httpPost);53 HttpEntity entity = response.getEntity();54 result = EntityUtils.toString(entity);55 }catch (Exception e){56 e.printStackTrace();57 }58 return result;59 }60 }

 

Java學習之HttpClient的GET與POST請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.