2

I am using C# for my project, can anyone tell me what is a standered structure of a HTTP POST Requset. How to attach POST data like a file in the Request from code.

Simply i want to create a POST request from my code itself, with diffrent items to be posted available.

I have checked Ietf's RFC for http POST but its too long....

bjb568
  • 11,089
  • 11
  • 50
  • 71
Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79

3 Answers3

5

Specs for simple reference

I have always appreciated HTTP Made Really Easy as a starting point. It's small, concise and friendly.

Often you can get enough implementation details (or at least enough understanding) from this guide's simple style to suffice your need. It has worked for me many times. There is a section on POST. The guide builds cumulatively.

Additionally it links to proper specifications and fuller resources should you need to reference them and get into more detail.

.NET Supporting Classes

Fortunately the .NET Framework Class Library contains higher level classes that can simplify your life. Look into the MSDN documentation and examples about System.Net.WebClient (doesn't lend itself as well to POST, favours GET for quick usage methods). Consider the more flexible System.Web.HttpRequest and System.Web.HttpResponse counterpart classes.

Example using C#

This code sample shows the concept of posting binary data to a stream.

This method is called like:

PostMyData(Stream_instance, "http://url_to_post_to");

Namespaces involved are:

using System.IO;
using System.Net;

The custom method would look something like the following.
Note: Concept taken from MSDN sample code here.

Although I use MIME type application/octet-stream for generic binary data, you can use any well known type from this list of mime types to target the kind of binary data you are sending.

public int PostMyData(Stream binaryData, string postToUrl) {

    // make http request
    var request = (HttpWebRequest)WebRequest.Create(postToUrl);
    request.Method = "POST";
    request.ContentType = "application/octet-stream"; // binary data: 

    // data (bytes) that will be posted in body of request
    var streamOut = request.GetRequestStream();
    binaryData.CopyTo(streamOut);

    // post and get response
    using (var response = (HttpWebResponse)request.GetResponse()) {
        var code = response.StatusCode;
        return (int)code;
    }

}
John K
  • 28,441
  • 31
  • 139
  • 229
  • Thanx for your answer, but how do i add a Image in the POST data, i mean can i send the raw bytes or do i need to do somthing with it. – Shekhar_Pro Dec 18 '10 at 03:50
  • For C# code to stuff an image into a System.IO.Stream instance for passing into this function's `binaryData` parameter see this answer: http://stackoverflow.com/questions/1668469/system-drawing-image-to-stream-c – John K Dec 18 '10 at 05:31
1

Use HttpWebRequest, its always the best way, but for a more simple aproach on Http Post read: http://programaticallyspeaking.site40.net/blog/2010/11/how-to-implement-an-http-server-part-1/

DVD
  • 1,744
  • 3
  • 17
  • 34
0

Hey i found a way to view a sample POST request, Use Fiddler to track HTTp transfers and click on RAW to view raw data being transfered.

Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79