本文主要是介绍用httpclient模拟登陆网站注意事项,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近一直在用httpclient进行模拟登陆网站,即先用httpwatch记录手动登陆时浏览器与服务器之间的数据交互,然后用httpclient模拟浏览器的行为进行登陆。
但是一开始进行模拟时总是登陆不成功,即使发送的内容与httpwatch中记录的内容一致时,也是返回不了正确的结果,经过网上搜索和尝试,发现是发送请求时,请求头的信息不够完整。同样的请求体,如果把请求头的信息尽量完善,就能够成功登陆,否则则是登陆失败。
其次还要注意第一次向服务器请求时,服务器一般都会返回一个“Set-Cookie”字段,把这个字段中的内容(如sessionid)记录下来,添加到cookie中,再次向服务器发请求时带上cookie,这样就能有很大概率登陆成功了。
下面是一个模拟登陆的代码,其中包括对于验证码的处理
public static void main(String[] args) throws HttpException, IOException {HttpClient client = new HttpClient();String url = "http://www.xxxxx.com/user/login.html";GetMethod getMethod = new GetMethod(url);getMethod.setRequestHeader("Host", "www.xxxxxx.com"); getMethod.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; NP06; .NET4.0C; .NET4.0E; InfoPath.3; Media Center PC 6.0)"); getMethod.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); getMethod.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5"); getMethod.setRequestHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"); getMethod.setRequestHeader("Connection", "keep-alive"); getMethod.setRequestHeader("Cache-Control", "max-age=0");client.executeMethod(getMethod);Header header = getMethod.getResponseHeader("Set-cookie");//记录服务器返回的cookie值 System.out.println(header.getValue()); String headerCookie = header.getValue(); String SessionId = headerCookie.substring(headerCookie.indexOf("ASP.NET_SessionId=") + "ASP.NET_SessionId=".length(), headerCookie.indexOf(";")); System.out.println(SessionId); StringBuffer sb = new StringBuffer();sb.append("Hm_lvt_ebd37b04edd1e0fceaa52dc641b8ff6f=1412760752,1412820282; sgsa_id=anxin.com|1412760752435322; _ga=GA1.2.1171830066.1412760753; cuuid=E407DF6BFFBF4599; _gat=1; Hm_lpvt_ebd37b04edd1e0fceaa52dc641b8ff6f=1412820282; sgsa_vt_187935_192889=1412820281676; CNZZDATA1252986781=736053870-1412760776-%7C1412820303; dntadmin=;");sb.append("ASP.NET_SessionId=").append(SessionId).append("; ");String cookie = sb.toString();System.out.println(cookie);getMethod = new GetMethod("http://www.xxxxxx.com/vcode.aspx?xxxx=xxxxx&x=x");//获取验证码client.executeMethod(getMethod);InputStream is = getMethod.getResponseBodyAsStream();File file = new File("D:/image.png");//图片格式根据具体情况而定(有些网站在获取验证码时也要发送带cookie的请求头,要注意具体情况具体分析)FileOutputStream fos = new FileOutputStream(file);byte b[] = new byte[1024];int j = 0;while((j=is.read(b))!=-1){fos.write(b, 0, j);}fos.flush();fos.close();String key = "";System.out.println("请输入验证码");Scanner scanner = new Scanner(System.in);key = scanner.nextLine();client.getHostConfiguration().setHost("www.xxxxxx.com", 80, "http"); PostMethod method = getPostMethod(key); method.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; NP06; .NET4.0C; .NET4.0E; InfoPath.3; Media Center PC 6.0)"); method.setRequestHeader("Host", "www.xxxxxx.com"); method.setRequestHeader("Accept", "application/json, text/javascript, */*"); method.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");
// method.setRequestHeader("Accept-Encoding", "gzip, deflate"); method.setRequestHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"); method.setRequestHeader("Connection", "keep-alive"); method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); method.setRequestHeader("X-Requested-With", "XMLHttpRequest"); method.setRequestHeader("Referer", "http://www.xxxxxx.com/user/login.html"); method.setRequestHeader("Cookie", cookie); client.executeMethod(method); System.out.println(method.getStatusCode()); System.out.println(method.getResponseBodyAsString());Header header1 = method.getResponseHeader("Set-cookie"); sb.append(header1.getValue()); Cookie[] cookies = client.getState().getCookies(); method.releaseConnection(); String my = "http://www.xxxxx.com/usercenter"; System.out.println(sb.toString()); getMethod = new GetMethod(my); getMethod.setRequestHeader("Host", "www.xxxxx.com"); getMethod.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; NP06; .NET4.0C; .NET4.0E; InfoPath.3; Media Center PC 6.0)"); getMethod.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); getMethod.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5"); getMethod.setRequestHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"); getMethod.setRequestHeader("Connection", "keep-alive"); getMethod.setRequestHeader("Referer", "http://www.xxxxx.com/user/login.html"); getMethod.setRequestHeader("Cookie", sb.toString()); int code = client.executeMethod(getMethod); System.out.println(getMethod.getStatusCode()); System.out.println(getMethod.getResponseBodyAsString()); }private static PostMethod getPostMethod(String ValidCode) { PostMethod post = new PostMethod("/ajax/xxxx.xxxx"); NameValuePair[] simcard = { new NameValuePair("Action", "post"), new NameValuePair("Keep", "0"), new NameValuePair("sPassword", "xxxxx"), new NameValuePair("sUserName", "xxxxx"), new NameValuePair("sVerCode", ValidCode), }; post.setRequestBody(simcard); return post; }
这篇关于用httpclient模拟登陆网站注意事项的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!