java 发起get请求_java发起http请求

引入工具依赖包如下:

commons-httpclient

commons-httpclient

3.1

org.apache.httpcomponents

httpclient

4.3.6

方法如下所示:

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.methods.PostMethod;

import org.apache.commons.httpclient.params.HttpMethodParams;

import org.apache.commons.lang.ArrayUtils;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.HttpStatus;

import org.apache.commons.httpclient.NameValuePair;

import org.apache.http.client.config.RequestConfig;

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.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import java.io.IOException;

import java.util.*;

@Slf4j

public class HttpClientTest {

/**

* 发起post请求 没有任何body参数的情况

*

* @param url 请求的目标url地址

* @param data 请求数据

* @return 将响应结果转换成string返回

* @throws IOException 可能出现的异常

*/

private static String postMsg(String url, String data) throws IOException {

// 根据url地址发起post请求

HttpPost httppost = new HttpPost(url);

StringEntity stEntity;

// 获取到httpclient客户端

CloseableHttpClient httpclient = HttpClients.createDefault();

try {

// 设置请求的一些头部信息

httppost.addHeader("Content-Type", "application/json");

httppost.addHeader("procode", "test");

stEntity = new StringEntity(data, "UTF-8");

httppost.setEntity(stEntity);

// 设置请求的一些配置设置,主要设置请求超时,连接超时等参数

RequestConfig requestConfig = RequestConfig.custom()

.setConnectTimeout(5000).setConnectionRequestTimeout(1000).setSocketTimeout(5000)

.build();

httppost.setConfig(requestConfig);

// 执行请求

CloseableHttpResponse response = httpclient.execute(httppost);

// 请求结果

String resultString = "";

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

log.info("请求状态:{}", response.getStatusLine().getStatusCode());

// 获取请求响应结果

HttpEntity entity = response.getEntity();

if (entity != null) {

// 将响应内容转换为指定编码的字符串

resultString = EntityUtils.toString(entity, "UTF-8");

log.info("Response content:{}", resultString);

return resultString;

}

} else {

log.info("请求失败!");

return resultString;

}

} catch (Exception e) {

throw e;

} finally {

httpclient.close();

}

return null;

}

/**

* body有内容的post请求方法 controller可以用bean直接接收参数

*

* @param url 目标地址

* @param params 封装的参数

* @param codePage 字符编码

* @return 将响应结果转换成string返回

* @throws Exception 可能出现的异常

*/

private synchronized static String postData(String url, Map params, String codePage) throws Exception {

HttpClient httpClient = new HttpClient();

// 请求相关超时时间设置

httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10 * 1000);

httpClient.getHttpConnectionManager().getParams().setSoTimeout(10 * 1000);

PostMethod method = new PostMethod(url);

if (params != null) {

// 内容编码设置

method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, codePage);

// 请求体信息设置

method.setRequestBody(assembleRequestParams(params));

// 请求头设置

method.setRequestHeader("procode", "test");

}

// 响应结果信息处理

String result = "";

try {

httpClient.executeMethod(method);

result = new String(method.getResponseBody(), codePage);

} catch (Exception e) {

throw e;

} finally {

// 释放连接

method.releaseConnection();

}

return result;

}

/**

* 组装http请求参数

*

* @param data 键值对

* @return 返回一个名称-值的键值对

*/

private synchronized static NameValuePair[] assembleRequestParams(Map data) {

List nameValueList = new ArrayList<>();

Iterator> it = data.entrySet().iterator();

while (it.hasNext()) {

Map.Entry entry = it.next();

nameValueList.add(new NameValuePair(entry.getKey(), entry.getValue()));

}

log.info("键值对参数:{}", ArrayUtils.toString(nameValueList.toArray(new NameValuePair[nameValueList.size()])));

return nameValueList.toArray(new NameValuePair[nameValueList.size()]);

}

/**

* get请求

*

* @param url 目标请求地址

* @return 将响应结果转换成string返回

*/

private static String get(String url) {

String result = "";

try {

// 根据地址获取请求

HttpGet request = new HttpGet(url);

request.setHeader("procode", "test");

// 获取当前客户端对

CloseableHttpClient httpclient = HttpClients.createDefault();

// 通过请求对象获取响应对象

HttpResponse response = httpclient.execute(request);

// 判断请求结果状态码

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

result = EntityUtils.toString(response.getEntity());

}

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

public static void main(String[] args) throws Exception {

String url = "http://localhost:8080/sales-webapp/admin/report/smslist";

// 发起post请求

String resultPost = postMsg(url, "1");

log.info("post返回结果:{}", resultPost);

// 发起get请求

String resultGet = get(url);

log.info("get返回结果:{}", resultGet);

// 测试body有参数的post方法

Map map = new HashMap<>(16);

map.put("age", "13");

map.put("name", "jason");

String resultPostHaveBody = postData(url, map, "UTF-8");

log.info("post带有body请求返回结果:{}", resultPostHaveBody);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值