anurahim wrote:Hi frnds,
my application needs to login to a web site thru the application. How
can i do it?
for that I need to post the username ,password and some other data to
the url.
the code is as follows. I am using SDK 1.0
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost("my url to submit");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("user_name", "myusername"));
nvps.add(new BasicNameValuePair("password", "mypassword"));
nvps.add(new BasicNameValuePair("hidden_data_to_post", "data
to post"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
if I execute this code , I will get an error:
Exception in thread "main" java.lang.RuntimeException: Stub!
with error in the line
DefaultHttpClient httpclient = new DefaultHttpClient();
if I use
HttpClient client = new HttpClient();
try{
method = new PostMethod("https://my url to post");
method.addParameter("user_name", "myusername"));
method.addParameter("password", "mypassword"));
method.addParameter("hidden_data_to_post", "data to post"));
method.setFollowRedirects(false);
client.executeMethod(method);
the response body will be 405 error
if I set the
method.setFollowRedirects(false); to be true,
the error will be
java.lang.IllegalArgumentException: Entity enclosing requests cannot be redirected without user intervention
So what may be the probelm.If I am wrng pls help me with sample code to login to https site with login data and some other parametrs the server needs using SDK 1.0
It's a problem with your parameters. From Wikipedia's entry on HTTP status codes:
405 Method Not Allowed
A request was made of a resource using a request method not supported by that resource; for example, using GET on a form which requires data to be presented via POST, or using PUT on a read-only resource.
The method I use for posting data right now for what I've been working on is:
Using java Syntax Highlighting
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url + authPath);
ArrayList<BasicNameValuePair> pairs = new ArrayList();
pairs.add(new BasicNameValuePair("username", logonName));
pairs.add(new BasicNameValuePair("password", this.password));
pairs.add(new BasicNameValuePair("destination", this.url + "/Exchange/"));
pairs.add(new BasicNameValuePair("flags", "0"));
pairs.add(new BasicNameValuePair("SubmitCreds", "Log+On"));
pairs.add(new BasicNameValuePair("forcedownlevel", "0"));
pairs.add(new BasicNameValuePair("trusted", "0"));
try {
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs);
/** Assign the POST data to the entity */
httppost.setEntity(p_entity);
/** Perform the actual HTTP POST */
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
int status_code = response.getStatusLine().getStatusCode();
this.cookies = httpclient.getCookieStore();
if (status_code >= 300) {
this.isAuthenticated = false;
return false;
} else {
this.isAuthenticated = true;
return true;
}
} catch (UnsupportedEncodingException uee) {
// Woops
} catch (IOException ioe) {
// Woops
}
Parsed in 0.037 seconds, using
GeSHi 1.0.8.4
I apologize if any of that isn't decipherable. That's just copy/pasted from my scratchpad.