Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to wait until a web request with HttpWebRequest is finished?

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();
}

I read a lot about this buf I didn't find the solution. Thanks everyone!!!

like image 545
Luis López Avatar asked Mar 19 '23 02:03

Luis López


1 Answers

Just use the newer style of asynchrony:

using (var response = (HttpWebResponse) await request.GetResponseAsync())
{
    ...
}

You shouldn't need to call BeginXXX much now - certainly the Microsoft APIs have pretty universally added support for the Task-based Asynchronous Pattern.

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)
{
}

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.

like image 153
Jon Skeet Avatar answered Apr 27 '23 05:04

Jon Skeet