How does one login to website using httpclient?
I could able to login to application by using below code. Now after login whenever I try to access any link from the application, it is redirecting me to login page again.
I got below response header after login and after executing get method.
Response header:
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
Below is the code
HttpClient httpclient = new HttpClient();
httpclient.getHostConfiguration().setHost(host);
httpclient.getPa开发者_运维问答rams().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);
Please suggest me changes required in above code.
Thanks
Make your program identify https url and use some thign like following. You can remove keystore and keypass if you are only using http.
/**
* Initialize the HTTP/S connection (if needed)
*
* @param keystoreFile the full path of the keystore file
* @param keystorePass the password for the keystore file
*/
private void initHttps(String keystoreFile, String keystorePass)
{
// check if the URL uses HTTP/S
if (url.toLowerCase().startsWith(HTTPS_PROTOCOL))
{
print("Initializing HTTP/S protocol...");
// set the system properties needed for HTTP/S
System.setProperty("javax.net.ssl.keyStore", keystoreFile);
System.setProperty("javax.net.ssl.keyStorePassword", keystorePass);
System.setProperty("javax.net.ssl.keyStoreType", "JKS");
System.setProperty("javax.net.ssl.trustStore", keystoreFile);
System.setProperty("javax.net.ssl.trustStorePassword", keystorePass);
System.setProperty("javax.protocol.handler.pkgs",
"com.sun.net.ssl.internal.www.protocol");
//int addProvider = Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier()
{ // fix a HTTP/S handshake issue
public boolean verify(String hostName, SSLSession session)
{ // Verify that the host name is acceptable
return true;
}
});
}
}
You may need to set a couple of additional flags:
httpclient.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
httpclient.getParams().setParameter("http.protocol.single-cookie-header", Boolean.TRUE);
精彩评论