473,508 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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
0 12750

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2305
by: Gill Bates | last post by:
I'm trying to login to a banking site (https://www.providentconnection.com) using vb.net. I've tried many variations of WebClient and HttpWebRequest; none of which I've got to work. My latest...
14
7863
by: n8 | last post by:
Hi, Hi have to do the followign and have been racking my brain with various solutions that have had no so great results. I want to use the System.Net.WebClient to submit data to a form (log a...
1
1453
by: PokerJoker | last post by:
My company has a third party purchased website that we use to allow clients to log in and view/modify orders in our system. It has its own login page, but I would like to add a form to my...
0
1363
by: Brett | last post by:
I'm trying three different methods for logging into a webmail account. In Post1() and Post2(), I always get back the login page. Not the page after logging in. On TryCookies(), I'm not sure. It...
6
3319
by: AppleBag | last post by:
I'm having the worst time trying to login to myspace through code. Can someone tell me how to do this? Please try it yourself before replying, only because I have asked this a couple of times in...
10
8133
by: Victor | last post by:
hi guys. In my project, now I am using a asp.net login control and a customized membership provider to do the form authentication. Now I want some function that user can skip the login form and be...
0
1182
by: Rama Jayapal | last post by:
i use the following code to login to a website but i dont achieve my purpose can anybody help me on this //code to request the page webRequest = HttpWebRequest.Create(url) as...
1
1986
by: twebb72 | last post by:
Scratching my head for a while on this one... This project uses code from "Understanding HttpWebRequest CookieContainer?" post on this site. C#, asp.net 2.0 The following code supplies...
5
3175
by: mcfly1204 | last post by:
I am attempting to use WebRequest to access a page that requires a login/password to access. My last WebRequest continues to timeout. Any help or thoughts would be appreciated. namespace...
0
7328
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.