Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpWebRequest is not sending Headers

I'm trying to simulate POST web request with HttpWebRequest class:

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(<my url>));
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;
        using (Stream stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }
        request.UserAgent = @"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)";

I need to set cookies for request. I tried tho ways:

request.Headers.Add(HttpRequestHeader.Cookie, GetGlobalCookies(<cookies url>));

 [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref uint pcchCookieData, int dwFlags, IntPtr lpReserved);
    const int INTERNET_COOKIE_HTTPONLY = 0x00002000;

    public static string GetGlobalCookies(string uri)
    {
        uint datasize = 1024;
        StringBuilder cookieData = new StringBuilder((int)datasize);
        if (InternetGetCookieEx(uri, null, cookieData, ref datasize, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero)
            && cookieData.Length > 0) {
            return cookieData.ToString(); //.Replace(';', ',');
        }
        else
        {
            return null;
        }
    }

and

request.CookieContainer = new CookieContainer();
request.CookieContainer.SetCookies(new Uri(<my url>), GetGlobalCookies(<cookies-url>).Replace(';', ','));

GetGlobalCookies() returns correct string with cookies i need. But after running requset.GetResponse() my request in fiddler looks like this:

POST <my url> HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: <my host>
Content-Length: 3
Expect: 100-continue
Connection: Keep-Alive

i=1

if i change Method to GET then cookies are correcyly sent, but still no User-Agent:

GET <my url> HTTP/1.1
Cookie: ASP.NET_SessionId=uajwt1ybpde4hudwwizuq2ld
Host: <my host>
Connection: Keep-Alive

Why HttpWebRequest is not sending Cookie and User-Agent headers?

like image 543
Oleg Osovitskiy Avatar asked Jul 17 '13 11:07

Oleg Osovitskiy


1 Answers

OK, figured it out myself. HttpWebRequest constructs request body when you call GetRequestStream(), so you have to set all required headers before calling GetRequestStream(), otherwise they won't be sent.

like image 182
Oleg Osovitskiy Avatar answered Nov 11 '22 01:11

Oleg Osovitskiy