2009-11-29 12 views
0

I가 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가 나를 위해 인증을하도록 내버려 둘 것입니다.