我正在尝试使用HttpClient登录后浏览网站。
我首先定义一个HttpClient实例以及一个cookie存储:
public HttpClient httpclient = new DefaultHttpClient();
public CookieStore cookieStore = new BasicCookieStore();
public HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
在一个(android)BackgroundTask中,我成功登录网站。
HttpPost httppost = new HttpPost("http://www.deeproute.com/deeproute/default.asp");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("cookieexists","false"));
nameValuePairs.add(new BasicNameValuePair("name", mUser));
nameValuePairs.add(new BasicNameValuePair("password", mPassword));
nameValuePairs.add(new BasicNameValuePair("subbera", "Login"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
res = httpclient.execute(httppost, localContext);
在此之后,在另一个BackgroundTask中,我尝试连接到同一网站上的不同页面:
HttpGet rosterGet = new HttpGet("http://deeproute.com/deeproute/?sel=rosterlook&myleagueno=6&myteamno=12");
res = httpclient.execute(rosterGet, localContext);
然而,当我连接到此页面时,我不再登录了。我查看并且在成功登录后仍然存在cookie,所以我不知所措。
编辑:对于记录,此网站通常在普通浏览器中正常运行。
编辑2:为了回应下面Aaron的回答,似乎Cookie正在保存,因为如果在第一次请求之后,我会这样做:
List<Cookie> cookies = cookieStore.getCookies();
int cookieSize = cookies.size();
for (int i = 0; i < cookieSize; i++) {
Log.v(TAG, "Cookie " + i + "name: "
+ cookies.get(i).getName());
Log.v(TAG, "Cookie " + i + "value: "
+ cookies.get(i).getValue());
}
我找回了四个cookie,包括一个存储了我的用户名的cookie。如果我在第二次请求后做了同样的事情,我实际上会找回6个cookie,因为它显然给了我一个新的会话ID。这似乎是问题的根源,但我不确定如何解决它。
第一次请求后,这是我的会话ID cookie:
name: ASPSESSIONIDSCSCSBCS
value: GBAJALJBOGKBFLAELPNKEDOE
在第二次请求之后,我有两个会话ID cookie:
name: ASPSESSIONIDSCSCSBCS
value: GBAJALJBOGKBFLAELPNKEDOE
name: ASPSESSIONIDSCSCSBCS
value: MBAJALJBDBOKPEHNCDDFOCBC
答案 0 :(得分:0)
我遇到了同样的问题。我实现了cookie,但仍然没有登录。我终于让它工作了,所以我发布了这段代码,希望能帮助其他人解决同样的问题。
以下是Apache关于如何登录和检索所有Cookie的示例:https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientFormLogin.java
您还可以查看Apache的HttpClient文档,了解&#34; HTTP状态管理&#34;在这里:https://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html
以下是我的登录功能的实现方式:
CredentialsProvider
BasicCookieStore
CloseableHttpClient
/login.html
登录。/user
执行GET请求以查看我们是否正确登录打印回复状态代码
//////// SETUP
//Setup Proxy (If needed)
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("proxy.mycompany", 8080),
new UsernamePasswordCredentials("my_username", "my_password"));
//Setup Cookies
BasicCookieStore cookieStore = new BasicCookieStore();
//Build HttpClient
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.setDefaultCookieStore(cookieStore)
.build();
HttpHost target = new HttpHost("www.mycompany.com", 80, "http");
HttpHost proxy = new HttpHost("proxy.mycompany", 8080);
RequestConfig config = RequestConfig.custom()
.setProxy(proxy)
.build();
try {
//////// LOGIN REQUEST, POST TO /login.html
//Start POST Request
HttpPost httppost = new HttpPost('/login.html');
//Add parameters to POST Request
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair('username','my_username'));
nameValuePairs.add(new BasicNameValuePair('password','my_password'));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Set Config for POST Request
httppost.setConfig(config);
//Execute POST Request
httpresponse = httpclient.execute(target, httppost);
//Print Response Code
try {
System.out.println("Status Code: "+httpresponse.getStatusLine().getStatusCode());
} finally {
//Close HTTP Reponse (must be in a 'finally' block)
httpresponse.close();
}
//////// CHECK LOGIN STATUS, GET TO /user
//Start GET Request
HttpGet httpget = new HttpGet('/user');
//Set Config for GET Request
httpget.setConfig(config);
//Execute GET Request
httpresponse = httpclient.execute(target, httpget);
//Print Response Code
try {
System.out.println("Status Code: "+httpresponse.getStatusLine().getStatusCode());
} finally {
//Close HTTP Reponse (must be in a 'finally' block)
httpresponse.close();
}
} finally {
httpclient.close();
}
答案 1 :(得分:-1)
看起来不像初始请求中保存的Cookie,因此下一个请求中没有任何内容可以告诉您登录的网站。 你需要一个cookie容器。谷歌吧。