模拟一个网站登录,获得的验证码和其相应的cookie已经完成,但是等提交的时候总是验证码错误
static HttpClient httpClient_lab = new DefaultHttpClient();
//获取验证码和cookie
public static byte[] getImgAndcookiesByUrl(String url)
{
byte[] data=null;
String html = null;
HttpGet httpget = new HttpGet(url);//以get方式请求该URL
try
{
HttpResponse response = httpClient_lab.execute(httpget);//得到responce对象
int resStatu = response.getStatusLine().getStatusCode();//返回码
if (resStatu==HttpStatus.SC_OK)
{
HttpEntity entity = response.getEntity();
InputStream inStream=entity.getContent();
//System.out.println(inStream.toString());
data = readInputStream(inStream);
printHexString(data);
System.out.println(String.valueOf(data.length));
inStream.close();
}
}
catch (Exception e)
{
System.out.println("访问【"+url+"】出现异常!");
System.out.println(e.toString());
e.printStackTrace();
}
try
{
List<Cookie> cookies = ((AbstractHttpClient)httpClient_lab).getCookieStore().getCookies();
if (cookies.isEmpty())
{
System.out.println("None");
}
else
{
for (int i = 0; i < cookies.size(); i++)
{
html=cookies.get(i).toString();
System.out.println(html);
String[] str_cookie=html.split(":");
html=str_cookie[3].substring(1,27);
FileOperate.write(html);
System.out.println(html);
}
}
}
catch (Exception e)
{
System.out.println("获取cookie出现异常!");
System.out.println(e.toString());
e.printStackTrace();
}
return data;
}
//提交登录信息
public static String sendRequestFromHttpClient(Map<String, String> params,String cookies) throws Exception
{
Log.i("Set-Cookie 0000", cookies);
String html="";
//需要把参数放到NameValuePair
List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
if(params!=null && !params.isEmpty())
{
for(Map.Entry<String, String> entry : params.entrySet())
{
paramPairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity(paramPairs, HTTP.UTF_8);
HttpPost post = new HttpPost("http://202.200.151.19/reader/redr_verify.php");
post.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, false);
post.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
post.addHeader("Accept-Encoding","gzip,deflate,sdch");
post.addHeader("Accept-Language","zh-CN,zh;q=0.8");
post.addHeader("Cache-Control","max-age=0");
post.addHeader("Connection","keep-alive");
//post.addHeader("Content-Length","69");
post.addHeader("Content-Type","application/x-www-form-urlencoded");
post.addHeader("Cookie","PHPSESSID="+cookies);
post.addHeader("Host","202.200.151.19");
post.addHeader("Origin","http://202.200.151.19");
post.addHeader("Referer","http://202.200.151.19/reader/login.php");
post.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36");
post.setEntity(entitydata); //设置用户信息
//执行post请求
//HttpClient httpClient = CustomerHttpClient.getHttpClient();
HttpResponse response = httpClient_lab.execute(post);
//从状态行中获取状态码,判断响应码是否符合要求
log_cookie = response.getFirstHeader("Set-Cookie").getValue();
log_cookie=log_cookie.substring(10, 36);
Log.i("Set-Cookie 9999", log_cookie);
Log.i("Set-Cookie code",String.valueOf(response.getStatusLine().getStatusCode()));
if(response.getStatusLine().getStatusCode()==200)
{
HttpEntity entity = response.getEntity();
if (entity!=null)
{
html = EntityUtils.toString(entity);//获得html源代码
}
}
return html;
}
访问的网站是http://202.200.151.19/reader/login.php
我觉得每次我用httpclient获到的PHPSESSID是变化的,但实际访问时不管怎么刷新或者提交,不管信息对不对PHPSESSID的值都不变
你要是用到了多线程的话可能就会出错了
为什么?怎么加呢?
@黑黑的世界: 要是用到了多线程的话,你的每次请求到的验证码在提交的这个过程中,可能另一个线程又去请求了一次,这样就照成了验证码改变了,
解决方法是不用多线程。
@博客¥: 我没加多线程,还以为你说加呢,我觉得这个没多线程吧,本来我的httpclient还加了单例呢
是不是不同请求返回的验证图片不同?我最也有这方面的问题。
对,我已解决了,太烦了
@黑黑的世界: 怎么解决的能把方案简单描述下吗?
httpclient4.0会自动记住cookie,所以不需要自己再进行获取,不然反而会出错
请问,是怎么解解的呢?
httpclient4.0是自己维护cookie信息的,你就不要自己再去获取然后再添加了,只要保证你多次访问用的是同样一个httpclient对象就可以了,不知道你是想问这个不
你的代码确实也只用的一个httpclient对象啊,直接把获取cookie和添加cookie去掉请问就可以了吗