gpt4 book ai didi

c# - 使用 TcpClient 创建 http 请求

转载 作者:可可西里 更新时间:2023-11-01 02:30:58 28 4
gpt4 key购买 nike

我创建了一个空的 asp.net web 应用程序,其中有一个简单的 aspx 页面:

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

当我转到 http://localhost:2006/1.aspx 时,我看到一个页面显示“Hello world”。


好吧,如果我这样做了,那么在 c# 上:

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

然后 response2 == "Hello world"

我需要用原始 tcp 连接实现同样的事情

我正在尝试通过 tcp 连接实现相同的目的,但由于某种原因它不起作用:

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

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);

这里是 response1 != “Hello Wold”。 (注意我使用 != 表示不等于)

在这个例子中,我得到一个错误的请求错误。


我想将 tcp 连接用于学习目的。我不明白为什么第二个例子不起作用。我的第一 react 可能是 header 不正确,所以我启动了 wireshark 以查看由我的 chrom 浏览器发送的 header 。事实上,当我转到 http://localhost:2006/1.aspx 时,我的浏览器发送的实际请求是:

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

我也尝试过使用该请求,当我这样做时,我也收到了 Bad Request 响应!为什么?

换句话说我已经替换了

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

为了

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

还是不行。

最佳答案

这是我的版本。工作正常

    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("\r\n\r\n")) + 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;
}

关于c# - 使用 TcpClient 创建 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19523088/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com