You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Andrés López <pe...@gmail.com> on 2010/02/27 17:13:37 UTC

Form Login problem

Hi all,

I'm trying to access a protected page in a website made in ASPX. The website
is not mine. The problem i have is that i can't seem to get to login, i'm
not sure wh, I tried a lot of different things but i always get the "not
logged in" page.

Please notice that i'm a novice Java programmer.

Here is my code:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class HTTPPoster {

     public static HttpResponse doPost(String url, Map<String, String>
kvPairs)
               throws ClientProtocolException, IOException {
          DefaultHttpClient httpclient = new DefaultHttpClient();

          httpclient.getParams().setParameter(
                  ClientPNames.COOKIE_POLICY,
                  CookiePolicy.BROWSER_COMPATIBILITY);



          HttpPost httppost = new HttpPost(url);
          httppost.addHeader("Referrer", "
https://servicios.personal.com.ar/autogestionindividuos/default.aspx");

          if (kvPairs != null && kvPairs.isEmpty() == false) {
               List<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>(
                         kvPairs.size());
               String k, v;
               Iterator<String> itKeys = kvPairs.keySet().iterator();
               while (itKeys.hasNext()) {
                    k = itKeys.next();
                    v = kvPairs.get(k);
                    nameValuePairs.add(new BasicNameValuePair(k, v));
               }
               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
          }
          HttpResponse response;
          response = httpclient.execute(httppost);

          HttpEntity ent = response.getEntity();
          if (ent != null) ent.consumeContent();
          System.out.println("First page: " +
response.getStatusLine().toString());

          List<Cookie> cookies = httpclient.getCookieStore().getCookies();
          if (cookies.isEmpty()) {
              System.out.println("None");
          } else {
              for (int i = 0; i < cookies.size(); i++) {
                  System.out.println("- " + cookies.get(i).toString());
              }
          }

          HttpGet httpget = new HttpGet("
https://servicios.personal.com.ar/autogestionindividuos/MedidorTraficoDatosPospago.aspx
");
          httpget.addHeader("Referrer", "
https://servicios.personal.com.ar/autogestionindividuos/default.aspx");
          HttpResponse response2 = httpclient.execute(httpget);
          System.out.println("Second page: " +
response2.getStatusLine().toString());
          //cookies = httpclient.getCookieStore().getCookies();
          if (cookies.isEmpty()) {
              System.out.println("None");
          } else {
              for (int i = 0; i < cookies.size(); i++) {
                  System.out.println("- " + cookies.get(i).toString());
              }
          }
          return response2;
     }

}

And here is my output log. In bold you can see the prints done in the
program.

2010/02/27 00:51:37:884 GMT-03:00 [DEBUG] SingleClientConnManager - Get
connection for route HttpRoute[{s}->https://servicios.personal.com.ar]
2010/02/27 00:51:38:843 GMT-03:00 [DEBUG] RequestAddCookies - CookieSpec
selected: compatibility
2010/02/27 00:51:38:850 GMT-03:00 [DEBUG] DefaultHttpClient - Attempt 1 to
execute request
2010/02/27 00:51:38:850 GMT-03:00 [DEBUG] DefaultClientConnection - Sending
request: POST /autogestionindividuos/default.aspx HTTP/1.1
2010/02/27 00:51:38:851 GMT-03:00 [DEBUG] headers - >> POST
/autogestionindividuos/default.aspx HTTP/1.1
2010/02/27 00:51:38:851 GMT-03:00 [DEBUG] headers - >> Referrer:
https://servicios.personal.com.ar/autogestionindividuos/default.aspx
2010/02/27 00:51:38:851 GMT-03:00 [DEBUG] headers - >> Content-Length: 67
2010/02/27 00:51:38:851 GMT-03:00 [DEBUG] headers - >> Content-Type:
application/x-www-form-urlencoded; charset=ISO-8859-1
2010/02/27 00:51:38:851 GMT-03:00 [DEBUG] headers - >> Host:
servicios.personal.com.ar
2010/02/27 00:51:38:851 GMT-03:00 [DEBUG] headers - >> Connection:
Keep-Alive
2010/02/27 00:51:38:852 GMT-03:00 [DEBUG] headers - >> User-Agent:
Apache-HttpClient/4.0.1 (java 1.5)
2010/02/27 00:51:38:852 GMT-03:00 [DEBUG] headers - >> Expect: 100-Continue
2010/02/27 00:51:40:976 GMT-03:00 [DEBUG] DefaultClientConnection -
Receiving response: HTTP/1.1 200 OK
2010/02/27 00:51:40:976 GMT-03:00 [DEBUG] headers - << HTTP/1.1 200 OK
2010/02/27 00:51:40:977 GMT-03:00 [DEBUG] headers - << Date: Sat, 27 Feb
2010 03:51:49 GMT
2010/02/27 00:51:40:977 GMT-03:00 [DEBUG] headers - << Set-Cookie:
ASP.NET_SessionId=kqyrzo45zji3ur451jvfluba; path=/; HttpOnly
2010/02/27 00:51:40:977 GMT-03:00 [DEBUG] headers - << Cache-Control:
private
2010/02/27 00:51:40:977 GMT-03:00 [DEBUG] headers - << Content-Type:
text/html; charset=utf-8
2010/02/27 00:51:40:977 GMT-03:00 [DEBUG] headers - << Content-Length: 47694
2010/02/27 00:51:40:982 GMT-03:00 [DEBUG] ResponseProcessCookies - Cookie
accepted: "[version: 0][name: ASP.NET_SessionId][value:
kqyrzo45zji3ur451jvfluba][domain: servicios.personal.com.ar][path:
/][expiry: null]".
2010/02/27 00:51:40:983 GMT-03:00 [DEBUG] DefaultHttpClient - Connection can
be kept alive indefinitely
2010/02/27 00:51:41:373 GMT-03:00 [DEBUG] SingleClientConnManager -
Releasing connection
org.apache.http.impl.conn.SingleClientConnManager$ConnAdapter@99681b
*First page: HTTP/1.1 200 OK
- [version: 0][name: ASP.NET_SessionId][value:
kqyrzo45zji3ur451jvfluba][domain: servicios.personal.com.ar][path:
/][expiry: null]*
2010/02/27 00:51:41:374 GMT-03:00 [DEBUG] SingleClientConnManager - Get
connection for route HttpRoute[{s}->https://servicios.personal.com.ar]
2010/02/27 00:51:41:374 GMT-03:00 [DEBUG] DefaultHttpClient - Stale
connection check
2010/02/27 00:51:41:375 GMT-03:00 [DEBUG] RequestAddCookies - CookieSpec
selected: compatibility
2010/02/27 00:51:41:375 GMT-03:00 [DEBUG] RequestAddCookies - Cookie
[version: 0][name: ASP.NET_SessionId][value:
kqyrzo45zji3ur451jvfluba][domain: servicios.personal.com.ar][path:
/][expiry: null] match [(secure)
servicios.personal.com.ar:443/autogestionindividuos/MedidorTraficoDatosPospago.aspx
]
2010/02/27 00:51:41:375 GMT-03:00 [DEBUG] DefaultHttpClient - Attempt 1 to
execute request
2010/02/27 00:51:41:375 GMT-03:00 [DEBUG] DefaultClientConnection - Sending
request: GET /autogestionindividuos/MedidorTraficoDatosPospago.aspx HTTP/1.1
2010/02/27 00:51:41:375 GMT-03:00 [DEBUG] headers - >> GET
/autogestionindividuos/MedidorTraficoDatosPospago.aspx HTTP/1.1
2010/02/27 00:51:41:375 GMT-03:00 [DEBUG] headers - >> Referrer:
https://servicios.personal.com.ar/autogestionindividuos/default.aspx
2010/02/27 00:51:41:375 GMT-03:00 [DEBUG] headers - >> Host:
servicios.personal.com.ar
2010/02/27 00:51:41:375 GMT-03:00 [DEBUG] headers - >> Connection:
Keep-Alive
2010/02/27 00:51:41:376 GMT-03:00 [DEBUG] headers - >> User-Agent:
Apache-HttpClient/4.0.1 (java 1.5)
2010/02/27 00:51:41:376 GMT-03:00 [DEBUG] headers - >> Cookie:
ASP.NET_SessionId=kqyrzo45zji3ur451jvfluba
2010/02/27 00:51:41:434 GMT-03:00 [DEBUG] DefaultClientConnection -
Receiving response: HTTP/1.1 200 OK
2010/02/27 00:51:41:434 GMT-03:00 [DEBUG] headers - << HTTP/1.1 200 OK
2010/02/27 00:51:41:434 GMT-03:00 [DEBUG] headers - << Date: Sat, 27 Feb
2010 03:51:50 GMT
2010/02/27 00:51:41:435 GMT-03:00 [DEBUG] headers - << Cache-Control:
private
2010/02/27 00:51:41:435 GMT-03:00 [DEBUG] headers - << Content-Type:
text/html; charset=utf-8
2010/02/27 00:51:41:435 GMT-03:00 [DEBUG] headers - << Content-Length: 3903
2010/02/27 00:51:41:435 GMT-03:00 [DEBUG] DefaultHttpClient - Connection can
be kept alive indefinitely
*Second page: HTTP/1.1 200 OK
- [version: 0][name: ASP.NET_SessionId][value:
kqyrzo45zji3ur451jvfluba][domain: servicios.personal.com.ar][path:
/][expiry: null]*
2010/02/27 00:51:41:438 GMT-03:00 [DEBUG] SingleClientConnManager -
Releasing connection
org.apache.http.impl.conn.SingleClientConnManager$ConnAdapter@2f1921


Thanks for for you help

Andrés