API发送简单的POST请求

API发送简单的POST请求

本文介绍了如何使用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?

推荐答案

	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请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:19