【问题标题】:Unexpected EOF in HTTPS requestHTTPS 请求中出现意外的 EOF
【发布时间】:2009-11-29 21:15:50
【问题描述】:

我有以下使用 Https 请求发送 xml 的函数

public WebResponse SendXmlPost(string url, string xml)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.KeepAlive = false;
    request.ProtocolVersion = HttpVersion.Version11;
    request.Method = "POST";

    if (this.UseBasicAuthentication)
    {
        string usernamePassword = this.Login + ":" + this.Password;
        request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
        NetworkCredential cred = new NetworkCredential(this.Login, this.Password);
        request.Credentials = cred;
    }

    if (this.desactivateCertificateChecking)
    {
        System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        };
    }

    byte[] postBytes = Encoding.UTF8.GetBytes(xml);

    request.ContentType = "text/xml";
    request.ContentLength = postBytes.Length;
    Stream requestStream = request.GetRequestStream(); //error occurs here

    requestStream.Write(postBytes, 0, postBytes.Length);
    requestStream.Close();

    WebResponse response = request.GetResponse();

    return response;
}

当我调用此方法时,出现“意外 EOF”异常:

System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count) +7067555
   System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) +116
   System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) +123
   System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) +7243141
   System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) +217
   System.Threading.ExecutionContext.runTryCode(Object userData) +376

有什么线索吗?

谢谢

【问题讨论】:

    标签: c# https httpwebrequest


    【解决方案1】:

    我认为您的问题是 UTF8 编码。默认情况下,UTF-8 编码会添加一个 BOM (ByteOrderMark),并且您的 ContentLength 与正在传输的数据不同步。尝试创建一个没有 BOM 的 UTF8 编码(使用另一个重载),它应该可以解决问题。

    另外,我自己不会添加 Basic auth 标头。我只需在请求上设置凭据,然后让 HttpWebRequest 为我进行身份验证。

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 1970-01-01
      • 2022-07-08
      • 1970-01-01
      • 2018-04-18
      • 2014-01-01
      • 2012-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多