Tôi có đoạn mã sau:ứng dụng WP7 không bao giờ thoát BeginGetResponse và đi vào hàm callback
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
Stream postStream = request.EndGetRequestStream(asynchronousResult);
//Console.WriteLine("Please enter the input data to be posted:");
//string postData = Console.ReadLine();
string postData = "my data";
// Convert the string into a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Write to the request stream.
postStream.Write(byteArray, 0, postData.Length);
postStream.Close();
// Start the asynchronous operation to get the response
IAsyncResult result =
(IAsyncResult)request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
Console.WriteLine(responseString);
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
allDone.Set();
Dispatcher.BeginInvoke((Action)(() => Debug.WriteLine("George")));
}
Tuy nhiên khi mã của tôi chạm BeginGetResponse nó không bao giờ thoát ra (và tôi không đánh một breakpoint trong hàm GetResponseCallback). Tôi đã thử thêm lời gọi BeginInvoke, nhưng tôi vẫn chưa bao giờ nhập phương thức này. Mã này hoạt động trong ứng dụng giao diện điều khiển cửa sổ - trên Windows Phone 7 mà nó không có nghĩa là
Có ai có thể nhìn thấy những gì tôi đang làm không?
Cảm ơn.
Tôi không thể nhìn thấy nơi bạn đang tạo HttpWebRequest –