I'm attempting to log into a wesbite. On my first request, I do a POST on the login page with the username and password and log in successfully. Using the same instance of HttpClient, I do a second request on a different page on the website, this time a GET request, but this time, the page returned is not logged in. I think this is due to the fact that the second request is being done in a new session.
After the first request, among the many cookies it returns, this is the session ID cookie:
name: ASPSESSIONIDSCSCSBCS
value: GBAJALJBOGKBFLAELPNKEDOE
And after the second request, among many other cookies, I have two different session ID cookies:
name: ASPSESSIONIDSCSCSBCS
value: GBAJALJBOGKBFLAELPNKEDOE
name: ASPSESSIONIDSCSCSBCS
value: MBAJALJBDBOKPEHNCDDFOCBC
I'm assuming that because the session id is different during the second request, it ignores cookies with the first session id.
How do I fix this?
Edit: This is my code
public HttpClient httpclient = new DefaultHttpClient();
public CookieStore cookieStore = new BasicCookieStore();
public HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
//The first request
HttpPost httppost = new HttpPost("http://www.deeproute.com/deeproute/default.asp");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("cookieexists","false"));
nameValuePairs.add(new BasicNameValuePair("name", mUser));
nameValuePairs.add(new BasicNameValuePair("password", mPassword));
nameValuePairs.add(new BasicNameValuePair("subbera", "Login"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
res = httpclient.execute(httppost, localContext);
//The second request
HttpGet rosterGet = new HttpGet("http://deeproute.com/deeproute/?sel=rosterlook&myleagueno=6&myteamno=12");
res = httpclient.execute(rosterGet, localContext);
I think you should overwrite cookieStore with a
cookieStore = (BasicCookieStore) httpClient.getCookieStore();
only after success in login..
after that I think you should set again the Context with new cookieStore..
Let me know!
Related
When i try to send post request using Java (JVM on Mac) with correct credentials I got correct response code 302 FOUND.
But when using the same code on the android, with SAME correct params, i got response CODE 200 OK.
Code to send post request:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(POST_URL);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("auth", "login"));
nvps.add(new BasicNameValuePair("login", login));
nvps.add(new BasicNameValuePair("password", password));
nvps.add(new BasicNameValuePair("set_cookie", "on"));
httpost.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
302 FOUND is the common way of doing a "redirection", it seems Android or MacJVM is not performing that redirection. Are you using the same SDK version?
Try to use :
final HttpParams params = new BasicHttpParams();
HttpClientParams.setRedirecting(params, false);
I have an application that uses a https post to a remote server.
Everytime I attempt to post to that server I get a response that says that I have not added the id parameter.
Here is my code
HttpHost host = new HttpHost("hostname", 443, "https");
HttpPost httppost = new HttpPost(uri);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("description", "Android"));
nameValuePairs.add(new BasicNameValuePair("type", "Android"));
nameValuePairs.add(new BasicNameValuePair("id", DeviceUtils.getID()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
String content = httppost.getEntity().toString();
HttpResponse response = httpclient.execute(host, httppost);
I have tried with and without the "custom" httphost, but I always get the same response.
Similar code has been used in a javaclient that runs on the desktop, and it works just fine.
The Id is the deviceId that has been RSA Encrypted and Base64 encoded.
Any ideas as to what I am doing wrong here.
Hi I'm having a lot of trouble submitting a simple form, I have searched around and it appears quite a few people have had the same problem but I haven't found an answer.
Here's my code so far:
public void postData(TextView txtResult, String user, String pass) throws ClientProtocolException, IOException {
HttpPost post = new HttpPost("https://www.mymeteor.ie");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", user));
nameValuePairs.add(new BasicNameValuePair("userpass", pass));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
txtResult.setText(responseText);
}
The above code will simply return the original page,
can anybody help me?
thanks
Are you sure that URL supports logging in via post that way? It looks to me like the login form sends the post data to this URL: https://www.mymeteor.ie/go/mymeteor-login-manager
I would also suspect you should be using some sort of API instead of just posting data to their login form, remotely.
I am using the HttpClient 4.1.1 to test my server's REST API.
I can manage to login seem to work fine but when I try to do anything else I am failing.
Most likely I have a problem setting the cookie in the next request.
Here is my code currently:
HttpGet httpGet = new HttpGet(<my server login URL>);
httpResponse = httpClient.execute(httpGet)
sessionID = httpResponse.getFirstHeader("Set-Cookie").getValue();
httpGet.addHeader("Cookie", sessionID);
httpClient.execute(httpGet);
Is there a better way to manage the session/cookies setting in the HttpClient package?
The correct way is to prepare a CookieStore which you need to set in the HttpContext which you in turn pass on every HttpClient#execute() call.
HttpClient httpClient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
// ...
HttpResponse response1 = httpClient.execute(method1, httpContext);
// ...
HttpResponse response2 = httpClient.execute(method2, httpContext);
// ...
I'm trying to add a referer to an http post in Apache HttpClient (httpclient-4.0-beta2).
I found some sample code that does this. The code works, but I'm wondering if there is not a simpler, more straightforward way to add the referer than using the (ominously named) addRequestInterceptor, which appears to take an (yikes!) inner class as a parameter.
The code in question begins below with "// add the referer header". I'm a novice, and this code is doing several things that I don't understand. Is this really the simplest way to add a referer to my http post?
Thanks for any pointers.
// initialize request parameters
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("firstName", "John"));
formparams.add(new BasicNameValuePair("lastName", "Doe"));
// set up httppost
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
HttpPost httppost = new HttpPost(submitUrl);
httppost.setEntity(entity);
// create httpclient
DefaultHttpClient httpclient = new DefaultHttpClient();
// add the referer header, is an inner class used here?
httpclient.addRequestInterceptor(new HttpRequestInterceptor()
{
public void process(final HttpRequest request,
final HttpContext context) throws HttpException, IOException
{
request.addHeader("Referer", referer);
}
});
// execute the request
HttpResponse response = httpclient.execute(httppost);
Any reason not to do:
httppost.addHeader("Referer", referer);
? HttpPost subclasses (indirectly) AbstractHttpMessage so you should be able to just add headers that way.