Archived Forum Post

Index of archived forum posts

Question:

HTTP POST Example, the other end in C#...?

Nov 10 '12 at 09:22

There is a simple but very clear and easy example of using the POST command with the HTTP libary here:

[http://www.example-code.com/vcpp/http_post_simple.asp][1]

It is exactly what I need. But I was wondering if there was an equally simple example in C# of how to implement the server side. I know that that is not a ChilKat job, but maybe someone has some code or a link lying around.....


Answer

HTTP requests and responses have the same format. They are composed of a "start line" followed by MIME. (For simplicity, I'm omitting the possibility of chunked responses.)

The "start line" of an HTTP request is a line such as:

GET /test/something.html HTTP/1.0
It has 3 parts: the HTTP method (GET, POST, PUT, etc.), the resource URI, and the HTTP protocol version. Following the start line is the MIME header, followed by the MIME body.

A server would consume an incoming HTTP request by reading the incoming data on the TCP socket (or possibly SSL/TLS connection) in the following way:
1) Consume the start line by reading until the first CRLF.
2) Consume the HTTP request header by reading until the first double CRLF.
3) If the HTTP request is GET, HEAD, or possibly some others, then the MIME body is assumed to be empty, and you are done. Perhaps the next HTTP request might be arriving on the same connection..
4) If a MIME body is to follow (and the MIME body might actually be a multi-part MIME message with nested multi-part MIME messages), then the total size of the entire MIME body is indicated in the "Content-Length" header field. The server should read this exact number of bytes to consume the full MIME body. The MIME body may include binary data, so it's best to read this into a byte array -- NOT a string.
5) Now that the server has both MIME header and body, compose the full MIME message in a byte array (the header followed by body) and then use the Chilkat MIME API to access the content of the MIME (you can get header fields, body content, navigate to MIME sub-parts, etc.)
6) The server's response would be a start line followed by MIME.

I think you probably get the general idea. I would highly recommend the O'Reilly book "HTTP: The Definitive Guide" as both a guide and reference if you're going to implement either the server-side or client-side of the HTTP protocol directly.

PS> If your server-side is custom-built to handle requests from a specific application, where the format of the requests are very controlled, and complexities can be avoided, then it's probably possible to roll-your-own HTTP server to handle the requests. However, writing a more general HTTP server is no small undertaking.


Answer

This was the wrong place to ask I expect, but thanks for the detailed reply.


Answer

Anyway, all you need to do is create a Azure Cloud solution containing an MVC4 project in VC2010 and much of the code is already there, with the default routing from arriving URI to C# function in a C# file called ValuesControllers.cs

Inside that there are some already defined sample functions, and one of them is:

    // POST api/values
    public void Post([FromBody]string value)
    {
    }

Upload the MVC program as a cloud package to Azure and you can test it from you MFC C++ Exe.