-
大小: 6KB文件类型: .java金币: 1下载: 0 次发布日期: 2021-05-10
- 语言: Java
- 标签: httpclient get post json
资源简介
httpclient get/post请求工具类(map参数封装),方便调用
代码片段和文件信息
package com.abc.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONobject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.client.utils.URIBuilder;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by Administrator on 2017/6/16.
*/
public class HttpClientUtil {
/**
* get请求
*
* @param url 请求地址
* @param params map参数
* @return json
*/
public static JSONobject getJsonData(String url Map params) {
JSONobject jsonobject = null;
// 获取当前客户端对象
// HttpClient httpClient = HttpClientUtil.createDefault();
CloseableHttpClient httpClient = HttpClients.createDefault();
List pairs = new ArrayList(params.size());
for (Map.Entry entry : params.entrySet()) {
pairs.add(new BasicNameValuePair(entry.getKey() entry.getValue()));
}
CloseableHttpResponse response = null;
try {
URIBuilder builder = new URIBuilder(url);
builder.setParameters(pairs);
// 根据地址发送get请求
HttpGet request = new HttpGet(builder.build());
// 通过请求对象获取响应对象
response = httpClient.execute(request);
// 判断网络连接状态码是否正常(0--200都数正常)
if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity “UTF-8“);
// 返回json格式:
jsonobject = JSON.parseobject(result);
}
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return jsonobject;
}
/**
* post请求
* @param url url
* @par
相关资源
- HttpClient4.5 实现https忽略SSL证书验证
- wget 完整版 for android 支持Android8.0 aa
- java实现的http服务器
- httppost和httpget需要的jar包
- 基于HttpClient 4.3的可访问自签名HTTPS站
- httpclient-4.5.5 完整架包
- Delphi XE8 IDHTTP1.get()方法從Servlet服务
- 使用Http post的方式调用webservice
- commons-io-2.4.jar
- android studio实现HttpsURLConnection post请求
- mybatis-generator+lombok自动生成mybatis代码
- HttpClient发送http请求需要的jar包
- httpClient jar包.rar
- httpclient-4.5所需jar包
- Get Programming with Go [true PDF]
- commons-httpclient-3.0.jar
- httpclient-4.5 jar
- dedecms v5.7漏洞批量getshell工具
- org.apache.commons.httpclient. jar
- GetMusic3.0正式版.apk
- VBA连接postgreasql生成javaBeam
- Android网络框架Retrofit2使用封装:Get
- android AppWidgetExample(AppWidget)源码
- android Widget 小部件开发
- 微信获取Ticket,token
- HttpPost
- 使用GET方法获取数据
- android httpclient asynctask demo
- httpcore-4.2.4.jar 和 httpclient-4.2.5.jar
- java 访问 wcf 使用的HttpClient
评论
共有 条评论