作者:真个田_707 | 来源:互联网 | 2023-05-18 20:44
之前写过一个Java工具类–通过HttpClient发送http请求最近在访问一个接口的时候被拒绝了。具体信息如下我们用程序访问我的csdn博客地址解决方案就是在httpclient发
之前写过一个Java工具类–通过HttpClient发送http请求
最近在访问一个接口的时候被拒绝了。具体信息如下
我们用程序访问我的csdn博客地址
解决方案就是在httpclient发出请求的时候在Header中设置一个User-Agent
HTTP响应头和请求头信息对照表
具体如下
String userAgent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36";
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("User-Agent",userAgent);
response = httpclient.execute(httpGet);
我电脑的谷歌浏览器User-Agent
加上User-Agent之后访问我的博客地址
完整的代码
HttpUtil.java
package com.jrbac.util;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Http工具类,发送Http请求, Get请求请将参数放在url中 Post请求请将参数放在Map中
*
* @author 程高伟
* @date 2017年1月5日 下午6:03:50
*/
public class HttpUtil {
private static final Logger log = LoggerFactory.getLogger(HttpUtil.class);
private static final CloseableHttpClient httpclient = HttpClients.createDefault();
private static final String userAgent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36";
/**
* 发送HttpGet请求
*
* @param url
* 请求地址
* @return 返回字符串
*/
public static String sendGet(String url) {
String result = null;
CloseableHttpResponse respOnse= null;
try {
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("User-Agent", userAgent);
respOnse= httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
} catch (Exception e) {
log.error("处理失败 {}" + e);
e.printStackTrace();
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
return result;
}
/**
* 发送HttpPost请求,参数为map
*
* @param url
* 请求地址
* @param map
* 请求参数
* @return 返回字符串
*/
public static String sendPost(String url, Map map) {
List formparams = new ArrayList();
for (Map.Entry entry : map.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("User-Agent", userAgent);
httpPost.setEntity(formEntity);
CloseableHttpResponse respOnse= null;
String result = null;
try {
respOnse= httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
} catch (IOException e) {
log.error(e.getMessage());
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
return result;
}
/**
* 发送HttpPost请求,参数为文件,适用于微信上传素材
*
* @param url
* 请求地址
* @param file
* 上传的文件
* @return 返回字符串
* @throws IOException
* @throws ClientProtocolException
*/
public static String sendPost(String url, File file) {
String result = null;
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("User-Agent", userAgent);
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("media", new FileBody(file));
httpPost.setEntity(multipartEntity.build());
CloseableHttpResponse respOnse= null;
try {
respOnse= httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
} catch (IOException e) {
log.error(e.getMessage());
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
return result;
}
/**
* 发送HttpPost请求,参数为json字符串
*
* @param url
* @param jsonStr
* @return
*/
public static String sendPost(String url, String jsonStr) {
String result = null;
StringEntity entity = new StringEntity(jsonStr, Consts.UTF_8);
entity.setContentType("application/json");
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("User-Agent", userAgent);
httpPost.setEntity(entity);
CloseableHttpResponse respOnse= null;
try {
respOnse= httpclient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
result = EntityUtils.toString(httpEntity);
} catch (IOException e) {
log.error(e.getMessage());
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
return result;
}
/**
* 发送不带参数的HttpPost请求
*
* @param url
* @return
*/
public static String sendPost(String url) {
String result = null;
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("User-Agent", userAgent);
CloseableHttpResponse respOnse= null;
try {
respOnse= httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
} catch (IOException e) {
log.error(e.getMessage());
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
return result;
}
}
参考文献
HTTP响应头和请求头信息对照表