views:

97

answers:

2

I am trying to make a POST request in which I am supposed to send Raw POST data.

Which property should I modify to achieve this.

Is it the HttpWebRequest.ContentType property. If, so what value should I assign to it.

+1  A: 

You want to set the ContentType property to the mime type of the data. If its a file, it depends on the type of file, if it's plain text then text/plain and if it's an arbitrary binary data of your own local purposes then application/octet-stream. In the case of text-based formats you'll want to include the charset along with the content type, e.g. "text/plain; charset=UTF-8".

You'll then want to call GetRequestStream() and write the data to the stream returned.

Jon Hanna
+1  A: 
    public static string HttpPOST(string url, string querystring)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.ContentType = "application/x-www-form-urlencoded"; // or whatever - application/json, etc, etc
        StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());

        try
        {
            requestWriter.Write(querystring);
        }
        catch
        {
            throw;
        }
        finally
        {
            requestWriter.Close();
            requestWriter = null;
        }

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        using (StreamReader sr = new StreamReader(response.GetResponseStream()))
        {
            return sr.ReadToEnd();
        }
    }
Phil Winkel
Can you please elaborate a bit on the part where you wrote "whatever". Actually, this is the part I need clarifying. What should it be in the case of Raw Posting.
Gunner
Gunner, the problem is, what do **you** mean by "raw"?
Jon Hanna
I think the word 'raw' here is internal jargon which I was told about. Basically, all I need to do is post the request. "req.ContentType = "application/x-www-form-urlencoded";", this is working for me
Gunner
Ah, I took "raw" as meaning that isn't what you needed. Kirk was right about the duplication.
Jon Hanna