Question

Im trying to get the string response from this request:

    void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

        // End the operation
        Stream postStream = request.EndGetRequestStream(asynchronousResult);

        string postData = "{\"email\":\"yudelsuarez@gmail.com\", \"password\":\"1234\"}";

        // Convert the string into a byte array. 
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        // Write to the request stream.
        postStream.Write(byteArray, 0, postData.Length);
        postStream.Close();

        // Start the asynchronous operation to get the response
        request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
    }

    private void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        // End the operation
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        Stream streamResponse = response.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        string responseString = streamRead.ReadToEnd();


        // Close the stream object
        streamResponse.Close();
        streamRead.Close();

        // Release the HttpWebResponse
        response.Close();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.tremendamuela.com/beta/index.php/api/Account");

        request.ContentType = "application/json";

        // Set the Method property to 'POST' to post data to the URI.
        request.Method = "PUT";

        // start the asynchronous operation
       request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request); 
    }

the problem is that I dont know how to save the string....cos thats an async method it doesnt' let me modify a textBlock in the pag or save it with a StreamWriter.

No correct solution

OTHER TIPS

cos thats an async method it doesnt' let me modify a textBlock in the pag or save it with a StreamWriter.

Try:

Dispatcher.BeginInvoke(() => 
    {
        textBlock.Text = responseString;
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top