我尝试做的是使用TcpClient通过HTTP协议发送数据。但我在这里面临一个问题。我发送的数据(在标题之后)没有完全发送到客户端的应用程序(例如浏览器)。代码:
// Building data.
string notfound = "NOT FOUND";
byte[] data = Encoding.UTF8.GetBytes(notfound);
// Building header.
m_responseBuilder.Append("HTTP/1.1 404 Not Found").Append(CRLF);
m_responseBuilder.Append("Server: ETP").Append(CRLF);
m_responseBuilder.Append("Content-Type: text/plain").Append(CRLF);
m_responseBuilder.Append("Content-Length: ").Append(data.Length.ToString()).Append(CRLF);
m_responseBuilder.Append(CRLF);
byte[] header = Encoding.UTF8.GetBytes(m_responseBuilder.ToString());
// Sending header.
await SendDataAsync(header);
// Sending data.
await SendDataAsync(data);
SendDataAsync:
async Task SendDataAsync(byte[] data)
{
try
{
await m_tcpchannel.WriteAsync(data, 0, data.Length);
}
catch
{
DestroyConnection();
}
}
我得到了什么:
NOT FOUN
这里错了什么?
答案 0 :(得分:0)
是的,我遇到了问题。这是CRLF。我写了\ n \ r而不是\ r \ n ....