locked
Can you send HttpRequests on a StreamSocket?

    Question

  • Hello,

    I have client that needs to be able to poll a web server in the background. I've looked at the ControlChannelTrigger StreamSocket example and it looks like what i want.

    I need to have a persistent connection to the web server and send http requests down it. I don't want to go through the hassle of writing my own HTTP client, so is there a way i can construct my HTTP request using the HttpRequestMessage class then send it down the StreamSocket? (doing the same with the response)

    I don't want to send the message using the HttpClient method because i want to keep the socket open and reuse it, plus i need to use SSL/TLS in the background. According to the documentation, you cannot use SSL in the background with a HttpClient.

    Any ideas how i can acheive this?

    Thursday, April 24, 2014 9:03 AM

Answers

  • I don't think that what you want is possible.  You can use a background task to make short-lived HTTP requests to a web server. What's the point of a constantly-open connection to a web server anyway?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, April 24, 2014 2:38 PM
    Moderator

All replies

  • I don't think that what you want is possible.  You can use a background task to make short-lived HTTP requests to a web server. What's the point of a constantly-open connection to a web server anyway?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, April 24, 2014 2:38 PM
    Moderator
  • The point is that its a real-time system so requires one. Doing it with HttpClient is very inefficient, as it we need constantly disconnect and reconnect, meaning it effects battery life.

    I cannot see any information in the documentation that states you cannot send a HTTP message down a StreamSocket. When I manually sent 

    GET /index.html HTTP/1.1
    Host: localhost

    down the stream socket, nothing was ever put onto the wire (tested using wireshark), but if I put a zero byte (0x00) on the beginning, making a it a malformed http message it did get put onto the wire.

    It appeared that there was something blocking the outgoing data if was a HTTP messgae.

    So why is not possible to send a raw HTTP message down a StreamSocket?

    Sample

    StreamSocket socket; // Initialised and set up else where in the code. .... public async void SendMessageHelper(string message) { #if DEBUG Diag.DebugPrint("NcsHttpClient : SendMessageHelper", Diag.LogLevel.TRACE); #endif try { if (writePacket == null) { writePacket = new DataWriter(socket.OutputStream); }

    writePacket.WriteString(message); Diag.DebugPrint("NcsHttpClient : SendMessageHelper - sending message: " + message, Diag.LogLevel.VERBOSE); await writePacket.StoreAsync(); Diag.DebugPrint("NcsHttpClient : SendMessageHelper - Message Stored", Diag.LogLevel.VERBOSE); await writePacket.FlushAsync(); Diag.DebugPrint("NcsHttpClient : SendMessageHelper - Message Flusheds", Diag.LogLevel.VERBOSE); } catch (Exception exp) { Diag.DebugPrint("NcsHttpClient : SendMessageHelper - Socket write failed with error: " + exp.Message, Diag.LogLevel.ERROR); } } .... void TestSendMessage() { // Not sent on wire SendMessageHelper("GET / HTTP/1.1\r\nHost:localhost:8080\r\n" ); // Sent on wire SendMessageHelper("\0GET / HTTP/1.1\r\nHost:localhost:8080\r\n" ); }


    Thursday, August 14, 2014 9:01 AM