Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

c# - Create http request using TcpClient

I have created an empty asp.net web application where I have a simple aspx page:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Hello world");
}

when i goto http://localhost:2006/1.aspx I see a page that says "Hello world".


Ok so on c# if I do:

WebClient webClient = new WebClient() { Proxy = null };
var response2 = webClient.DownloadString("http://localhost:2006/1.aspx");

then response2 == "Hello world"

I need to achieve the same thing with a raw tcp connection

I am trying to achieve the same thing with a tcp connection and for some reason it does not work:

byte[] buf = new byte[1024];
string header = "GET http://localhost:2006/1.aspx HTTP/1.1
" +
                "Host: localhost:2006
" +
                "Connection: keep-alive
" +
                "User-Agent: Mozilla/5.0
" +
                "
";

var client = new TcpClient("localhost", 2006);            

// send request
client.Client.Send(System.Text.Encoding.ASCII.GetBytes(header));

// get response
var i = client.Client.Receive(buf);
var response1 = System.Text.Encoding.UTF8.GetString(buf, 0, i);

here response1 != "Hello Wold". (note I use != meaning NOT equal)

In this example I get a bad request error.


I want to use a tcp connection for learning purposes. I dont understand why the second example does not work. My first reaction was maybe the headers are incorrect so what I did is I launched wireshark in order to see the headers send by my chrom browser. In fact the actual request sent by my browser when I goto http://localhost:2006/1.aspx is:

GET http://localhost:2006/1.aspx HTTP/1.1
Host: localhost:2006
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

I have also tried using that request and when I do so I also get a Bad Request response! Why?

In other words I have replaced

string header = "GET http://localhost:2006/1.aspx HTTP/1.1
" +
                "Host: localhost:2006
" +
                "Connection: keep-alive
" +
                "User-Agent: Mozilla/5.0
" +
                "
";

FOR

string header = "GET http://localhost:2006/1.aspx HTTP/1.1
" +
        "Host: localhost:2006
" +
        "Connection: keep-alive
" +
        "Cache-Control: max-age=0
" +
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
" +
        "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
" +
        "Accept-Encoding: gzip,deflate,sdch
" +
        "Accept-Language: en-US,en;q=0.8" +
        "

";

and it still does not work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is my version. Works fine

    private static async Task<string> HttpRequestAsync()
    {
        string result = string.Empty;

        using (var tcp = new TcpClient("www.bing.com", 80))
        using (var stream = tcp.GetStream())
        {
            tcp.SendTimeout = 500;
            tcp.ReceiveTimeout = 1000;
            // Send request headers
            var builder = new StringBuilder();
            builder.AppendLine("GET /?scope=images&nr=1 HTTP/1.1");
            builder.AppendLine("Host: www.bing.com");
            //builder.AppendLine("Content-Length: " + data.Length);   // only for POST request
            builder.AppendLine("Connection: close");
            builder.AppendLine();
            var header = Encoding.ASCII.GetBytes(builder.ToString());
            await stream.WriteAsync(header, 0, header.Length);

            // Send payload data if you are POST request
            //await stream.WriteAsync(data, 0, data.Length);

            // receive data
            using (var memory = new MemoryStream())
            {
                await stream.CopyToAsync(memory);
                memory.Position = 0;
                var data = memory.ToArray();

                var index = BinaryMatch(data, Encoding.ASCII.GetBytes("

")) + 4;
                var headers = Encoding.ASCII.GetString(data, 0, index);
                memory.Position = index;

                if (headers.IndexOf("Content-Encoding: gzip") > 0)
                {
                    using (GZipStream decompressionStream = new GZipStream(memory, CompressionMode.Decompress))
                    using (var decompressedMemory = new MemoryStream())
                    {
                        decompressionStream.CopyTo(decompressedMemory);
                        decompressedMemory.Position = 0;
                        result = Encoding.UTF8.GetString(decompressedMemory.ToArray());
                    }
                }
                else
                {
                    result = Encoding.UTF8.GetString(data, index, data.Length - index);
                    //result = Encoding.GetEncoding("gbk").GetString(data, index, data.Length - index);
                }
            }

            //Debug.WriteLine(result);
            return result;
        }
    }

    private static int BinaryMatch(byte[] input, byte[] pattern)
    {
        int sLen = input.Length - pattern.Length + 1;
        for (int i = 0; i < sLen; ++i)
        {
            bool match = true;
            for (int j = 0; j < pattern.Length; ++j)
            {
                if (input[i + j] != pattern[j])
                {
                    match = false;
                    break;
                }
            }
            if (match)
            {
                return i;
            }
        }
        return -1;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...