【问题标题】:Windows phone 7: post some values, retrieve the responseWindows phone 7:发布一些值,检索响应
【发布时间】:2011-09-15 13:35:42
【问题描述】:

如何将值发布到在线页面,然后检索响应以将其放入字符串中? 这是一个 PHP 页面,它采用 $_POST 值,然后回显我想要获取的响应。

【问题讨论】:

    标签: windows-phone-7 post data-retrieval


    【解决方案1】:

    查看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();
    }
    

    他的代码稍微改编自this question

    【讨论】:

    • 好的,我认为它有效。但我在哪里可以检索我的回复?如果我使用: var Response = streamReader.ReadToEnd(); MessageBox.Show(Response.ToString());我收到一个错误
    • streamReader.ReadToEnd() 已经返回一个字符串,因此 .ToString() 是多余的。假设您在 GetResponseCallback() 方法中创建一个 MessageBox,请注意您在那里的一个新线程上,因此要弹出消息框,您需要将其称为 Dispatcher.BeginInvoke(() => MessageBox.Show(Response));
    • 是的,你是对的! :-P 但是现在,要了解这件事的最后一个问题,我如何将字段值附加到帖子数据?我已经这样做了,但出现错误:string postData = "language=en&value=" + testo.Text;
    • 如何将文本框值放入帖子字符串中???我检索到无效跨线程的错误
    • 您可以在调用后台线程之前将值存储在字符串中(简单的方法),或者使用调度程序在方法中获取值。有关示例,请参阅stackoverflow.com/questions/710034/…。抱歉,我现在正在度假,所以不能直接给你代码。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    相关资源
    最近更新 更多