91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 6KB
    文件類型: .java
    金幣: 1
    下載: 0 次
    發(fā)布日期: 2021-05-10
  • 語言: Java
  • 標簽: httpclient??get??post??json??

資源簡介

httpclient get/post請求工具類(map參數(shù)封裝),方便調用

資源截圖

代碼片段和文件信息

package?com.abc.controller;

import?com.alibaba.fastjson.JSON;
import?com.alibaba.fastjson.JSONobject;
import?org.apache.http.HttpEntity;
import?org.apache.http.HttpStatus;
import?org.apache.http.NameValuePair;
import?org.apache.http.client.entity.UrlEncodedFormEntity;
import?org.apache.http.client.methods.CloseableHttpResponse;
import?org.apache.http.client.methods.HttpGet;
import?org.apache.http.client.methods.HttpPost;
import?org.apache.http.client.utils.URIBuilder;
import?org.apache.http.entity.ByteArrayEntity;
import?org.apache.http.impl.client.CloseableHttpClient;
import?org.apache.http.impl.client.HttpClientBuilder;
import?org.apache.http.impl.client.HttpClients;
import?org.apache.http.message.BasicNameValuePair;
import?org.apache.http.util.EntityUtils;

import?java.io.IOException;
import?java.net.URISyntaxException;
import?java.util.ArrayList;
import?java.util.List;
import?java.util.Map;

/**
?*?Created?by?Administrator?on?2017/6/16.
?*/
public?class?HttpClientUtil?{

????/**
?????*?get請求
?????*
?????*?@param?url????請求地址
?????*?@param?params?map參數(shù)
?????*?@return?json
?????*/
????public?static?JSONobject?getJsonData(String?url?Map?params)?{
????????JSONobject?jsonobject?=?null;
????????//?獲取當前客戶端對象
//????????HttpClient?httpClient?=?HttpClientUtil.createDefault();
????????CloseableHttpClient?httpClient?=?HttpClients.createDefault();
????????List?pairs?=?new?ArrayList(params.size());
????????for?(Map.Entry?entry?:?params.entrySet())?{
????????????pairs.add(new?BasicNameValuePair(entry.getKey()?entry.getValue()));
????????}
????????CloseableHttpResponse?response?=?null;

????????try?{
????????????URIBuilder?builder?=?new?URIBuilder(url);
????????????builder.setParameters(pairs);
????????????//?根據(jù)地址發(fā)送get請求
????????????HttpGet?request?=?new?HttpGet(builder.build());
????????????//?通過請求對象獲取響應對象
????????????response?=?httpClient.execute(request);
????????????//?判斷網(wǎng)絡連接狀態(tài)碼是否正常(0--200都數(shù)正常)
????????????if?(response?!=?null?&&?response.getStatusLine().getStatusCode()?==?HttpStatus.SC_OK)?{
????????????????HttpEntity?entity?=?response.getEntity();
????????????????String?result?=?EntityUtils.toString(entity?“UTF-8“);
????????????????//?返回json格式:
????????????????jsonobject?=?JSON.parseobject(result);
????????????}
????????}?catch?(URISyntaxException?e)?{
????????????e.printStackTrace();
????????}?catch?(IOException?e)?{
????????????e.printStackTrace();
????????}?finally?{
????????????try?{
????????????????httpClient.close();
????????????????if?(response?!=?null)?{
????????????????????response.close();
????????????????}
????????????}?catch?(IOException?e)?{
????????????????e.printStackTrace();
????????????}
????????}
????????return?jsonobject;
????}

????/**
?????*?post請求
?????*?@param?url?url
?????*?@par

評論

共有 條評論