HttpWebRequest 抛出 System.Net.WebException Section=ResponseStatusLine

发布于 2024-12-28 17:20:56 字数 1975 浏览 2 评论 0原文

我编写了一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神魇的王 2025-01-04 17:20:56

嗯,一开始就很可疑:

_output = new StreamWriter(_stream, Encoding.UTF8) { NewLine = "\n\r" };

我认为您的意思是 \r\n 而不是 \n\r

Well this is very suspect to start with:

_output = new StreamWriter(_stream, Encoding.UTF8) { NewLine = "\n\r" };

I think you meant \r\n rather than \n\r.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文