By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,636 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,636 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.AllowAutoRedirect = 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.AllowAutoRedirect = 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 HttpPostRedirect() method below:

NetworkCredential credentials = new NetworkCredential(username,
password);
CookieContainer cookies = new CookieContainer();

string data = "username=" + username + "&password=" + password;
string webpage = HttpPostRedirect(
"https://www.vodafone.ie/myv/services/login/Login.shtml", data,
credentials, cookies);
// HTTP POST REDIRECT METHOD
protected string HttpPostRedirect(string uri, string data,
NetworkCredential networkCredentials, CookieContainer cookies)
{
// PREPARE REQUEST
System.Net.HttpWebRequest req =
(HttpWebRequest)System.Net.WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "application/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.CookieContainer = cookies;
req.Credentials = networkCredentials;
req.AllowAutoRedirect = false;

// ENCODE DATA & POST IT
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(data);
req.ContentLength = bytes.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();

// GET RESPONSE
System.Net.HttpWebResponse resp =
(HttpWebResponse)req.GetResponse();
if (resp == null) return null;

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

// 302 REDIRECT?
string webpage;
if (resp.StatusCode == HttpStatusCode.Found)
{
string newLocation = resp.Headers["Location"];
webpage = HttpGet(newLocation, networkCredentials,
cookies);
}
else
{
// READ RESPONSE
System.IO.StreamReader sr = new
System.IO.StreamReader(resp.GetResponseStream());
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,image/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=XXXXXXXXXX&password=XXXX&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=10AEDBFCA615C2372A76E4B2CDC02D5D;Path=/
Set-Cookie: SITESELECTION=PERSONAL;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,image/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=10AEDBFCA615C2372A76E4B2CDC02D5D;
SITESELECTION=PERSONAL

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=ISO-8859-1
Cache-Control: private,no-cache,max-age=0
Transfer-Encoding: chunked

Jan 18 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.