views:

1468

answers:

2

How to post the following HTTP requests in C#

POST http://10.0.0.1/st_poe.cgi

Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
Referer: http://10.0.0.1/RST_st_poe.htm
Accept-Language: en-US
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: 10.0.0.1
Content-Length: 21
Connection: Keep-Alive
Pragma: no-cache
Authorization: Basic YWStaW47c3Jsa3NobQ==

ConMethod=++Connect++

I'm trying to do it with the following code. It is not working.

            string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));

            byte[] bytes = System.Text.Encoding.ASCII.GetBytes("ConMethod=++Connect++");

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://10.0.0.1/st_poe.cgi");

            request.Method = "POST";
            request.Headers.Add("Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*");
            request.Referer = "http://10.0.0.1/RST_st_poe.htm";
            request.Headers.Add("Accept-Language: en-US");
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Headers.Add("Accept-Encoding: gzip, deflate");
            request.ContentLength = bytes.Length;
            request.Headers.Add("Host: 10.0.0.1");
            request.Headers.Add("Connection: Keep-Alive");
            request.Headers.Add("Pragma: no-cache");
            request.Headers.Add("Authorization: Basic "+user);


            Stream reqStream = request.GetRequestStream();

            reqStream.Write(bytes, 0, bytes.Length);

            reqStream.Close();

Can anyone point out where I'm messing up. I'm using HttpWebRequest for the first time.

+9  A: 

You should perform the actual request with

var response = request.GetResponse();

Alternatively, you can use the simpler System.Net.WebClient class:

var client = new WebClient();
client.Headers["..."] = ...;
// Use one of the DownloadXXX/UploadXXX methods.
var responseBody = client.UploadData("Url", dataToUpload);
Mehrdad Afshari
I didn't know there was a method called UploadData() cool I thought it was only possible to uploadfile()
dr. evil
+1  A: 

Mehrdad has it right. I'll only add that if you're going to stick to HttpWebRequest, then you need to learn to use "using" blocks for any resource you allocate that implements IDisposable:

var user =
    Convert.ToBase64String(
        Encoding.UTF8.GetBytes(username + ":" + password));

var bytes = Encoding.ASCII.GetBytes("ConMethod=++Connect++");

var request =
    (HttpWebRequest) WebRequest.Create("http://10.0.0.1/st_poe.cgi");

request.Method = "POST";
request.Headers.Add(
    "Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*");
request.Referer = "http://10.0.0.1/RST_st_poe.htm";
request.Headers.Add("Accept-Language: en-US");
request.UserAgent =
    "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("Accept-Encoding: gzip, deflate");
request.ContentLength = bytes.Length;
request.Headers.Add("Host: 10.0.0.1");
request.Headers.Add("Connection: Keep-Alive");
request.Headers.Add("Pragma: no-cache");
request.Headers.Add("Authorization: Basic " + user);

using (var reqStream = request.GetRequestStream())
{
    reqStream.Write(bytes, 0, bytes.Length);
}

using (var response = (HttpWebResponse) request.GetResponse())
{
    using (var responseStream = response.GetResponseStream())
    {
        using (var reader = new StreamReader(responseStream))
        {
            return reader.ReadToEnd();
        }
    }
}
John Saunders
it does not invalidate your assertion that `using` is a convenient construct, but for wcf CommunicationObject, which implement IDisposable, `using` is a quick way for the uninitiated to plumb the depths of madness...
Sky Sanders
@code: I'm aware of that. But note that WCF is the only serious exception I know about, and that's due to a design flaw in WCF. In every other case, the fact that a class implements IDisposable means you should make sure to call Dispose, and in all but one case, that means you should almost certainly use a using block.
John Saunders