HTTP Request string

I am trying to do some HTTP via the raw modem communication, which does not support adding headers and stuff. Planning to use .NET Webrequest to generate the complete string of headers and content.

BUT cannot find the method from HttpWebRequest that returns the content string itself - ie. the bytes that is transferred over the socket.

Am I just lousy to look in the documentation, or is this not possible?
EDIT: This is the docs I am looking in: System.Net Namespace | Microsoft Learn

Hi,

could you please me some further details. Do you want to read the content of a response or send something via HTTP? If you want to read the content you have to take the Response andread that with the StreamReader. If you want to send a content then you have to write the Stream. Here you have both implementations: netmf-azure-mobile-services/MobileServiceClient.cs at master · mobernberger/netmf-azure-mobile-services · GitHub

Michael

@ njbuch -

Look at this code and note where I am getting the content length.


HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:36142/api/values");
request.Method = "Post";
request.UserAgent = "netmf";
request.ContentType = "application/json; charset=utf-8";
string body ="{\"Email\":\"tps@ tps.com\",\"PhoneNumber\":\"512-1234\",\"RowKey\":\"20141210:221577\",\"PartitionKey\":\"spencer\"}";
byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
request.ContentLength = bodyBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(bodyBytes, 0, bodyBytes.Length);
WebResponse response = request.GetResponse();
HttpWebResponse hResponse = (HttpWebResponse)response;
stream.Close();


1 Like

@ mobernberger - my question is not clear. I have planned to generate the whole http request by hand. Not possible to extract it from .net

@ terrence - i am sorry. I don’t understand what you want to show.

Just to be clear, this is a HTTP Request:


POST http://hostname/path HTTP/1.1
Host: hostname
Authorization: Basic dkjshafhjGVy
Connection: keep-alive
Content-Length: 404
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Content-Type: */*

{     "eventtype": "AllOK",     "eventtime": "2014-10-10 20:41",     "object": "1111" }


I was hoping the NETMF code could assist in generating one, that I could just ship using a socket.

But its simple to generate, so no problem.

@ njbuch -

Not sure what you exactly want…

Something like this?

@ njbuch

I think I have understood your question …

You have a raw socket and you want to send an HTTP request over it as string that you need to generate yourself because HttpWebRequest can help you to build but doesn’t return the generated string.

The only answer is that you need to build the string yourself of course, using basic string type or StringBuilder class.

In the past I implemented an HttpClient class in my uPLibrary (http://uplibrary.codeplex.com/). It doesn’t use HttpWebRequest and manage raw string to generate the HTTP request and read related response.

Is it what you are searching for ?
Am I near to solve your problem ? :slight_smile:

Paolo.

1 Like

@ ppatierno - This was a goody bag link. Looks very promising! Could not find documentation or examples though…??