Writing HTTP Applications (Internet Pack)
From RemObjects Software
This is a Internet Pack How To entry
Feel free to add your notes to this topic below.
Internet Pack provides complete encapsulation of the HTTP request and response messages by means of the HttpClientRequest/HttpClientResponse and HttpServerRequest/HttpServerResponse classes, which provide access to the HTTP headers that will be sent or have been received, as well as the message body.
Servers
To write a custom HTTP server component, you will descend a class from HttpServer and override it's HandleHttpRequest method. This method will be called - on a worker thread - for each incoming HTTP request, and it will pass you an HttpServerRequest class providing full access to the received HTTP Message, including the Header and the Content
HandleHttpRequest also provides a parameter of type [[HttpServerResponse (Internet Pack)|HttpServerResponse], which you can fill in your code to model the response you want to send back to the client. Again, you have full access to the Header as well as the Content.
A simple Server implementation might look like this:
protected internal override void HandleHttpRequest(...) { base.HandleHttpRequest(aConnection, aRequest, aResponse); if (aRequest.Header.RequestType == "GET") { aResponse.Header.SetHeaderValue("Content-Type","text/html"); aResponse.ContentString = "<p>Hello from Internet Pack</p>"; } else { aResponse.SendError(500, "BAD REQUEST"); } }
In this case, every HTTP GET request to the server will simply be served back a static string of HTML code. Alternatively, you could use the ContentStream property of the aResponse to send back - for example - a file from disk:
aResponse.Header.SetHeaderValue("Content-Type","application/binary"); aResponse.ContentStream = new FileStream(...); aResponse.CloseStream = true; /* Response will close stream once it's been sent */
Clients
For writing a custom HTTP Client, you can usually just use the existing HttpClient class and use it to send requests against the server. HttpClient provides a Dispatch method that will take a HttpClientRequest, send it to the server and return a HttpClientResponse.
These Request and Response objects behave almost exactly like their server counterparts: HttpClientRequest lets you access the Header and provide a Content body to send just like the HttpServerResponse you have seen above; HttpClientResponse provides access to the received header and content body just as the HttpServerRequest does.
For example, a simple client request might look like this:
HttpClientRequest lRequest = new HttpClientRequest(); lRequest.RequestType = RequestType.Post; lRequest.Url = "http://server/login.asp"; lRequest.ContentString = "Username=test\n\rPassword=password"; HttpClientResponse lResponse = lClient.Dispatch(lRequest); MessageBox.Show(lResponse.ContentString);
This code sends a form to the server via HTTP POST, and displays the resulting web page as message box.
Accessing Content
Both the incoming (HttpClientResponse and HttpServerRequest) and outgoing (HttpClientRequest and HttpServerResponse) message classes provide access to the body of the HTTP message in optimized ways.
For convenience, each of them allows you to access the body as Stream, String or byte[], so in most cases you can just pick what ever format suits you best for the operation at hand (just like we did above). However, there are a couple of performance caveats to keep in mind.
Sending Data
When sending data to the remote (as a request from the client, or as response from the server), all three options are pretty much similar, and the choice of what option to use pretty much depends on where your data comes from.
Obviously, if you already have the response you want to send back as String (as we had in the sample above) it makes the most sense to use the ContentString property - if on the other hand you want to send a - potentially large - file form disk, the easiest option is to simply pass the FileStream to ContentStream and let the IP framework worry about the rest.
Note that for performance reasons, the actual data will not be converted between the three different properties - so for example you cannot assign a FileStream, and then expect to access the contents of this stream via the ContentString property (say, to modify it before sending). IP will copy the data directly from the File Stream and send it to the connection once you're ready to send your response off.
Receiving Data
On the receiving side (as a request received on the server, or a response received on the client), the three properties h´behave a b it different. Here you can pick either property to access the received data - depending on your intention with it.
Again, if you're planning on processing the received body as a string - like we did in the last example above - you might as well directly access the ContentString property - which will automatically encode the received data into a new String object. If you are receiving a file to be stored to disk, the best option will be to work with the ContentStream, and use standard Copy Stream loop to read data from the source Stream into a FileStream.
Because ContentSteam provides you with direct access to the underlying Connection Stream, it is the fastest and most efficient way to access the response (opposed to ContentString and ContentBytes, both of which involve copying the entire data from the stream into a memory buffer. Note also, that you can easily use ContentStream to access and process even very large HTTP bodies (such as a huge file download), without the need to copy the entire body into memory, and without having to wait for the entire download to have finished before you can start processing it.
So if you are planning on receiving a large download - say 10 megabytes of data -, ContentStream might not just be the best, but the only feasible option to use - as accessing ContentBytes or ContentString would require to first wait for the entire 10 MB of data to download, and then to load all of them into memory into a one huge byte[].
See Also
Product: RemObjects Internet Pack
Current version: Internet Pack 'Vinci' (2.0)
Lists — Glossary — Features — How To — Components — Tools — Samples — Articles — Architecture — Issues
Categories: Text | Internet Pack | How To | .NET
