我正在执行Web请求以登录我的Web服务,但是我遇到了问题.我需要等到登录完成后才能结束启动登录的方法,因为我需要返回一个布尔值,该值代表登录是否正确.在下面的代码中,我在需要等待"httpWebRequest.BeginGetResponse(...)"结果的地方加了注释.
I am doing a web request to login in a my web services and I have a problem. I need to wait until the login is finished before end the method that launchs the login because I need to return a boolean value that represents if the login was correct or not. In the following code I put a comment where I need wait for the result of "httpWebRequest.BeginGetResponse(...)"
public async Task<bool> login(string user, string passwrd) { mLoginData.setUserName(user); mLoginData.setPasswd(passwrd); string serviceURL = mBaseURL + "/pwpcloud/service/user/login"; //build the REST request // HTTP web request var httpWebRequest = (HttpWebRequest)WebRequest.Create(serviceURL); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; // Write the request Asynchronously using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream, httpWebRequest.EndGetRequestStream, null)) { string parameters = "{\"username\":\"" + user + "\",\"password\":\"" + passwrd + "\"}"; byte[] byteArray = Encoding.UTF8.GetBytes(parameters); // Write the bytes to the stream await stream.WriteAsync(byteArray, 0, byteArray.Length); } //httpWebRequest.BeginGetResponse(new AsyncCallback(OnGettingLoginResponse), httpWebRequest); httpWebRequest.BeginGetResponse(r => { using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.EndGetResponse(r)) { string data; // Read the response into a Stream object. Stream responseStream = response.GetResponseStream(); using (var reader = new StreamReader(responseStream)) { data = reader.ReadToEnd(); } responseStream.Close(); if (response.StatusCode == HttpStatusCode.OK) { dynamic jsonData = JObject.Parse(data); string token = jsonData.token; mLoginData.setToken(token); string userId = jsonData.userId; mLoginData.setUserID(userId); mLoginData.setLogged(true); //guardamos los valores en el isolatedStorageSettings para que persistan. App.settings.Clear(); App.settings.Add("userId", mLoginData.getUserID()); App.settings.Add("token", mLoginData.getToken()); App.settings.Add("userName", mLoginData.getUserName()); App.settings.Add("passwd", mLoginData.getPasswd()); App.settings.Add("logged", mLoginData.isLogged()); App.settings.Save(); } else { if (CultureInfo.CurrentUICulture.Name.ToString().Contains("ES")) { if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.BadRequest) { MessageBox.Show("Usuario o contraseña incorrectos"); } else { MessageBox.Show("Error de conexión."); } } else { if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.BadRequest) { MessageBox.Show("User or password were incorrect"); } else { MessageBox.Show("NNetwork connection error"); } } } } }, null); //here i need wait for the result of the webrequest to return true if the login was successfull. return mLoginData.isLogged(); }我读了很多有关这个buf的文章,但没有找到解决方案. 谢谢大家!
I read a lot about this buf I didn't find the solution. Thanks everyone!!!
推荐答案只需使用更新的异步样式:
Just use the newer style of asynchrony:
using (var response = (HttpWebResponse) await request.GetResponseAsync()) { ... }您现在不需要调用BeginXXX了-当然,Microsoft API已相当普遍地添加了对基于任务的异步模式的支持.
You shouldn't need to call BeginXXX much now - certainly the Microsoft APIs have pretty universally added support for the Task-based Asynchronous Pattern.
如果GetResponseAsync不可用,请像使用BeginRequestStream一样使用Task.Factory.FromAsync:
If GetResponseAsync isn't available, use Task.Factory.FromAsync just as you have for BeginRequestStream:
var responseTask = Task.Factory.FromAsync<WebResponse> (httpWebRequest.BeginGetResponse, httpWebRequest.EndGetResponse, null); using (var response = (HttpWebResponse) await responseTask) { }请注意,因为您正在编写async方法,所以初始方法调用 仍会快速完成-但是它将返回Task<bool>,该方法仅在异步操作完成后才会完成.因此,您可能还应该等待调用代码中的结果.
Note that because you're writing an async method, the initial method call will still finish quickly - but it will return a Task<bool> which will only complete when the async operation has completed. So you should probably be awaiting the result in the calling code, too.
更多推荐
如何等待直到带有HttpWebRequest的Web请求完成?
发布评论