java 微信请求_java 模拟微信浏览器http请求

该博客展示了如何使用Java的HttpClient库模拟微信内置浏览器发送HTTP POST请求,通过设置User-Agent和Referer头信息,以及处理连接和传输超时,确保请求能正确达到目标服务器并获取响应。
摘要由CSDN通过智能技术生成

public static void main(String[] args) {

*/

String url="http://xxxx/park/searchParkListByCarNo";

List params = new ArrayList();

params.add(new BasicNameValuePair("centerLon", "xx"));

params.add(new BasicNameValuePair("centerLat", "xx"));

params.add(new BasicNameValuePair("carNo", "xx"));

StatusBean sb = post(url,params);

System.out.println("xxx"+sb.toString());

}

/** 超时设置 */

private static final int connectTimeOut = 3000;

private static final int socketTimeOut = 3000;

public static StatusBean> post(String url, List params) {

StatusBean> sb = null;

CloseableHttpClient httpclient = HttpClients.createDefault();

HttpPost httppost = new HttpPost(url);

//在这里我们给Post请求的头部加上User-Agent来伪装成微信内置浏览器

httppost.setHeader("User-Agent","Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/4.5.255");

//这个是在网上看到的,要加上这个,避免其他错误

httppost.setHeader("Referer", "https://mp.weixin.qq.com");

CloseableHttpResponse response = null;

RequestConfig config;// 设置连接超时时间 设置数据传输超时时间

config= RequestConfig.custom().setConnectTimeout(connectTimeOut).setSocketTimeout(socketTimeOut).build();

httppost.setConfig(config);

HttpEntity reqEntity = null;

try {

if(null!=params && !params.isEmpty())reqEntity = new UrlEncodedFormEntity(params, "UTF-8");

httppost.setEntity(reqEntity);

response = httpclient.execute(httppost);

HttpEntity resEntity = response.getEntity();

if (resEntity != null) {

String entityStr = EntityUtils.toString(resEntity, "UTF-8");

System.out.println(entityStr);

sb = new StatusBean<>(true, "0000","通信成功!", null == entityStr ? "" : entityStr );

} else {

sb = new StatusBean<>(false, "9000","服务器无返回内容!", "");

}

} catch (Exception e) {

sb = new StatusBean<>(false, "9999","请求服务器异常!", e.getMessage() + "--" + e);

} finally {

try { if(null!=response) response.close(); } catch (IOException e) {}// 关闭服务器请求接收

try { if(null!=httpclient) httpclient.close(); } catch (IOException e) {} // 关闭连接

response = null;

httpclient = null;

reqEntity = null;

httppost = null;

params = null;

}

return sb;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值