我是靠谱客的博主 耍酷乌冬面,最近开发中收集的这篇文章主要介绍HttpClient远程调用接口HttpClient远程调用接口,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

HttpClient远程调用接口

引入pom依赖


<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* @author liudean
* @date 2021/12/8 17:17
*/
public class HttpTest {
//设置连接超时时间
static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(500 * 1000).setConnectTimeout(500 * 1000).build();
public static void main(String[] args) throws Exception {
JSONObject params = new JSONObject();
params.put("data","这是类似于@RequestParam注解形式的参数");
params.put("apiKey","这是类似于@RequestParam注解形式的参数");
String result = callServiceByParam("http://192.168.3.254:8040/api/testParams", params);
JSONObject body = new JSONObject();
body.put("data","这是类似于@RequestBody注解形式的参数");
body.put("apiKey","这是类似于@RequestBody注解形式的参数");
String result2 = callServiceByJson("http://192.168.3.254:8040/api/testJson", body);
}
/**
* 远程调用服务接口
* 发送的是param形式的请求
*
* @param url url地址
* @return
* @JSONObject jsonParam 参数
*/
public static String callServiceByParam(String url, JSONObject jsonParam) throws IOException {
String body = "";
CloseableHttpClient hc = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
post.setConfig(requestConfig);
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
//接口需要的参数
System.out.println("请求地址:" + url);
Iterator iter = jsonParam.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
params.add(new BasicNameValuePair(entry.getKey().toString(), entry.getValue().toString()));
}
HttpEntity entity = null;
entity = new UrlEncodedFormEntity(params, "UTF-8");
post.setEntity(entity);
HttpResponse resp = null;
//得到了服务端响应的数据
resp = hc.execute(post);
//接口返回的值
String resultData = EntityUtils.toString(resp.getEntity(), "UTF-8");
return resultData;
}
/**
* 远程调用服务接口
* 发送的是json格式的请求
*
* @param url url地址
* @return
* @JSONObject jsonParam 参数
*/
public static String callServiceByJson(String url, JSONObject json) throws IOException {
String body = "";
//创建httpclient对象
CloseableHttpClient client = HttpClients.createDefault();
//创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
//装填参数
StringEntity s = new StringEntity(json.toString(), "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
//设置参数到请求对象中
httpPost.setEntity(s);
System.out.println("请求地址:" + url);
//设置header信息
//指定报文头【Content-type】、【User-Agent】
//
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//执行请求操作,并拿到结果(同步阻塞)
CloseableHttpResponse response = client.execute(httpPost);
//获取结果实体
HttpEntity entity = response.getEntity();
if (entity != null) {
//按指定编码转换结果实体为String类型
body = EntityUtils.toString(entity, "UTF-8");
}
EntityUtils.consume(entity);
//释放链接
response.close();
return body;
}
}

最后

以上就是耍酷乌冬面为你收集整理的HttpClient远程调用接口HttpClient远程调用接口的全部内容,希望文章能够帮你解决HttpClient远程调用接口HttpClient远程调用接口所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(34)

评论列表共有 0 条评论

立即
投稿
返回
顶部