Windows Phone 7:发布一些值,检索响应

| 如何将值发布到在线页面,然后检索响应以将其放入字符串中? 这是一个PHP页面,它采用
$_POST
值,然后回显我要抓取的响应。     
已邀请:
        查看HttpWebRequest对象。
public void DoWork()
{
    var url = \"http://posttestserver.com/post.php\";
    // Create the web request object
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Method = \"POST\";
    webRequest.ContentType = \"application/x-www-form-urlencoded\";
    // Start the request
    webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);    
}
void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
    HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
    // End the stream request operation
    Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
    // Create the post data
    // Demo POST data 
    string postData = \"Username=MyUserName&password=MyPassword\";
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    // Add the post data to the web request
    postStream.Write(byteArray, 0, byteArray.Length);
    postStream.Close();
    // Start the web request
    webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}

void GetResponseCallback(IAsyncResult asynchronousResult)
{
    try
    {
        HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
        HttpWebResponse response;
        // End the get response operation
        response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
        Stream streamResponse = response.GetResponseStream();
        StreamReader streamReader = new StreamReader(streamResponse);
        var Response = streamReader.ReadToEnd();
        streamResponse.Close();
        streamReader.Close();
        response.Close();
    }
    catch (WebException e)
    {
        // Error treatment
        // ...
    }
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    DoWork();
}
他的代码对此问题做了些微调整     

要回复问题请先登录注册