如何同步等待 HttpWebRequest 回调方法得到响应
How to wait Synchronously for HttpWebRequest callback Method to get a response
public void RemoteInsert()
{
String dataSet = "dataset=" + json.ToString();
accessKey = mobRemoteDB.accessKey;
String paramaters = "?llcommand=sndmsg&" + "ak=" + accessKey + "&command=remoteinsert&" + dataSet;
String url = mobRemoteDB.baseUrlForRemoteInsert + paramaters;
HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
if (NetworkInterface.GetIsNetworkAvailable() & IsDataValid)
{
webRequest.BeginGetRequestStream(newAsyncCallback(GetRequestStreamCallback),webR equest);
}
}
void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
JObject json_response = JObject.Parse(streamReader.ReadToEnd());
String key = (String)json_response["ret"];
JObject dic = (JObject)json_response["retdic"];
}
catch(WebException e)
{
}
}
我希望 RemoteInsert() 方法等待我的回调 GetResponseCallback。
我无法做到这一点,因为尝试了很多东西。
任何人都可以在这里帮助我。
在收到此响应之前,我不希望主线程处理其他事情。
请建议。 因为在那个响应之后我必须将它用于下一次执行。
Here My code is going like
1. Task(A).running----await
2. ------------------------------->Task(B)
3 ---------Response From Task(A)------->Task(C)
我想像第一个任务 A 完成,然后是任务 (C) 一样实施它,因为它是在任务 (A) 成功时完成的。然后任务(B)
如果我理解你的问题,你是想在调用线程上执行 webrequest,并等待它完成后再继续?
您可以通过使用 async/await 并使用 GetResponseAsync()
方法在一次调用中获取响应对象来简化 RemoteInsert()
方法。像这样:
public async Task RemoteInsert()
{
String dataSet = "dataset=" + json.ToString();
accessKey = mobRemoteDB.accessKey;
String paramaters = "?llcommand=sndmsg&" + "ak=" + accessKey + "&command=remoteinsert&" + dataSet;
String url = mobRemoteDB.baseUrlForRemoteInsert + paramaters;
HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
if (NetworkInterface.GetIsNetworkAvailable() && IsDataValid)
{
// Await the GetResponseAsync() call.
using(var response = await webRequest.GetResponseAsync().ConfigureAwait(false))
{
// The following code will execute on the main thread.
using (var streamResponse = response.GetResponseStream())
{
using (StreamReader streamReader = new StreamReader(streamResponse))
{
JObject json_response = JObject.Parse(streamReader.ReadToEnd());
String key = (String)json_response["ret"];
JObject dic = (JObject)json_response["retdic"];
}
}
}
}
}
并失去其他两种方法,因为它们不再需要。
当你调用这个方法时,你可以在上面等待():
RemoteInsert().Wait();
但是,WP 试图强制执行良好的异步模式以避免这样做。在这种情况下,您需要什么来阻止 UI 线程?
在您调用 RemoteInsert() 的方法中...假设它是某种按钮单击事件...您可以不这样做吗?
public async void Button_Click()
{
// First call Remote insert, and await the result.
// The web-request will not block the UI thread.
await RemoteInsert();
// This code will be executed after RemoteInsert() completes:
... perform additional tasks that are dependant on RemoteInsert()
}
换句话说,不是阻塞 UI 线程,而是在 RemoteInsert() 方法完成后执行其他方法。
public void RemoteInsert()
{
String dataSet = "dataset=" + json.ToString();
accessKey = mobRemoteDB.accessKey;
String paramaters = "?llcommand=sndmsg&" + "ak=" + accessKey + "&command=remoteinsert&" + dataSet;
String url = mobRemoteDB.baseUrlForRemoteInsert + paramaters;
HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
if (NetworkInterface.GetIsNetworkAvailable() & IsDataValid)
{
webRequest.BeginGetRequestStream(newAsyncCallback(GetRequestStreamCallback),webR equest);
}
}
void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
JObject json_response = JObject.Parse(streamReader.ReadToEnd());
String key = (String)json_response["ret"];
JObject dic = (JObject)json_response["retdic"];
}
catch(WebException e)
{
}
}
我希望 RemoteInsert() 方法等待我的回调 GetResponseCallback。
我无法做到这一点,因为尝试了很多东西。
任何人都可以在这里帮助我。
在收到此响应之前,我不希望主线程处理其他事情。
请建议。 因为在那个响应之后我必须将它用于下一次执行。
Here My code is going like
1. Task(A).running----await
2. ------------------------------->Task(B)
3 ---------Response From Task(A)------->Task(C)
我想像第一个任务 A 完成,然后是任务 (C) 一样实施它,因为它是在任务 (A) 成功时完成的。然后任务(B)
如果我理解你的问题,你是想在调用线程上执行 webrequest,并等待它完成后再继续?
您可以通过使用 async/await 并使用 GetResponseAsync()
方法在一次调用中获取响应对象来简化 RemoteInsert()
方法。像这样:
public async Task RemoteInsert()
{
String dataSet = "dataset=" + json.ToString();
accessKey = mobRemoteDB.accessKey;
String paramaters = "?llcommand=sndmsg&" + "ak=" + accessKey + "&command=remoteinsert&" + dataSet;
String url = mobRemoteDB.baseUrlForRemoteInsert + paramaters;
HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
if (NetworkInterface.GetIsNetworkAvailable() && IsDataValid)
{
// Await the GetResponseAsync() call.
using(var response = await webRequest.GetResponseAsync().ConfigureAwait(false))
{
// The following code will execute on the main thread.
using (var streamResponse = response.GetResponseStream())
{
using (StreamReader streamReader = new StreamReader(streamResponse))
{
JObject json_response = JObject.Parse(streamReader.ReadToEnd());
String key = (String)json_response["ret"];
JObject dic = (JObject)json_response["retdic"];
}
}
}
}
}
并失去其他两种方法,因为它们不再需要。
当你调用这个方法时,你可以在上面等待():
RemoteInsert().Wait();
但是,WP 试图强制执行良好的异步模式以避免这样做。在这种情况下,您需要什么来阻止 UI 线程?
在您调用 RemoteInsert() 的方法中...假设它是某种按钮单击事件...您可以不这样做吗?
public async void Button_Click()
{
// First call Remote insert, and await the result.
// The web-request will not block the UI thread.
await RemoteInsert();
// This code will be executed after RemoteInsert() completes:
... perform additional tasks that are dependant on RemoteInsert()
}
换句话说,不是阻塞 UI 线程,而是在 RemoteInsert() 方法完成后执行其他方法。