今天说一下使用HttpClient发送http请求
使用get无参请求方式
private CloseableHttpClient closeableHttpClient = null;
private CloseableHttpResponse closeableHttpResponse = null;
closeableHttpClient = HttpClientBuilder.create().build();
/**
* get 无参方式
*
* @param url
* @return
* @throws IOException
*/
public CloseableHttpResponse DoGet(String url) throws IOException {
HttpGet httpGet = new HttpGet(url);
closeableHttpResponse = closeableHttpClient.execute(httpGet);
return closeableHttpResponse;
}
//主方法
public static void main(String[] args) throws IOException {
HttpTest httpTest = new HttpTest(); //实例化测试类
CloseableHttpResponse closeableHttpResponse = httpTest.DoGet("http://localhost:8080/home"); //调用get方法,传入url
HttpEntity entity = closeableHttpResponse.getEntity(); //获取实体类
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
System.out.println("内容为-----" + br.readLine());
}
http接收端:
@RequestMapping(value = "/home")
public JSONObject home() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("wq", 30);
jsonObject.put("fxw", 23);
return jsonObject;
}
结果为:
使用get有参请求方式
private CloseableHttpClient closeableHttpClient = null;
private CloseableHttpResponse closeableHttpResponse = null;
private RequestConfig config = RequestConfig.custom()
.setConnectTimeout(300)
.setConnectionRequestTimeout(300)
.setSocketTimeout(300)
.setRedirectsEnabled(true).build();
private String scheme = "http";
/**
* get有参,使用NameValuePair传入参数
*
* @param host
* @param port
* @param path
* @param json
* @return
* @throws IOException
* @throws URISyntaxException
*/
public CloseableHttpResponse DoGet(String host, int port, String path, List<NameValuePair> json) throws IOException, URISyntaxException {
//初始化url
URI uri = new URIBuilder()
.setScheme(scheme)
.setHost(host)
.setPort(port)
.setPath(path)
.setParameters(json)
.build();
HttpGet httpGet = new HttpGet(uri);
httpGet.setConfig(config);
closeableHttpResponse = closeableHttpClient.execute(httpGet);
return closeableHttpResponse;
}
//主方法
public static void main(String[] args) throws IOException, URISyntaxException {
HttpTest httpTest = new HttpTest();
String host = "localhost";
int port = 8080;
String path = "home";
List<NameValuePair> list = new ArrayList<>();
list.add(new BasicNameValuePair("wq", "你好!"));
CloseableHttpResponse closeableHttpResponse = httpTest.DoGet(host, port, path, list);
HttpEntity entity = closeableHttpResponse.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
System.out.println("内容为-----" + br.readLine());
}
接收端代码为:
@RequestMapping(value = "/home", headers = "Accept=application/json")
public JSONObject home(@RequestParam(value = "wq") String wq) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("wq", 30);
return jsonObject;
}
打印出的结果为:
使用Post无参方式请求
private CloseableHttpClient closeableHttpClient = null;
private CloseableHttpResponse closeableHttpResponse = null;
/**
* 无参的post请求
*
* @param url
* @return
* @throws IOException
*/
public CloseableHttpResponse DoPost(String url) throws IOException {
HttpPost httpPost = new HttpPost(url);
closeableHttpResponse = closeableHttpClient.execute(httpPost);
return closeableHttpResponse;
}
//主方法
public static void main(String[] args) throws IOException, URISyntaxException {
HttpTest httpTest = new HttpTest();
CloseableHttpResponse closeableHttpResponse = httpTest.DoPost("http://localhost:8080/home");
HttpEntity entity = closeableHttpResponse.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
System.out.println("内容为-----" + br.readLine());
}
接收客户端为:
@RequestMapping(value = "/home", headers = "Accept=application/json")
public JSONObject home() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("wq", 30);
return jsonObject;
}
运行结果为:
使用Post有参方式
private CloseableHttpClient closeableHttpClient = null;
private CloseableHttpResponse closeableHttpResponse = null;
//设置请求配置
private RequestConfig config = RequestConfig.custom()
.setConnectTimeout(300)
.setConnectionRequestTimeout(300)
.setSocketTimeout(300)
.setRedirectsEnabled(true).build();
//请求头
private String scheme = "http";
// 请求类型
private static final String CONTENT_TYPE_APPLICATION_JSON = "application/json";
//创建请求
closeableHttpClient = HttpClientBuilder.create().build();
/**
* post 请求有参
*
* @param host
* @param port
* @param path
* @param jsons
* @return
* @throws IOException
* @throws URISyntaxException
*/
public CloseableHttpResponse DoPost(String host, int port, String path, List<NameValuePair> jsons) throws IOException, URISyntaxException {
URI uri = new URIBuilder().setScheme(scheme).setHost(host).setPort(port).setPath(path).setParameters(jsons).build();
HttpPost httpPost = new HttpPost(uri);
//配置请求时间
httpPost.setConfig(config);
//请求头
httpPost.addHeader(HTTP.CONTENT_TYPE, CONTENT_TYPE_APPLICATION_JSON);
//执行请求
closeableHttpResponse = closeableHttpClient.execute(httpPost);
return closeableHttpResponse;
}
//主方法
public static void main(String[] args) throws IOException, URISyntaxException {
HttpTest httpTest = new HttpTest();
String host = "localhost";
int port = 8080;
String path = "home";
List<NameValuePair> list = new ArrayList<>();
list.add(new BasicNameValuePair("wq", "30"));
CloseableHttpResponse closeableHttpResponse = httpTest.DoPost(host, port, path, list);
HttpEntity entity = closeableHttpResponse.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
System.out.println("内容为-----" + br.readLine());
}
接收客户端为:
@RequestMapping(value = "/home")
public JSONObject home(@RequestParam(value = "wq") String wq) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("wq", 100);
return jsonObject;
}
运行结果为: