要使用c#套接字放置的服务器响应



我有以下服务器代码:

class Program
{

static void Main(string[] args)
{
Socket server = new Socket(AddressFamily.InterNetwork, 
SocketType.Stream, ProtocolType.Tcp);
server.Bind(new IPEndPoint(IPAddress.Any, 8000));
server.Listen();

while (true)
{
Socket client = server.Accept();
StreamReader streamReader = new StreamReader(new 
NetworkStream(client));
StringBuilder buffer = new StringBuilder();
string eof = new string(new char[] { (char)10, (char)13, 
(char)10 });
while (!buffer.ToString().Contains(eof))
buffer = buffer.Append((char)streamReader.Read());
Console.WriteLine(buffer);
StreamWriter streamWriter = new StreamWriter(new 
NetworkStream(client));
streamWriter.WriteLine();
streamWriter.Flush();

}
}
static string createResponse()
{
string s = @"
HTTP/1.1 200 OK
Date: " + DateTime.Now + @"
Server: Custom
Connection: keep-alive
Allow: OPTIONS, GET, PUT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods GET, POST, PUT, DELETE, HEAD, OPTIONS
Access-Control-Allow-Headers: *
Access-Control-Allow-Credentials: true";
return s;
}
}

以及以下客户端代码(javascript(:

function connect() {
fetch('http://127.0.0.1:8000', 
{
method: 'put',
headers: {
"Content-Type" : "text/html"
},
body: "Test"
});
}

我在服务器中得到以下输出:

OPTIONS / HTTP/1.1
Host: 127.0.0.1:8000
Connection: keep-alive
Accept: */*
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: content-type
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36
Sec-Fetch-Mode: cors
Sec-Fetch-Site: cross-site
Sec-Fetch-Dest: empty
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7

但我在任何地方都找不到正文,甚至找不到接收到的客户端标头中的内容类型/长度——为什么?我到底做错了什么?在发送服务器头之后,我试图将流位置设置为零(尽管这根本没有意义,因为它是一个网络流(,但不出所料,它抛出了一个错误。HTTP协议究竟是如何与PUT方法相关联地工作的?为了在服务器和客户端之间建立正确的交换,服务器要提供什么?

感谢上帝,这里有解决方案:

class Program
{
static void Main(string[] args)
{
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(new IPEndPoint(IPAddress.Any, 8000));
server.Listen(int.MaxValue);

while (true)
{
Socket client = server.Accept();
StreamReader streamReader = new StreamReader(new NetworkStream(client));
StringBuilder buffer = new StringBuilder();
string eof = new string(new char[] { (char)10, (char)13, (char)10 });
while (!buffer.ToString().Contains(eof))
buffer = buffer.Append((char)streamReader.Read());

StreamWriter streamWriter = new StreamWriter(new NetworkStream(client));
streamWriter.WriteLine(createCORSHeader());
streamWriter.Flush();

int contentLength = 0;
var lines = buffer.ToString().Split('n');
for (int i = 0; i < lines.Length; i++)
{
var keyValuePair = lines[i].Split(':');
if (keyValuePair.Length < 2)
continue;
var key = keyValuePair[0];
var val = keyValuePair[0 + 1].Substring(1, keyValuePair[1].Length - 1);
if (key.Equals("Content-Length"))
contentLength = int.Parse(val);
}

buffer.Clear();
for (int i = 0; i < contentLength; i++)
buffer.Append((char)streamReader.Read());

File.AppendAllText("protokoll.txt", "[" + DateTime.Now + "]t" + buffer.ToString() + Environment.NewLine);

streamWriter.WriteLine(createPUTResponse());
streamWriter.Flush();

client.Close();
Console.WriteLine("Done.");
}
}
static string createCORSHeader()
{
string s = @"
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT
Access-Control-Allow-Headers: X-PINGOTHER, Content-Type
Access-Control-Max-Age: 86400
Vary: Accept-Encoding, Origin
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
";
return s;
}

static string createPUTResponse()
{
string s = @"
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT
Access-Control-Allow-Headers: X-PINGOTHER, Content-Type
Access-Control-Max-Age: 86400
Vary: Accept-Encoding, Origin
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
";
return s;
}
}

相关内容

  • 没有找到相关文章

最新更新