2010-11-12 38 views
9

trong dự án Silverlight-Windows Phone 7 Tôi đang tạo HttpWebRequest, nhận RequestStream, viết nội dung nào đó vào Luồng và cố gắng nhận phản hồi, nhưng tôi luôn nhận được NotSupportedException : "System.Net.Browser.OHWRAsyncResult.AsyncWaitHandle ném một ngoại lệ của loại 'System.NotSupportedException'HttpWebRequest.EndGetResponse ném NotSupportedException trong Windows Phone 7

mã sản xuất của tôi là xa phức tạp hơn, nhưng tôi có thể thu hẹp nó xuống mảnh nhỏ mã này:

public class HttpUploadHelper 
{ 
    private HttpWebRequest request; 
    private RequestState state = new RequestState(); 

    public HttpUploadHelper(string url) 
    { 
     this.request = WebRequest.Create(url) as HttpWebRequest; 
     state.Request = request; 
    } 

    public void Execute() 
    { 
     request.Method = "POST"; 
     this.request.BeginGetRequestStream(
      new AsyncCallback(BeginRequest), state); 
    } 

    private void BeginRequest(IAsyncResult ar) 
    { 
     Stream stream = state.Request.EndGetRequestStream(ar); 
     state.Request.BeginGetResponse(
      new AsyncCallback(BeginResponse), state); 
    } 

    private void BeginResponse(IAsyncResult ar) 
    { 
     // BOOM: NotSupportedException was unhandled; 
     // {System.Net.Browser.OHWRAsyncResult} 
     // AsyncWaitHandle = 'ar.AsyncWaitHandle' threw an 
     // exception of type 'System.NotSupportedException' 
     HttpWebResponse response = state.Request.EndGetResponse(ar) as HttpWebResponse; 
     Debug.WriteLine(response.StatusCode); 
    } 
} 

public class RequestState 
{ 
    public WebRequest Request; 
} 

}

Có ai biết điều gì sai với mã này không?

Trả lời

3

Vấn đề là cách bạn xử lý việc truy cập các yêu cầu ban đầu trong cuộc gọi lại từ BeginGetResponse.

Thay vì giữ một tham chiếu ot nhà nước, có được một tài liệu tham khảo về các yêu cầu ban đầu với:

var request = (HttpWebRequest)asynchronousResult.AsyncState; 

Hãy nhìn vào ví dụ này rất cơ bản (nhưng làm việc) thực hiện đăng nhập bằng cách đăng email và thông tin đăng nhập mật khẩu cho trang web.

public static string Email; 

public static string Password; 


private void LoginClick(object sender, RoutedEventArgs e) 
{ 
    Email = enteredEmailAddress.Text.Trim().ToLower(); 

    Password = enteredPassword.Password; 

    var request = (HttpWebRequest)WebRequest.Create(App.Config.ServerUris.Login); 

    request.ContentType = "application/x-www-form-urlencoded"; 
    request.Method = "POST"; 
    request.BeginGetRequestStream(ReadCallback, request); 
} 

private void ReadCallback(IAsyncResult asynchronousResult) 
{ 
    var request = (HttpWebRequest)asynchronousResult.AsyncState; 

    using (var postStream = request.EndGetRequestStream(asynchronousResult)) 
    { 
     using (var memStream = new MemoryStream()) 
     { 
      var content = string.Format("Password={0}&Email={1}", 
             HttpUtility.UrlEncode(Password), 
             HttpUtility.UrlEncode(Email)); 

      var bytes = System.Text.Encoding.UTF8.GetBytes(content); 

      memStream.Write(bytes, 0, bytes.Length); 

      memStream.Position = 0; 
      var tempBuffer = new byte[memStream.Length]; 
      memStream.Read(tempBuffer, 0, tempBuffer.Length); 

      postStream.Write(tempBuffer, 0, tempBuffer.Length); 
     } 
    } 

    request.BeginGetResponse(ResponseCallback, request); 
} 

private void ResponseCallback(IAsyncResult asynchronousResult) 
{ 
    var request = (HttpWebRequest)asynchronousResult.AsyncState; 

    using (var resp = (HttpWebResponse)request.EndGetResponse(asynchronousResult)) 
    { 
     using (var streamResponse = resp.GetResponseStream()) 
     { 
      using (var streamRead = new StreamReader(streamResponse)) 
      { 
       string responseString = streamRead.ReadToEnd(); 

       // do something with responseString to check if login was successful 
      } 
     } 
    } 
} 
+0

Xin chào Matt, tôi đang gặp phải sự cố tương tự. Xin vui lòng giúp đỡ, http://stackoverflow.com/questions/13598666/how-to-fix-system-argumentexception-in-httpwebresponse –

-1

Thay đổi này:

state.Request.BeginGetResponse(
     new AsyncCallback(BeginResponse), state); 

Để này:

state.Request.BeginGetResponse(BeginResponse, state); 
+0

Không, "mới AsyncCallback()" không có gì trong trường hợp này, nó có thể được bỏ qua nói chung. –

22

Các NotSupportedException có thể được ném ra khi dòng yêu cầu không được đóng trước khi cuộc gọi đến EndGetResponse. Luồng WebRequest vẫn mở và gửi dữ liệu đến máy chủ khi bạn đang cố gắng nhận phản hồi. Kể từ khi dòng cài đặt giao diện IDisposable, một giải pháp đơn giản là quấn mã của bạn bằng cách sử dụng dòng yêu cầu trong một khối using:

private void BeginRequest(IAsyncResult ar) 
{ 
    using (Stream stream = request.EndGetRequestStream(ar)) 
    { 
     //write to stream in here. 
    } 
    state.Request.BeginGetResponse(
     new AsyncCallback(BeginResponse), state); 
} 

Việc sử dụng khối sẽ đảm bảo rằng các dòng được đóng trước khi bạn cố gắng để có được những phản hồi từ máy chủ web.

1

NotSupportedException cũng có thể được ném khi string url quá dài. Tôi đã sai lầm và đính kèm dữ liệu đăng lên url thay vì bài đăng. Khi dữ liệu ngắn, nó hoạt động tốt, nhưng khi nó tăng quá lớn - EndGetResponse bị lỗi.