HttpWebRequest doesn't receive cookie on POST

B

Brent

The code below is, I believe, as simple as I can make it. The idea is
to connect to a login page at http://www.fitchratings.com/jsp/corporate/Login.faces
, submit an incorrect username and password, and get back a page that
informs me of the credentials problem. This behavior happens when
connecting using a regular browser. However, the code below instead
presents the original login form, as if nothing had been posted,
despite my efforts so far. I've tried to work through several
possibilities, but I'm wondering whether an Apache server, using some
sort of Java backend, just doesn't like C# HttpWebRequest.

Here are some variables I've tried to test and eliminate:

1) The server checks the User-Agent to see whether it can accept
cookies (I've set the User-Agent as a standard browser).

2) The server wants the Keep-Alive property true or false (tested
both).

3) The "Expect-100" property of an HttpWebRequest confuses the server
(turned it off).

4) The server wants a series of headers that look like a regular
brower (I added several headers in hopes of mimicking IE precisely).

5) The server expects Google Analytics cookies (added these manually,
though not in the example below).

6) The server wants the post data in ASCII bytes vs. UTF-8 bytes
(tested both).

I'm at the point of sacrificing a virgin or chicken, whichever crosses
my path first...so if you have any ideas at all, I would greatly
appreciate them.

Thanks in advance.

--Brent

-----------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;
using System.IO;

namespace FitchTest
{
class Program
{
static void Main(string[] args)
{
getFitchCookie();
}

static string userAgent = "Mozilla/4.0 (compatible; MSIE 7.0;
Windows NT 6.0;)";

static private CookieContainer getFitchCookie()
{
CookieContainer cookies = new CookieContainer();
string cookieText = string.Empty;

try
{
//1. Get initial cookie
string url = "http://www.fitchratings.com/";
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url);
request.Method = "GET";
request.Accept = "image/gif, image/x-xbitmap, image/
jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-
xpsdocument, application/xaml+xml, application/x-ms-xbap, application/
x-silverlight, application/vnd.ms-excel, application/vnd.ms-
powerpoint, application/msword, */*";
request.Headers.Add("Accept-Language", "en-us");
request.Headers.Add("UA-CPU", "x86");
request.Headers.Add("Accept-Encoding", "gzip,
deflate");
request.UserAgent = userAgent;
request.KeepAlive = true;
request.Headers.Add("Pragma", "no-cache");
request.CookieContainer = cookies;
request.ServicePoint.Expect100Continue = false;


HttpWebResponse resp = (HttpWebResponse)
request.GetResponse();
resp.Close();

//2. Get "real" cookie
string url2 = "http://www.fitchratings.com/jsp/
corporate/Login.faces";

string username = "username";
string password = "password";
string postData = "loginForm%3AuserName=" + username +
"&loginForm%3Apassword=" + password + "&loginForm
%3Aoverride=true&loginForm%3Asubmit=Login&loginForm_SUBMIT=1&loginForm
%3A_link_hidden_=";
byte[] postDataBytes = Encoding.UTF8.GetBytes
(postData);


request = (HttpWebRequest)WebRequest.Create(url2);

request.Method = "POST";
request.Referer = url2;
request.ContentType = "application/x-www-form-
urlencoded";
request.ContentLength = postDataBytes.Length;
request.AllowAutoRedirect = false;
request.Accept = "image/gif, image/x-xbitmap, image/
jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-
xpsdocument, application/xaml+xml, application/x-ms-xbap, application/
x-silverlight, application/vnd.ms-excel, application/vnd.ms-
powerpoint, application/msword, */*";
request.Headers.Add("Accept-Language", "en-us");
request.Headers.Add("UA-CPU", "x86");
request.Headers.Add("Accept-Encoding", "gzip,
deflate");
request.UserAgent = userAgent;
request.KeepAlive = true;
request.Headers.Add("Pragma", "no-cache");
request.CookieContainer = cookies;
request.ServicePoint.Expect100Continue = false;

Stream requestStream = request.GetRequestStream();
requestStream.Write(postDataBytes, 0,
postDataBytes.Length);
requestStream.Close();

resp = (HttpWebResponse)request.GetResponse();
resp.Cookies = request.CookieContainer.GetCookies
(request.RequestUri);
resp.Close();

}
catch (Exception ex)
{
Console.WriteLine(ex.Message + Environment.NewLine +
Environment.NewLine + ex.StackTrace);
}

return cookies;

}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top