2
我可以使用下面的代碼登錄到應用程序。現在,當我嘗試從應用程序訪問任何鏈接時登錄後,它會重新將我重定向到登錄頁面。如何使用httpclient登錄網站?
我在登錄後和執行get方法後得到響應頭。
響應頭:
status code: HTTP/1.1 302 Object moved
Cache-Control = private
Content-Length = 270
Content-Type = text/html
catid=%7B9B181EE2%2D1F13%2D4E31%2D9279%2D786364B46B82%7D®mode=existentmember&loginmode=loginsuccess
Set-Cookie = cartId=; path=/
Set-Cookie = loginId=%7B49C7B309%2DA3F7%2D4E2D%2DB5D6%2D39FE0AA3319E%7D%7BD73CF8E4%2DFB79%2D4D92%2DB8F9%2DE0B3DB6ABE6A%7D; expires=Thu, 28-Jul-2011 06:05:28 GMT; path=/
Set-Cookie = ASPSESSIONIDCQTABSCB=FPGEGHADKGCOODKIPILEAFPO; path=/
Date = Tue, 26 Jul 2011 06:05:28 GMT
CompareId=%7BF48141FB%2D43F1%2D4561%2D9FB5%2D1E297799B6AE%7D%7B246DE50A%2D618B%2D4E5E%2DBC3A%2D0D9D5C318FAA%7D; expires=Fri, 20-Jul-2012 16:53:22 GMT; path=/
Set-Cookie = ASPSESSIONIDASTAASDB=KDPFOLADKCNPCJGNLDPMBKNF; path=/
Date = Wed, 20 Jul 2011 16:53:22 GMT
下面是代碼
HttpClient httpclient = new HttpClient();
httpclient.getHostConfiguration().setHost(host);
httpclient.getParams().setParameter("http.connection.timeout",
new Integer(20000));
httpclient.getParams().setParameter("http.socket.timeout",
new Integer(20000));
PostMethod postMethod = new PostMethod(actionUrl);
// Prepare login parameters
NameValuePair[] data = {
new NameValuePair("MemberLoginName","username"),
new NameValuePair("MemberLoginPwd","password")
};
postMethod.setRequestBody(data);
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));*/
BufferedReader reader = null;
// Execute the method.
int statusCode = httpclient.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ postMethod.getStatusLine());
strHtmlContent = null;
} else {
System.out.println("status code: "
+ postMethod.getStatusLine());
GetMethod method = new GetMethod(nextUrl);
請建議我改變了上面的代碼需要。
感謝