Meven
<依存> <groupIdを> org.apache.httpcomponents </ groupIdを> <たartifactId> HTTPClientの</たartifactId> <バージョン> 4.5。2 </バージョン> </依存関係>
- コード
輸入com.alibaba.fastjson.JSON。 輸入com.alibaba.fastjson.JSONObject。 輸入ロンボク。extern .slf4j.Slf4j; 輸入org.apache.http.HttpEntity。 輸入org.apache.http.HttpStatus。 輸入org.apache.http.NameValuePair; 輸入org.apache.http.ParseException。 輸入org.apache.http.client.config.RequestConfig; 輸入org.apache.http.client.entity.UrlEncodedFormEntity。 輸入org.apache.http.client.methods.CloseableHttpResponse。 輸入org.apache.http.client.methods.HttpGet; 輸入org.apache.http.client.methods.HttpPost; 輸入org.apache.http.entity.StringEntity。 輸入org.apache.http.impl.client.CloseableHttpClient。 輸入org.apache.http.impl.client.HttpClients。 輸入org.apache.http.message.BasicNameValuePair; 輸入org.apache.http.util.EntityUtils。 インポートにjava.io.IOException; 輸入java.utilの。* ; / * * * HttpClient4.3工具类 * / @ SLF4J パブリック クラスHttpClientUtils { プライベート 静的 RequestConfig requestConfig = nullを。 プライベート 静的な文字列のcharset = " UTF-8 " ; パブリック 静的 ボイドメイン(文字列[]引数){ JSONObjectのTT = 新しいです JSONObject()。 tt.put(" AAA "、" 4444 " ); 地図ミリメートル = 新しいHashMapの(); mm.put(" AAA "、" 555 " ); // のSystem.out.println( "結果:" + httpPost(」http://127.0.0.1 :8761 / HI1" 、TT))。 // のSystem.out.println( "結果:" + httpPost(」http://127.0.0.1 :8761 / HI1" 、MM))。 // するSystem.out.println( "結果:" + httpPost(」http://127.0.0.1 :8761 / HI1" 、JSON。 システム。アウト .println(" 結果: " + httpPostForm(" http://127.0.0.1:8761/hi2 " 、MM)); } 静的{ // 設定要求と送信タイムアウト 。requestConfig = RequestConfig.custom()setSocketTimeout(5000) .setConnectTimeout(5000 ).build(); } / * * * @paramのURL * @param OBJ 1.地図3.JSONObject JSON文字列2 * @return JSONObject * / パブリック 静的JSONObject HttpPost(URL文字列、オブジェクトobj) { // POSTリクエストリターン結果 CloseableHttpClientのHttpClient =HttpClients.createDefault(); JSONObject化するJsonResult = nullを。 HttpPost httpPost = 新しいHttpPost(URL); // 设置请求和传输超时时间 httpPost.setConfig(requestConfig)。 してみてください{ 場合(ヌル!= OBJ){ StringEntityエンティティ = nullを。 もし(OBJ列のinstanceof){ エンティティ = 新しいStringEntity(obj.toString()、文字セット)。 } 他{ エンティティ = 新しいStringEntity(JSON.toJSONString(OBJ)、文字セット)。 } entity.setContentEncoding(文字セット)。 entity.setContentType(" アプリケーション/ JSON " )。 httpPost.setEntity(エンティティ) } log.debug(" {} - {} " 、URL、OBJ)。 CloseableHttpResponse応答 = httpClient.execute(httpPost)。 返すconvertResponse(応答)。 } キャッチ(例外e){ log.error("エラーHttpClientUtils {} - {} - {} " + URLは、OBJ、E); } 最後に{ httpPost.releaseConnection(); } 戻り化するJsonResultを; } / * * など* POST要求送信文字列パラメータ:名=ジャック&性別= 1 &タイプ= 2 コンテンツタイプ*:ファイルアプリケーション/ x-www-form-urlencodedで * * @paramのURLアドレスのURL * @param * @return * / パブリック 静的 JSONObject httpPostForm(文字列URL、地図<文字列、文字列> のparams ){ // POSTリクエストリターン結果 CloseableHttpClientのHttpClient =HttpClients.createDefault(); JSONObject化するJsonResult =はnull ; HttpPost httpPost = 新しいHttpPost(URL); httpPost.setConfig(requestConfig)。 してみてください{ 場合(ヌル =の!のparams ){ // 组织请求参数 一覧<のNameValuePair> paramList = 新しいのArrayList <のNameValuePair> (); もし(のparams!= nullの && のparams .size()> 0 ){ 設定 <文字列>のkeySet = paramsは.keySet()。 にとって(文字列のキー:のkeySet){ paramList.add(新しい BasicNameValuePair(キー、のparams。取得(キー))); } } httpPost.setEntity(新しいUrlEncodedFormEntity(paramList、文字セット))。 } CloseableHttpResponse応答 = httpClient.execute(httpPost)。 返すconvertResponse(応答)。 } キャッチ(IOExceptionを電子){ log.error(" ポスト请求提交失败:" + URL、E)。 } 最後に { httpPost.releaseConnection(); } を返す化するJsonResultを; } / * * *送信要求を取得する * * @paramのURLパス * @return * / パブリック 静的JSONObject HTTPGET(文字列のURL){ // GET要求リターンが結果 JSONObject化するJsonResult = ヌル; CloseableHttpClientクライアント = HttpClients.createDefault(); // get要求送信 HTTPGET =要求し、新たな新HTTPGET(URL); request.setConfig(requestConfig)。 試す { CloseableHttpResponse応答を = client.execute(リクエスト)。 convertResponse(応答)。 } キャッチ(例外e){ log.error(" 请求提交失败得る:" + URL、E)。 } 最後に{ request.releaseConnection()。 } 戻り化するJsonResultと、 } プライベート 静的JSONObject convertResponse(CloseableHttpResponseの応答)にIOException、はParseException {スロー // 请求发送成功、并得到は响应 場合(response.getStatusLine()。getStatusCode()== HttpStatus.SC_OK){ // データを介してサーバによって返される読み取りJSONストリング HttpEntityエンティティ= response.getEntity(); 文字列strResult = EntityUtils.toString(エンティティ、" UTF- 8 " ); // のJSON JSONオブジェクトに文字列を変換し 戻す。JSONObject.parseObject(strResult)を } 他{ log.error(" {} 」、応答); } 戻り NULL ; } }