2009-11-29 95 views
0

我有以下的功能,使用HTTPS請求意外EOF

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; 
} 

發送XML當我調用此方法,我已經和「意外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 

任何線索?

謝謝

回答

0

我認爲你的問題是UTF8編碼。默認情況下,UTF-8編碼添加BOM(ByteOrderMark),並且ContentLength與正在傳輸的數據不同步。嘗試創建一個沒有BOM的UTF8編碼(使用另一個過載),它應該解決這個問題。

此外,我不會自己添加基本身份驗證標頭。我只需在請求中設置crdentials,並讓HttpWebRequest爲我執行身份驗證。