如何使用Windows phone 8 API发送简单的POST请求? [英] How do I send a simple POST request using the Windows phone 8 API?

查看:102
本文介绍了如何使用Windows phone 8 API发送简单的POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为标题非常自我解释。我想发送一个http POST请求,例如:  http://192.168.2.9/android_connect/add_device.php?username = CoolSops& device_id = WindowsPhone



如何我是否使用Windows Phone 8 API执行此操作?

I think the title is pretty self explanatory. I want to send a http POST request like: http://192.168.2.9/android_connect/add_device.php?username=CoolSops&device_id=WindowsPhone

How do I do this using the Windows Phone 8 API?

推荐答案

您可以使用 HttpWebRequest 类这是一个小例子:

你把参数放在" GetRequestStreamCallback "中示例中显示的方法
(postData.Append(" param1 = value1")

(代码仍然需要响应方法才能工作)

You can use HttpWebRequest Class here is a little example :
you put the parametres in "GetRequestStreamCallback" method as shown in the example (postData.Append("param1=value1")
(the code still needs the response method to work)

	private void POST_TEST(object sender, RoutedEventArgs e)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.2.9/android_connect/add_device.php?username=CoolSops&device_id=WindowsPhone");
        request.ContentType = "application/x-www-form-urlencoded";
        request.Method = "POST";

        request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
    }

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

        Stream postStream = request.EndGetRequestStream(asyncResult);

        StringBuilder postData = new StringBuilder();
        postData.Append("param1=value1");
        postData.Append("param2=value2");

        byte[] byteArray = Encoding.UTF8.GetBytes(postData.ToString());

        postStream.Write(byteArray, 0, postData.Length);
        postStream.Close();

        request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
    }





这篇关于如何使用Windows phone 8 API发送简单的POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆