public static String sendPost(String url, Mapparams,
String charset) {
StringBuffer response= newStringBuffer();
HttpClient client= newHttpClient();
HttpMethod method= newPostMethod(url);
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.setRequestHeader("Cookie", "special-cookie=value");
method.setRequestHeader("", "");//设置Http Post数据
if (params != null) {
HttpMethodParams p= newHttpMethodParams();for (Map.Entryentry : params.entrySet()) {
p.setParameter(entry.getKey(), entry.getValue());
}
method.setParams(p);
}try{
client.executeMethod(method);if (method.getStatusCode() ==HttpStatus.SC_OK) {
BufferedReader reader= newBufferedReader(newInputStreamReader(method.getResponseBodyAsStream(),
charset));
String line;while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
}
}catch(IOException e) {
log.error("执行HTTP Post请求" + url + "时,发生异常!", e);
}finally{
method.releaseConnection();
}returnresponse.toString();
}
2,模拟登录微信公众平台
既然我们可以模拟post请求,那么理论上我们也可以模拟登录,一般来说登录的过程就是发起一个post请求,把账号密码发送给服务器,服务器验证通过后,在resopnse里写cookies,然后跳转页面,接下来和服务器的交互就要基于cookies了。
那么我们要做的就是发起一个请求,然后把成功返回的cookies保存起来,每次后续需要登录状态才能和服务器交互的操作都带上cookies 。关于cookies
其实就是这么个过程:
client ---post请求---> server
client
client -请求带cookies--server
首先,登录的请求是这样的:
我们还可以注意到header中有一个Referer, 也是很有意思的,它的意思是这个请求来自于哪里。这个可以做统计啊,限制啊什么的。虽然我们可以模拟,哈哈。但是很多正规的网站都会用这个东西。有兴趣可以了解下~
代码类似如下:
public final static String REFERER_H = "Referer";privateLoginResult _login(String username, String pwd) {
LoginResult loginResult= newLoginResult();try{
PostMethod post= newPostMethod(LOGIN_URL);
post.setRequestHeader("Referer", "https://mp.weixin.qq.com/");
post.setRequestHeader(USER_AGENT_H, USER_AGENT);
NameValuePair[] params= newNameValuePair[] {new NameValuePair("username", username),new NameValuePair("pwd", DigestUtils.md5Hex(pwd
.getBytes())),new NameValuePair("f", "json"),new NameValuePair("imagecode", "") };
post.setQueryString(params);int status =client.executeMethod(post);if (status ==HttpStatus.SC_OK) {
String ret=post.getResponseBodyAsString();
LoginJson retcode= JSON.parseObject(ret, LoginJson.class);//System.out.println(retcode.getRet());
if ((retcode.getBase_resp().getRet() == 302 ||retcode
.getBase_resp().getRet()== 0)) {
loginResult.setCookie(client.getState().getCookies());
StringBuffer cookie= newStringBuffer();//处理cookies
for(Cookie c : client.getState().getCookies()) {
cookie.append(c.getName()).append("=")
.append(c.getValue()).append(";");
cookiemap.put(c.getName(), c.getValue());
}
loginResult.setCookiestr(cookie.toString());
loginResult.setToken(getToken(retcode.getRedirect_url()));
loginResult.setLogin(true);returnloginResult;
}else{
loginResult.setLogin(false);returnloginResult;
}
}
}catch(Exception e) {
String info= "【登录失败】【发生异常:" + e.getMessage() + "】";
System.err.println(info);
log.debug(info);
log.info(info);returnloginResult;
}returnloginResult;
}
全部代码已经共享出来了:github 希望能帮到你~
3,重定向问题
面试中是不是曾经也被问到过:forword和redirect的区别。
网上的回答也有很多了,回过头来再看看这个问题。其实forword,可以说是服务器发起一个请求,访问自身应用下的资源。因为是服务器自己发起的,所以先前由客户端传递过来的参数就可以保持住了,而且是把访问的这个内部资源返回的内容作为客户端最初请求的响应,所以浏览器的url不会变。
那么redirect的原理:其实是服务端在接到客户端请求后,在响应中的header中加上了一个location的东西,这个东西的原理又是:
当浏览器接受到头信息中的 Location: xxxx 后,就会自动跳转到 xxxx 指向的URL地址,这点有点类似用 js 写跳转。但是这个跳转只有浏览器知道,不管体内容里有没有东西,用户都看不到。例:header("Location: http://www.xker.com/");也就是说redirect他的实现也依赖了浏览器的机制配合的。
那么这样就明白了,先回一个带有location的响应,然后浏览器自己发起一个新的向location下的值的请求。因为是浏览器自己发起的请求,所以第一次发起的请求中的参数就不能保持,而因为是一个新的请求,所以浏览器的url会改变。
还可以知道,在带有location的响应中,HTTP STATUS CODE 还是302。
这也许是更深一点的理解吧~
让我们继续前行
----------------------------------------------------------------------
努力不一定成功,但不努力肯定不会成功。