spring-boot httpClient
2991 ワード
依存pomを構成する.xml
org.apache.httpcomponents
httpclient
4.3.1
Getリクエストテスト(post/put/delete同理)
package com.lxf;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class TestHttpClient {
/**
* get
*/
public void get() {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// httpget.
HttpGet httpget = new HttpGet("http://localhost:8099/hello");
System.out.println("executing request " + httpget.getURI());
// get .
CloseableHttpResponse response = httpclient.execute(httpget);
//System.out.println("============="+response.toString());
try {
//
HttpEntity entity = response.getEntity();
System.out.println("--------------------------------------");
//
System.out.println(response.getStatusLine());
if (entity != null) {
//
//System.out.println("Response content length: " + entity.getContentLength());
//
System.out.println("Response content: " + EntityUtils.toString(entity));
}
System.out.println("------------------------------------");
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// ,
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
TestHttpClient client = new TestHttpClient();
Long start = System.currentTimeMillis();
int max=10000;
for(int i=1; i<=max; i++)
{
client.get();
}
Long end = System.currentTimeMillis();
Long elapse = end - start;
int perform = Double.valueOf(max / (elapse / 1000d)).intValue();
System.out.print("Rest " + max + " http , :" + elapse + " , " + perform + " / ");
}
}