Login to website using HttpWebRequest + AllowAutoRedirect + POST + Going MAD!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • barrybevel@ananzi.co.za

    Login to website using HttpWebRequest + AllowAutoRedirect + POST + Going MAD!

    Hi,
    I'm trying to login to the www.vodafone.ie website using
    HttpWebRequest.
    It works fine with IE/Firefox and the .NET Web Control too, just not
    with my code.
    I think it's a redirect 302 problem.
    I'm using this code in a ASP.NET 2.0 application just in case that
    matters,
    maybe someone knows a better way to do this?

    If request.AllowAu toRedirect = true I get back a page saying
    "Services We're sorry, there was a problem processing your request.
    Please try again, if the problem persists please contact Customer Care"
    This code works on other sites, just not this one.

    I tried setting request.AllowAu toRedirect = false and then get the
    value of the Location header and then perform a GET, but the Location
    header contains "http://www.vodafone.ie/myv/services/error/error.jsp"
    so it has already failed!

    I have the method I use to POST below follow by a dump of the HTTP
    headers.
    Any ideas/hints/tips would be appreciated. Thanks.

    Here's the code I use to call the HttpPostRedirec t() method below:

    NetworkCredenti al credentials = new NetworkCredenti al(username,
    password);
    CookieContainer cookies = new CookieContainer ();

    string data = "username=" + username + "&password= " + password;
    string webpage = HttpPostRedirec t(
    "https://www.vodafone.ie/myv/services/login/Login.shtml", data,
    credentials, cookies);


    // HTTP POST REDIRECT METHOD
    protected string HttpPostRedirec t(string uri, string data,
    NetworkCredenti al networkCredenti als, CookieContainer cookies)
    {
    // PREPARE REQUEST
    System.Net.Http WebRequest req =
    (HttpWebRequest )System.Net.Web Request.Create( uri);
    req.Method = "POST";
    req.ContentType = "applicatio n/x-www-form-urlencoded";
    req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
    5.1; SV1; .NET CLR 2.0.50727)";
    req.KeepAlive = true;
    req.CookieConta iner = cookies;
    req.Credentials = networkCredenti als;
    req.AllowAutoRe direct = false;

    // ENCODE DATA & POST IT
    byte[] bytes = System.Text.Enc oding.ASCII.Get Bytes(data);
    req.ContentLeng th = bytes.Length;
    System.IO.Strea m os = req.GetRequestS tream();
    os.Write(bytes, 0, bytes.Length);
    os.Close();

    // GET RESPONSE
    System.Net.Http WebResponse resp =
    (HttpWebRespons e)req.GetRespon se();
    if (resp == null) return null;

    // SAVE COOKIES
    cookies.Add(req .RequestUri, resp.Cookies);

    // 302 REDIRECT?
    string webpage;
    if (resp.StatusCod e == HttpStatusCode. Found)
    {
    string newLocation = resp.Headers["Location"];
    webpage = HttpGet(newLoca tion, networkCredenti als,
    cookies);
    }
    else
    {
    // READ RESPONSE
    System.IO.Strea mReader sr = new
    System.IO.Strea mReader(resp.Ge tResponseStream ());
    webpage = sr.ReadToEnd(). Trim();
    }
    return webpage;
    }


    Here are the HTTP request/response headers when I use Firefox to do it:

    POST /myv/services/login/Login.shtml HTTP/1.1
    Host: www.vodafone.ie
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1)
    Gecko/20061204 Firefox/2.0.0.1
    Accept:
    text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,ima ge/png,*/*;q=0.5
    Accept-Language: en-us,en;q=0.5
    Accept-Encoding: gzip,deflate
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive: 300
    Connection: keep-alive
    Referer: http://www.vodafone.ie/
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 42
    username=XXXXXX XXXX&password=X XXX&x=16&y=6

    HTTP/1.x 302 Moved Temporarily
    Server: Sun-ONE-Web-Server/6.1
    Date: Thu, 18 Jan 2007 00:54:55 GMT
    Content-Length: 0
    Content-Type: text/html
    Cache-Control: private,no-cache,max-age=0
    Location: https://www.vodafone.ie/myv/index.jsp
    Set-Cookie: JSESSIONID=10AE DBFCA615C2372A7 6E4B2CDC02D5D;P ath=/
    Set-Cookie: SITESELECTION=P ERSONAL;Expires =Thu, 25-Jan-2007 00:54:55
    GMT;Path=/

    GET /myv/index.jsp HTTP/1.1
    Host: www.vodafone.ie
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1)
    Gecko/20061204 Firefox/2.0.0.1
    Accept:
    text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,ima ge/png,*/*;q=0.5
    Accept-Language: en-us,en;q=0.5
    Accept-Encoding: gzip,deflate
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive: 300
    Connection: keep-alive
    Referer: http://www.vodafone.ie/
    Cookie: JSESSIONID=10AE DBFCA615C2372A7 6E4B2CDC02D5D;
    SITESELECTION=P ERSONAL

    HTTP/1.x 200 OK
    Server: Sun-ONE-Web-Server/6.1
    Date: Thu, 18 Jan 2007 00:54:56 GMT
    Content-Type: text/html;charset=IS O-8859-1
    Cache-Control: private,no-cache,max-age=0
    Transfer-Encoding: chunked

Working...