import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
* @Auther:
* @Date: 2020/10/10 18:43
* @Description:调用http请求的post、get
*/
public class httpRequest {
/*
* get请求
* */
public static void getRequest(String url, Map<String, String> headers, Map<String, Object> params) {
StringBuffer uris = new StringBuffer();
CloseableHttpClient httpClient = HttpClients.createDefault();
//遍历传参
if (params.size() > 0) {
Iterator<Map.Entry<String, Object>> itparams = params.entrySet().iterator();
while (itparams.hasNext()) {
Map.Entry<String, Object> entry = itparams.next();
String property = entry.getKey();
String value = entry.getValue().toString();
uris.append(property + "=" + value + "&");//拼接url 请求参数
}
}
uris.delete(uris.lastIndexOf("&"), uris.length());//删除最后一个&字符
//调get请求
HttpGet httpGet = new HttpGet(url + "?" + uris);
//遍历请求头,设置
Iterator<Map.Entry<String, String>> it = headers.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
String property = entry.getKey();
String value = entry.getValue();
httpGet.addHeader(property, value); //添加请求头
}
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();//获得响应的实体对象
String result = EntityUtils.toString(httpEntity, "utf-8");
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* post请求
* */
public static void postRequest(String url, Map<String, String> headers, List<NameValuePair> params) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
//遍历请求头,设置
Iterator<Map.Entry<String, String>> it = headers.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
String property = entry.getKey();
String value = entry.getValue();
httpPost.setHeader(property, value); //添加请求头
}
//遍历传参
if (params.size() > 0) {
try {
httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
//调post请求
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();//获得响应的实体对象
String result = EntityUtils.toString(httpEntity, "utf-8");
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
}
java 使用httpclient进行post/get请求
最新推荐文章于 2024-08-06 17:13:24 发布