To access a site which requires Login using java code, possiblyHttpClient

D

Dips

Hi,

I wan't to access a secured website(site which requires login, not
https) like any email site http://gmail.com through java code.

I know I can do it through HttpClient, but my code simply doesn't
works. Please help out on this. I am really stuck with it. Here is the
source code for your reference.

package com.websitechecker;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class CommonsHttpClientSample {

HttpClient client;

public void init() {
client = new HttpClient();
}

public byte[] getSiteContent(URI uri, Credentials creds)
throws java.io.IOException {
// May be able to move these to the init depending on your use
// PostMethod method = new PostMethod();

// HeadMethod method = new HeadMethod();


GetMethod method = new GetMethod();

HostConfiguration config = new HostConfiguration();
HttpState remoteSession = new HttpState();

config.setHost(uri);

AuthScope authScope = new AuthScope(config.getHost(),
AuthScope.ANY_PORT);

remoteSession.setCredentials(authScope, creds);

client
.getParams()
.setCookiePolicy(

org.apache.commons.httpclient.cookie.CookiePolicy.BROWSER_COMPATIBILITY);
// NameValuePair action = new NameValuePair("action", "cll_r.show");
/*
* NameValuePair url = new NameValuePair( "url",
* "/mysyfact/mnu.accept?p_url=%42%52%4B%5F%4C%4F%47%4F%55%54%2E
%73%68%6F%77%3F%70%5F%6E%65%78%74%70%61%67%65%3D%62%72%6B%5F%6D%65%6E
%75%2E%73%68%6F%77%23%26%70%5F%6D%69%6D%5F%69%64%5F%68%6F%6F%66%64%3D
%31%39%31%26%70%5F%73%65%73%73%69%6F%6E%69%64%3D
%32%30%30%39%30%31%32%33%30%38%31%32%35%31%31%33%31%39%33%31%32%5F
%48%54%59%31"); //
* NameValuePair hoof_d = new NameValuePair("p_mim_id_hoofd",
"215");
* NameValuePair userid = new NameValuePair("username", "shankar");
* NameValuePair password = new NameValuePair("password", "syfact");
* method.setRequestBody(new NameValuePair[] { url, userid,
password });
*/
int responseInt = client.executeMethod(config, method,
remoteSession);

String Host = config.getHostURL();
URI getURI = method.getURI();
System.out.println("URI: " + getURI.toString());

System.out.println("Host : " + Host);

System.out.println("Response Int: " + responseInt);

byte[] bytes = method.getResponseBodyAsString().getBytes();
method.releaseConnection();

return bytes;
}

public String getContent(URI uri, Credentials creds)
throws java.io.IOException {
byte[] in = getSiteContent(uri, creds);

// ...
// you'll need pass 'in' through the rewriters just like
WebPagePortlet
// ...

return new String(in);
}

public static void main(String[] args) {
try {

URI uri = new URI(
"somewebsite",
true);
System.out.println("URI");

Credentials creds = new UsernamePasswordCredentials("username",
"password");
CommonsHttpClientSample foo = new CommonsHttpClientSample();
foo.init();
// foo.getContent(uri, creds);
System.out.print(foo.getContent(uri, creds));
} catch (Exception e) {
e.printStackTrace();
}
}
}


When I access it using a valid username and password, I am not able to
do so.

Please tell me as to where am I going wrong or if possible please
provide me with ready source code for the same.

Regards,
Dipesh
 
T

Tom Anderson

I wan't to access a secured website(site which requires login, not
https) like any email site http://gmail.com through java code.

I know I can do it through HttpClient, but my code simply doesn't works.
Please help out on this. I am really stuck with it. Here is the source
code for your reference.

Your code is using HTTP authentication - the password mechanism built into
the HTTP protocol. Most sites, gmail included, don't use that - they just
use a password typed into a form field. You have the code in your example
to do that, but it's commented out. Jere:
/*
* NameValuePair url = new NameValuePair( "url",
* "/mysyfact/mnu.accept?p_url=%42%52%4B%5F%4C%4F%47%4F%55%54%2E
%73%68%6F%77%3F%70%5F%6E%65%78%74%70%61%67%65%3D%62%72%6B%5F%6D%65%6E
%75%2E%73%68%6F%77%23%26%70%5F%6D%69%6D%5F%69%64%5F%68%6F%6F%66%64%3D
%31%39%31%26%70%5F%73%65%73%73%69%6F%6E%69%64%3D
%32%30%30%39%30%31%32%33%30%38%31%32%35%31%31%33%31%39%33%31%32%5F
%48%54%59%31"); //
* NameValuePair hoof_d = new NameValuePair("p_mim_id_hoofd",
"215");
* NameValuePair userid = new NameValuePair("username", "shankar");
* NameValuePair password = new NameValuePair("password", "syfact");
* method.setRequestBody(new NameValuePair[] { url, userid,
password });
*/

Why?

tom
 
A

Arne Vajhøj

Dips said:
I wan't to access a secured website(site which requires login, not
https) like any email site http://gmail.com through java code.

I know I can do it through HttpClient, but my code simply doesn't
works. Please help out on this. I am really stuck with it. Here is the
source code for your reference.
GetMethod method = new GetMethod();

HostConfiguration config = new HostConfiguration();
HttpState remoteSession = new HttpState();

config.setHost(uri);

AuthScope authScope = new AuthScope(config.getHost(),
AuthScope.ANY_PORT);

remoteSession.setCredentials(authScope, creds);

client
.getParams()
.setCookiePolicy(

org.apache.commons.httpclient.cookie.CookiePolicy.BROWSER_COMPATIBILITY);
int responseInt = client.executeMethod(config, method,
remoteSession);

String Host = config.getHostURL();
URI getURI = method.getURI();
System.out.println("URI: " + getURI.toString());

System.out.println("Host : " + Host);

System.out.println("Response Int: " + responseInt);

byte[] bytes = method.getResponseBodyAsString().getBytes();
method.releaseConnection();
When I access it using a valid username and password, I am not able to
do so.

Please tell me as to where am I going wrong or if possible please
provide me with ready source code for the same.

As other have suggested then you may need to used form based login
instead of HTTP authentication.

See below for an example.

Arne

================================

import java.io.IOException;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class Login {
private HttpClient client;

public Login() {
client = new HttpClient();
}

public void login(String url,
String userField, String userValue,
String passField, String passValue) {
NameValuePair[] nvp = new NameValuePair[2];
nvp[0] = new NameValuePair(userField, userValue);
nvp[1] = new NameValuePair(passField, passValue);
post(url, nvp);
}
public String get(String url) {
GetMethod met = new GetMethod(url);
try {
client.executeMethod(met);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return met.getResponseBodyAsString();
}
public String post(String url, NameValuePair[] nvp) {
PostMethod met = new PostMethod(url);
if(nvp != null) {
met.setRequestBody(nvp);
}
try {
client.executeMethod(met);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return met.getResponseBodyAsString();
}
public static void main(String[] args) {
Login lgi = new Login();
lgi.login("http://arne:8080/useradmin/Login",
"username", args[0],
"password", args[1]);

System.out.println(lgi.get("http://arne:8080/useradmin/UserAdmin.jsp"));
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top