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
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