C# HttpListenerException“;参数不正确";

C# HttpListenerException“;参数不正确";,c#,c#-4.0,C#,C# 4.0,我正在尝试使用一些Json数据为HttpListenerResponse提供服务器,但在写入HttpListenerResponse outputstream时,始终会遇到异常 我首先设置响应头,然后设置ContentLength,最后将数据写入输出流 response.StatusCode = endpointResponse.Status; response.ContentType = endpointResponse.ContentType; respo

我正在尝试使用一些Json数据为HttpListenerResponse提供服务器,但在写入HttpListenerResponse outputstream时,始终会遇到异常

我首先设置响应头,然后设置ContentLength,最后将数据写入输出流

      response.StatusCode = endpointResponse.Status;
      response.ContentType = endpointResponse.ContentType;
      response.ContentLength64 = endpointResponse.Payload.Length;
       //response.OutputStream.Write(endpointResponse.Payload, 0, endpointResponse.Payload.Length); // Throws same exception
        using (var output = response.OutputStream)
        {
          using (var writer = new BinaryWriter(output))
          {
            writer.Write(endpointResponse.Payload); // Throws exception here.
            writer.Flush();
          }
        }
        response.OutputStream.Close();
这将导致writer.Write(endpointResponse.Payload)或response.OutputStream.Write(endpointResponse.Payload,0,endpointResponse.Payload.Length)中出现异常

两者都抛出相同的HttpListenerExceptoin,其中包含消息{“参数不正确”}和错误代码87

   at System.Net.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.IO.BinaryWriter.Write(Byte[] buffer)
   at FarmLink.Server.Http.HttpHandler.BuildResponse(HttpListenerResponse response, EndpointResponse endpointResponse) in c:\wc\FarmLink\src\FarmLink.Server.Http\HttpHandler.cs:line 82
   at FarmLink.Server.Http.HttpHandler.<ContextCallback>d__0.MoveNext() in c:\wc\FarmLink\src\FarmLink.Server.Http\HttpHandler.cs:line 56
at System.Net.HttpResponseStream.Write(字节[]缓冲区,Int32偏移量,Int32大小)
在System.IO.BinaryWriter.Write处(字节[]缓冲区)
在c:\wc\FarmLink\src\FarmLink.Server.Http.HttpHandler.BuildResponse(HttpListenerResponse,EndpointResponse-EndpointResponse)中的FarmLink.Server.Http\HttpHandler.cs:第82行
在c:\wc\FarmLink\src\FarmLink.Server.Http.HttpHandler.d\uu 0.MoveNext()中,Http\HttpHandler.cs:第56行

有什么想法吗?

改变代码的顺序,似乎有效。因此,不必在将数据写入OutputStream之前将头写入

    response.ContentLength64 = endpointResponse.Payload.Length;
    response.OutputStream.Write(endpointResponse.Payload, 0, endpointResponse.Payload.Length);
    response.OutputStream.Close();

    foreach (var endpointParameter in endpointResponse.Headers)
    {
        response.AddHeader(endpointParameter.Key, endpointParameter.Value);
    }
    response.StatusCode = endpointResponse.Status;
    response.ContentType = endpointResponse.ContentType;
看起来很有意思


知道为什么吗?

改变代码的顺序,似乎是可行的。因此,不必在将数据写入OutputStream之前将头写入

    response.ContentLength64 = endpointResponse.Payload.Length;
    response.OutputStream.Write(endpointResponse.Payload, 0, endpointResponse.Payload.Length);
    response.OutputStream.Close();

    foreach (var endpointParameter in endpointResponse.Headers)
    {
        response.AddHeader(endpointParameter.Key, endpointParameter.Value);
    }
    response.StatusCode = endpointResponse.Status;
    response.ContentType = endpointResponse.ContentType;
看起来很有意思


知道原因吗?

检查传输编码http头是否已设置。如果是,则不应设置内容长度。否则就会产生上面的错误。我对此表示严重怀疑。我在我的电脑上试过了,它解决了这个错误,但是邮件头并没有被发送出去。事实证明,
内容长度
标题导致了一个问题,删除该标题修复了该问题,因此我不知道问题是什么。无法解释-因此我确定我的“答案”不正确,似乎@Edd有错误的原因。我删除了“正确”标记,但如果传输编码http头已经设置,则无法测试原始codeCheck。如果是,则不应设置内容长度。否则就会产生上面的错误。我对此表示严重怀疑。我在我的电脑上试过了,它解决了这个错误,但是邮件头并没有被发送出去。事实证明,
内容长度
标题导致了一个问题,删除该标题修复了该问题,因此我不知道问题是什么。无法解释-因此我确定我的“答案”不正确,似乎@Edd有错误的原因。我删除了正确的标记,但无法测试原始代码