輸入org.apache.commons.httpclient.HttpStatus。
輸入org.apache.http.client.config.CookieSpecs。
輸入org.apache.http.client.config.RequestConfig;
輸入org.apache.http.client.methods.CloseableHttpResponse。
輸入org.apache.http.client.methods.HttpPost;
輸入org.apache.http.entity.StringEntity。
輸入org.apache.http.impl.client.CloseableHttpClient。
輸入org.apache.http.impl.client.DefaultHttpRequestRetryHandler。
輸入org.apache.http.impl.client.HttpClients。
輸入org.apache.http.impl.conn.PoolingHttpClientConnectionManager。
輸入org.apache.http.util.EntityUtils。
/ **
* HTTP接続プールの制御
* /
publicクラスHttpClientPool {
プライベート・クライアント静的揮発性CloseableHttpClient = NULL;
プライベート= NULL静的PoolingHttpClientConnectionManager CM<
プライベート静的RequestConfig設定= NULL;
// ----------------------- --- --------------------------接続プロパティを設定する
プライベート静的int型のrequestTimeout = 120000;プールから//時間の取得要求
プライベート静的int型のconnecttimeout = 120000; //時間は、サーバーに接続する
プライベート静的int型socketTimeout = 120000;情報を読み取る//時間
// ---------------接続プールの構成プロパティを---- ----------------------
プライベートmaxTotal静的int型= 400; //接続の最大数
プライベート静的int型defaultMaxPerRoute = 200; //デフォルトの単一のアドレスは接続数を超えていません最大接続データ
// ------------------------------------------------ -----
静的{
コンフィグ= RequestConfig.custom()
.setConnectionRequestTimeout(のrequestTimeout)
.setConnectTimeout(のconnecttimeout)
.setSocketTimeout(socketTimeout).build();
CM =新しいPoolingHttpClientConnectionManager();
cm.setMaxTotal(maxTotal)。
cm.setDefaultMaxPerRoute(defaultMaxPerRoute)。
RequestConfig globalConfig = RequestConfig.custom()setCookieSpec(CookieSpecs.IGNORE_COOKIES).build()。
クライアント= HttpClients.custom()
.setRetryHandler(DefaultHttpRequestRetryHandler.INSTANCE)
.setDefaultRequestConfig(設定)
.setConnectionManager(センチ)
.setDefaultRequestConfig(globalConfig)
.build();
}
パブリック静的CloseableHttpClient getHttpClient(){
IF(ヌル==クライアント){
同期(AmarHttpClientPool.class){
IF(ヌル==クライアント){
RequestConfig globalConfig = RequestConfig.custom()setCookieSpec(CookieSpecs.IGNORE_COOKIES).build()。
クライアント= HttpClients.custom()
.setRetryHandler(DefaultHttpRequestRetryHandler.INSTANCE)
.setDefaultRequestConfig(設定)
.setConnectionManager(センチ)
.setDefaultRequestConfig(globalConfig)
.build();
}
}
}
クライアントを返します。
} @SuppressWarningsは( "未使用")
パブリック静的文字列例外{スロー)(実行
文字列のURL = "HTTP:// ..."と、
CloseableHttpClientのHttpClient = HttpClients.createDefault(); // HttpClientPool.getHttpClient();
HttpPost httpPost =新しいHttpPost(URL);
RequestConfigコンフィグ= RequestConfig.custom()setConnectTimeout(5000).setSocketTimeout(3000).setConnectionRequestTimeout(5000))(.build。
httpPost.setConfig(設定)。
httpPost.setEntity(新しいStringEntity( "请求参数...."、 "UTF-8"));
試みる(CloseableHttpResponse応答= httpClient.execute(httpPost)){
int型のステータス= response.getStatusLine()。
(!ステータス= HttpStatus.SC_OK){IF
スロー新しい新しい例外(:+ URL + "失敗した戻りステータスコード:" "サービスコール" +ステータス);
}
文字列の結果= EntityUtils.toString(response.getEntity());
システム.out.println( "元のメッセージ:"結果+);
文字列startStr = "<のNS1:OUT>";
int型のstartIndex = result.indexOf(startStr);
INT endIndexの= result.indexOf(「</のNS1:OUT> ");
アサートのstartIndex> = 0 && endIndexの> = 0:"新聞スタイルフラグ<NS1:アウト>または</ NS1:アウト>が存在しない場合、元のメッセージ:「結果+;
EntityUtils.consume(response.getEntity( ));
戻りresult.substring(のstartIndex + startStr.length()、endIndexの)。
}
}
/ **
*通話ケース
* /
パブリック静的無効メイン(文字列[] args){
CloseableHttpClientのHTTPClient = HttpClientPool.getHttpClient()。
HttpPost httpPost =新HttpPost( "URL");
httpPost.setHeader( "コンテンツタイプ"、 "アプリケーション/ JSON;のcharset = UTF-8");
httpPost.setHeader( "同意する"、 "アプリケーション/ JSON");
httpPost.setHeader( "X-転送さ-のために"、 "IP");
httpPost.setEntity(新しいStringEntity(requestParams.toString()、 "UTF-8"));
試みる(CloseableHttpResponse応答= httpclient.execute(httpPost)){
int型のステータス= response.getStatusLine()getStatusCode()。
もし(ステータス= HttpStatus.SC_OK!){
新しい例外を投げる( "调用服务:" + hubserviceUrl + "失败、返回状态码为:" +状態)。
}
HttpEntityエンティティ= response.getEntity()。
IF(!エンティティ= NULL){
RESP = EntityUtils.toString(エンティティ、 "UTF-8");
}
EntityUtils.consume(response.getEntity())。
}
}
}