2011-09-02 40 views
5

Giúp tôi. Sau khi gửi truy vấn bài viết tôi có ngoại lệ "Lỗi khi nhận luồng phản hồi (ReadDone2): Nhận lỗi". giúp loại bỏ lỗi này. cảm ơn.Lỗi khi nhận luồng phản hồi (ReadDone2): Nhận lỗi

đoạn mã

try 
{ 
string queryContent = string.Format("login={0}&password={1}&mobileDeviceType={2}/", 
login, sessionPassword, deviceType); 
request = ConnectionHelper.GetHttpWebRequest(loginPageAddress, queryContent); 

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())//after this line //occurs exception - "Error getting response stream (ReadDone2): Receive Failure" 
{ 

ConnectionHelper.ParseSessionsIdFromCookie(response); 

string location = response.Headers["Location"]; 
if (!string.IsNullOrEmpty(location)) 
{ 
string responseUri = Utils.GetUriWithoutQuery(response.ResponseUri.ToString()); 
string locationUri = Utils.CombineUri(responseUri, location); 
result = this.DownloadXml(locationUri); 
} 
response.Close(); 
} 
} 
catch (Exception e) 
{ 
errorCout++; 
errorText = e.Message; 
} 

// Methot GetHttpWebRequest

public static HttpWebRequest GetHttpWebRequest(string uri, string queryContent) 
    { 
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);    
     request.Proxy = new WebProxy(uri); 
     request.UserAgent = Consts.userAgent; 
     request.AutomaticDecompression = DecompressionMethods.GZip; 
     request.AllowWriteStreamBuffering = true; 
     request.AllowAutoRedirect = false; 

     string sessionsId = GetSessionsIdForCookie(uri); 
     if (!string.IsNullOrEmpty(sessionsId)) 
      request.Headers.Add(Consts.headerCookieName, sessionsId); 

     if (queryContent != string.Empty) 
     { 
      request.ContentType = "application/x-www-form-urlencoded"; 
      request.Method = "POST"; 
      byte[] SomeBytes = Encoding.UTF8.GetBytes(queryContent); 
      request.ContentLength = SomeBytes.Length; 
      using (Stream newStream = request.GetRequestStream()) 
      { 
       newStream.Write(SomeBytes, 0, SomeBytes.Length); 
      } 
     } 
     else 
     { 
      request.Method = "GET"; 
     } 

     return request; 
    } 
+0

Bạn có thể gửi mã cho lớp ConnectionHelper (hoặc có thể chỉ là phương pháp GetHttpWebRequest)? – clarkb86

Trả lời

0
using (Stream newStream = request.GetRequestStream()) 
{ 
    newStream.Write(SomeBytes, 0, SomeBytes.Length); 

    //try to add 
    newStream.Close(); 
} 
+1

Có cần phải gọi hàm Close() một cách rõ ràng khi sử dụng từ khoá 'sử dụng' không? Tôi nghĩ rằng luồng được tự động xử lý/đóng lại khi nó nằm ngoài phạm vi của câu lệnh 'sử dụng'. –

+0

Tôi cũng nghĩ như vậy, nhưng trên thực tế mà không có .Đóng() nó sẽ không gửi yêu cầu. – mironych

0

Trong trường hợp của tôi máy chủ đã không gửi nội dung phản hồi. Sau khi sửa chữa máy chủ, "Nhận thất bại" biến mất.

Vì vậy, bạn có hai lựa chọn:

  1. Đừng yêu cầu một luồng phản ứng, nếu bạn có thể sống mà không có nó.

  2. Đảm bảo máy chủ gửi nội dung phản hồi.

    Ví dụ: thay vì

    self.send_response(200) 
    self.wfile.close() 
    

    mã máy chủ Python nên

    self.send_response(200) 
    self.send_header('Content-type', 'text/plain') 
    self.end_headers() 
    self.wfile.write("Thanks!\n") 
    self.wfile.close() 
    
Các vấn đề liên quan