HttpWebRequest 抛出 System.Net.WebException Section=ResponseStatusLine
我编写了一个简单的 httpserver,我计划与 HttpRequest/Response 进行交互。服务器收到请求并且解析正常,但是当我写回时,我得到异常,
System.Net.WebException: Der Server hat eine Protokollverletzung ausgeführt. Section=ResponseStatusLine
bei System.Net.HttpWebRequest.GetResponse()
我的服务器代码写回
TcpClient client = ...;
_stream = _client.GetStream();
// make sure "\r\n" is used as line ending, because it may be different on different OS
_output = new StreamWriter(_stream, Encoding.UTF8) { NewLine = "\r\n" };
length = 0;
code = 200;
message = "OK";
_output.WriteLine("HTTP/1.1 " + code + " " + message);
_output.WriteLine("Server: My Server");
_output.WriteLine("ContentLength: " + length);
_output.WriteLine("Connection: " + (_keepAlive ? "Keep-Alive" : "Close"));
_output.WriteLine();
_output.Flush();
客户端代码
var request = (HttpWebRequest)WebRequest.Create("http://127.0.0.1:8080/foo");
request.KeepAlive = false;
request.ServicePoint.Expect100Continue = false;
using (var res = request.GetResponse()) // throws here
{
}
,你知道吗?
更新:正如 Jon 提到的,第一个错误已被删除,但仍然无法正常工作,
备用客户端代码
using (var client = new TcpClient())
{
client.Connect(new IPEndPoint(IPAddress.Loopback, 8080));
using (var stream = client.GetStream())
{
var writer = new StreamWriter(stream);
writer.WriteLine("GET /foo HTTP/1.1");
writer.WriteLine("HOST:127.0.0.1");
writer.WriteLine("Connection:Close");
writer.WriteLine();
writer.Flush();
var response = new StreamReader(stream).ReadToEnd();
foreach (var c in response)
{
if (char.IsControl(c))
Console.Write("'{0}'" , (int)c);
else
Console.Write(c);
}
}
}
返回
HTTP/1.1 200 OK'13''10'Server: My custom Server'13''10'ContentLength: 0'13''10'Connection: Close'13''10''13''10'
I wrote a simple httpserver which i plan to interact with with HttpRequest/Response. The server gets the request and the parsing is fine but when i write back i get exception
System.Net.WebException: Der Server hat eine Protokollverletzung ausgeführt. Section=ResponseStatusLine
bei System.Net.HttpWebRequest.GetResponse()
my servercode which writes back
TcpClient client = ...;
_stream = _client.GetStream();
// make sure "\r\n" is used as line ending, because it may be different on different OS
_output = new StreamWriter(_stream, Encoding.UTF8) { NewLine = "\r\n" };
length = 0;
code = 200;
message = "OK";
_output.WriteLine("HTTP/1.1 " + code + " " + message);
_output.WriteLine("Server: My Server");
_output.WriteLine("ContentLength: " + length);
_output.WriteLine("Connection: " + (_keepAlive ? "Keep-Alive" : "Close"));
_output.WriteLine();
_output.Flush();
client code
var request = (HttpWebRequest)WebRequest.Create("http://127.0.0.1:8080/foo");
request.KeepAlive = false;
request.ServicePoint.Expect100Continue = false;
using (var res = request.GetResponse()) // throws here
{
}
any idea?
Update: as Jon mentioned first bug removed but still not working
alternate client code
using (var client = new TcpClient())
{
client.Connect(new IPEndPoint(IPAddress.Loopback, 8080));
using (var stream = client.GetStream())
{
var writer = new StreamWriter(stream);
writer.WriteLine("GET /foo HTTP/1.1");
writer.WriteLine("HOST:127.0.0.1");
writer.WriteLine("Connection:Close");
writer.WriteLine();
writer.Flush();
var response = new StreamReader(stream).ReadToEnd();
foreach (var c in response)
{
if (char.IsControl(c))
Console.Write("'{0}'" , (int)c);
else
Console.Write(c);
}
}
}
returns
HTTP/1.1 200 OK'13''10'Server: My custom Server'13''10'ContentLength: 0'13''10'Connection: Close'13''10''13''10'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,一开始就很可疑:
我认为您的意思是
\r\n
而不是\n\r
。Well this is very suspect to start with:
I think you meant
\r\n
rather than\n\r
.