Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HTTPS请求中的意外EOF_C#_Https_Httpwebrequest - Fatal编程技术网

C# HTTPS请求中的意外EOF

C# HTTPS请求中的意外EOF,c#,https,httpwebrequest,C#,Https,Httpwebrequest,我有以下使用Https请求发送xml的函数 public WebResponse SendXmlPost(string url, string xml) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.KeepAlive = false; request.ProtocolVersion = HttpVersion.Version11; request.Method

我有以下使用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
有线索吗


谢谢

我想你的问题是UTF8编码。默认情况下,UTF-8编码会添加BOM字节顺序标记,并且您的ContentLength与传输的数据不同步。尝试在不使用BOM的情况下创建UTF8编码,并使用另一个重载,这样可以解决问题

此外,我自己也不会添加基本的auth头。我只需要在请求上设置crdentials,然后让HttpWebRequest为我进行身份验证