473,383 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 12741

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

Similar topics

2
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
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
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
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
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
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
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
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.