3

I apologise if this is a bit dim but I want to send something like this over an sslstream to a server that I have acquired a secure socket connection to...

GET /F5Check/qpi/1 HTTP/1.1
Host: *****.com
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

I tried using a string builder

var sb = new StringBuilder();
sb.AppendLine("GET /F5Check/qpi/1 HTTP/1.1");
sb.AppendLine(string.Format("Host: {0}", host));
sb.AppendLine("Connection: keep-alive");
sb.AppendLine("User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5");
sb.AppendLine("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
sb.AppendLine("Accept-Encoding: gzip,deflate,sdch");
sb.AppendLine("Accept-Language: en-US,en;q=0.8");
sb.AppendLine("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3");

and then doing this

//Translate the data.
byte[] sendBuffer = UTF8Encoding.UTF8.GetBytes(sb.ToString());

//Send the data.
requestStream.Write(sendBuffer);

where sb is the stringbuilder object!

I am simply not getting any response from the server when I try to read from the stream so clearly the server can't make any sense of my dumbass request!

I know I could use variations of webrequest to do the same thing but there is a specific reason why I am trying to do this...

Basically I need to know what to encode for sending so that I can effect a get request?

Dave Lawrence
  • 3,843
  • 2
  • 21
  • 35
  • 1
    What's wrong with HttpWebRequest? (besides you could use WebClient or HttpClient) – dtb Jun 08 '12 at 12:03
  • Implementing HTTP is not a piece of cake. You need to read the specification to send the right bits and pieces. Take a look here: http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging http://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics http://tools.ietf.org/html/draft-ietf-httpbis-p3-payload – dtb Jun 08 '12 at 12:05
  • I know it's not a piece of cake... If it was; I wouldn't be asking it here ;-) – Dave Lawrence Jun 08 '12 at 13:23
  • Can you include the code that you're using to construct the request in the StringBuilder? – Iridium Jun 08 '12 at 13:51
  • Yes. I'll do that now.. should have done that as well – Dave Lawrence Jun 08 '12 at 14:03
  • Why aren't you using HttpWebRequest over https? If you don't like the way HttpWebRequest raises exceptions even 304s, you can use the REST friendly HttpClient available from NuGet for .NET 4.0 – Panagiotis Kanavos Jun 13 '12 at 12:57
  • BTW how do you listen for server responses? – Panagiotis Kanavos Jun 13 '12 at 12:58

3 Answers3

3

I second the needing an extra line break according to HTTP standards. Simply expanding on what Tim had, here's some source code. I went ahead and used the SslStream class for connecting, I'm assuming that's what you did as well. I tested this against Google's HTTPS site, and got back a response. Hope this is enough to get you back on track.

NOTE : I changed the UA String because our proxy for some reason here hated that one. I don't believe that to be your problem, but it was an issue for me.

static void Main(string[] args)
    {

        string host = "encrypted.google.com";

        // Connect socket
        TcpClient client = new TcpClient(host,443);
        NetworkStream stream = client.GetStream();

        // Wrap in SSL stream
        SslStream sslStream = new SslStream(stream);
        sslStream.AuthenticateAsClient(host);

        //Build request
        var sb = new StringBuilder();

        sb.AppendLine("GET / HTTP/1.1");
        sb.AppendLine(string.Format("Host: {0}", host));
        sb.AppendLine("Connection: keep-alive");
        sb.AppendLine("User-Agent: CSharp");
        sb.AppendLine("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        sb.AppendLine("Accept-Encoding: gzip,deflate,sdch");
        sb.AppendLine("Accept-Language: en-US,en;q=0.8");
        sb.AppendLine("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3");
        sb.AppendLine();

        //Go go go!
        byte[] headerBytes = Encoding.UTF8.GetBytes(sb.ToString());
        sslStream.Write(headerBytes, 0, headerBytes.Length);
        sslStream.Flush();


        //Get a place to put it
        byte[] buffer = new byte[2048];
        int bytes;

        //Answer
        do
        {
            bytes = sslStream.Read(buffer, 0, buffer.Length);
            Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
        } while (bytes != 0);

        //And done
        client.Close();
        Console.ReadKey();


    }
Breland
  • 183
  • 1
  • 11
3

A Shot in the dark:

Try setting the request's header Connection to "close".

Totero
  • 2,524
  • 20
  • 34
2

One thing you seem to be missing is the blank line at the end of the request. The HTTP specification requires a blank line to separate the header fields from the message body, or to signify the end of a GET request.

As it stands, the server doesn't know your request is finished so it would be waiting for the next line. Adding an extra sb.AppendLine() should fix that.

Tim Rogers
  • 21,297
  • 6
  • 52
  • 68