Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The issue is with how you're dealing with the accessing the original requests in the callback from <code>BeginGetResponse</code>.</p> <p>Rather than holding a reference ot the state, get a reference back to the original request with:</p> <pre><code>var request = (HttpWebRequest)asynchronousResult.AsyncState; </code></pre> <p>Have a look at this very basic (but working) example of implementing logging in by posting email and password credentials to a website.</p> <pre><code>public static string Email; public static string Password; private void LoginClick(object sender, RoutedEventArgs e) { Email = enteredEmailAddress.Text.Trim().ToLower(); Password = enteredPassword.Password; var request = (HttpWebRequest)WebRequest.Create(App.Config.ServerUris.Login); request.ContentType = "application/x-www-form-urlencoded"; request.Method = "POST"; request.BeginGetRequestStream(ReadCallback, request); } private void ReadCallback(IAsyncResult asynchronousResult) { var request = (HttpWebRequest)asynchronousResult.AsyncState; using (var postStream = request.EndGetRequestStream(asynchronousResult)) { using (var memStream = new MemoryStream()) { var content = string.Format("Password={0}&amp;Email={1}", HttpUtility.UrlEncode(Password), HttpUtility.UrlEncode(Email)); var bytes = System.Text.Encoding.UTF8.GetBytes(content); memStream.Write(bytes, 0, bytes.Length); memStream.Position = 0; var tempBuffer = new byte[memStream.Length]; memStream.Read(tempBuffer, 0, tempBuffer.Length); postStream.Write(tempBuffer, 0, tempBuffer.Length); } } request.BeginGetResponse(ResponseCallback, request); } private void ResponseCallback(IAsyncResult asynchronousResult) { var request = (HttpWebRequest)asynchronousResult.AsyncState; using (var resp = (HttpWebResponse)request.EndGetResponse(asynchronousResult)) { using (var streamResponse = resp.GetResponseStream()) { using (var streamRead = new StreamReader(streamResponse)) { string responseString = streamRead.ReadToEnd(); // do something with responseString to check if login was successful } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload