当我使用 HttpWebRequest 和 HttpWebResponse 时,我无法停止 UI 线程,当它调用 WaitOne() 时,它也会挂起
void method1()
{
url = "http://192.168.5.22/mobile/testpost.php" ;
request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = true;
//// request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6";
RequestState myRequestState = new RequestState();
myRequestState.Request = request;
IAsyncResult result = request.BeginGetResponse(new AsyncCallback(FinaliseAsyncRequest), myRequestState);
event_2.WaitOne();
String s = myRequestState.responseAsString;
String g = s;
}
private void FinaliseAsyncRequest(IAsyncResult AsyncResult)
{
RequestState myrequestobj = (RequestState)AsyncResult.AsyncState;
HttpWebRequest myrequest = myrequestobj.Request;
response = (HttpWebResponse)myrequest.EndGetResponse(AsyncResult);
string test1;
test1 = ((HttpWebResponse)response).StatusDescription;
if (response.StatusCode == HttpStatusCode.OK)
{
// Create the stream, encoder and reader.
Stream responseStream = response.GetResponseStream();
var x =response.GetResponseStream();
//Encoding streamEncoder = Encoding.UTF8;
StreamReader responseReader = new StreamReader(responseStream);
responseAsString = responseReader.ReadToEnd();
myrequestobj.responseAsString = responseAsString;
}
else
{
throw new Exception(String.Format("Response Not Valid {0}", response.StatusCode));
}
event_2.Set();
}
void method1()
{
url = "http://192.168.5.22/mobile/testpost.php" ;
request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = true;
//// request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6";
RequestState myRequestState = new RequestState();
myRequestState.Request = request;
IAsyncResult result = request.BeginGetResponse(new AsyncCallback(FinaliseAsyncRequest), myRequestState);
event_2.WaitOne();
String s = myRequestState.responseAsString;
String g = s;
}
private void FinaliseAsyncRequest(IAsyncResult AsyncResult)
{
RequestState myrequestobj = (RequestState)AsyncResult.AsyncState;
HttpWebRequest myrequest = myrequestobj.Request;
response = (HttpWebResponse)myrequest.EndGetResponse(AsyncResult);
string test1;
test1 = ((HttpWebResponse)response).StatusDescription;
if (response.StatusCode == HttpStatusCode.OK)
{
// Create the stream, encoder and reader.
Stream responseStream = response.GetResponseStream();
var x =response.GetResponseStream();
//Encoding streamEncoder = Encoding.UTF8;
StreamReader responseReader = new StreamReader(responseStream);
responseAsString = responseReader.ReadToEnd();
myrequestobj.responseAsString = responseAsString;
}
else
{
throw new Exception(String.Format("Response Not Valid {0}", response.StatusCode));
}
event_2.Set();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HttpWebRequest 方法似乎将其部分工作委托给 UI 线程。以下是我遇到的死锁情况的堆栈跟踪的一部分:
请注意 BeginGetResponse 之后的 BeginOnUI。 “异步”回调实际上与 UI 线程同步,因此当 UI 线程被阻塞时会挂起。 HttpWebResponse 方法可能会执行相同类型的同步。
HttpWebRequest methods seem to be delegating part of their work to the UI thread. Here is a part of a stack trace from a deadlock situation I had:
Note the BeginOnUI after the BeginGetResponse. The 'asynchronous' callback is actually synchronized with the UI thread and therefore hangs when the UI thread is blocked. HttpWebResponse methods might be doing the same kind of synchronization.