0

我试图从这个请求中获取字符串响应:

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

问题是我不知道如何保存字符串....因为这是一个异步方法,它不允许我修改 pag 中的 textBlock 或使用 StreamWriter 保存它。

4

1 回答 1

0

因为那是一个异步方法,它不允许我修改页面中的 textBlock 或使用 StreamWriter 保存它。

尝试:

Dispatcher.BeginInvoke(() => 
    {
        textBlock.Text = responseString;
    });
于 2013-07-17T21:12:11.423 回答