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

問題は、文字列を保存する方法がわからないことです....これは非同期メソッドであるため、ページ内のtextBlockを変更したり、StreamWriterで保存したりできません。

4

1 に答える 1

0

これは非同期メソッドであるため、ページ内の textBlock を変更したり、StreamWriter で保存したりできません。

試す:

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