日期:2014-05-17  浏览次数:20818 次

httpclient 4.1.3 提交https登录表单失败
先描述一下背景,我想解析一个https网页的内容,但是这个地址是需要登录的,如果我在浏览器直接打开这个地址,它会出现一个登录页面,输入用户名密码后会自动重定向到我最开始输入的地址。
现在我想用httpclient来实现自动登录然后得到网页内容并解析,现在的问题是我的登录好像失败了(手工登录是可以的),因此无法得到网页内容。
我的主要程序如下,问题在authenticate()里面,httpsClient.execute(httppost)这句是执行成功的,但是返回的页面内容仍然是登录的页面,如果是手工在浏览器上输入用户名密码则不会这样,哪位大侠帮忙看看哪的问题?
private boolean authenticate() throws Exception
{
boolean ret = false;

HttpPost httppost = new HttpPost(authurl);
List <NameValuePair> input = new ArrayList <NameValuePair>();
input.add(new BasicNameValuePair("oper", "auth"));
input.add(new BasicNameValuePair("userid", username));
input.add(new BasicNameValuePair("password", password));
input.add(new BasicNameValuePair("changePassword", "C"));
input.add(new BasicNameValuePair("timezone", "55"));
input.add(new BasicNameValuePair("daylightSavings", "D"));
input.add(new BasicNameValuePair("useEuropeanNodes", "E"));
input.add(new BasicNameValuePair("redirectURL", url));
input.add(new BasicNameValuePair("redirectToLogon", "YES"));
httppost.setEntity(new UrlEncodedFormEntity(input, HTTP.UTF_8));
HttpResponse response = httpsClient.execute(httppost);
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
System.out.println("OK");
ret = true;
}
else
{
System.out.println("FAIL:"+response.getStatusLine().getStatusCode());
}

EntityUtils.consume(response.getEntity());
return ret;
}


private String getHTML()
{
String html = null;
try {

if (authenticate())
{
HttpResponse response = httpsClient.execute(httpget);
if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
return null;
HttpEntity entity = response.getEntity();
html = EntityUtils.toString(entity);

EntityUtils.consume(entity);
//System.out.println(html);
}
return html;
} catch (Exception e) {
e.printStackTrace();
return null;
}

}

------解决方案--------------------
会不会因为是 https的原因

还有你是否将httpclient模拟成浏览器了 
------解决方案--------------------
引用:
Quote: 引用:

会不会因为是 https的原因

还有你是否将httpclient模拟成浏览器了 


我就是用https的方式来的,你说“是否将httpclient模拟成浏览器了”是什么意思啊?
HTTP 头信息封装一下: