C# windowsphone8中的Post方法

C# windowsphone8中的Post方法,c#,windows-phone-8,http-post,C#,Windows Phone 8,Http Post,我需要在WP8应用程序中使用post方法向php服务器发送一些参数,并获得json格式的响应。我在stackoverflow和其他网站上找到的所有东西都试过了,但仍然没有找到 我提出的最后一个代码是: public static async void GetData(string url, string data) { HttpClient client = new HttpClient(); HttpResponseMessage response = await client

我需要在WP8应用程序中使用post方法向php服务器发送一些参数,并获得json格式的响应。我在stackoverflow和其他网站上找到的所有东西都试过了,但仍然没有找到

我提出的最后一个代码是:

public static async void GetData(string url, string data)
{
    HttpClient client = new HttpClient();
    HttpResponseMessage response = await client.PostAsync(new Uri(url), new StringContent(data));

    response.EnsureSuccessStatusCode();
    string responseBody = await response.Content.ReadAsStringAsync();
    MessageBox.Show(responseBody); // just showing the response for now
}
它显示了一条来自服务器的消息(这是一个错误,说明某些字段丢失),表明它实际上与服务器通信,但问题在于发送数据。我将上述方法称为:

GetData("http_address_here", "?action=REGISTER&email=asc@fsdf.com&password=54561wefwe&firstname=sdfsdf&lastname=sdfsdf&picture=10");

但我看到了一个用xml发送数据的示例。可能错误在于调用该方法。看过几十个示例代码并尝试了所有东西后,我真的很困惑。非常感谢您的帮助。

HTTPWebRequest将是另一种选择。

您能试试这个吗

public static async Task<string> SendRequestPostResponse()
    {
        try
        {
            var postRequest = (HttpWebRequest)WebRequest.Create("your Url Here");

            postRequest.ContentType = "application/x-www-form-urlencoded"; // Whichever content type you want to POST
            postRequest.Method = "POST";

            using (var requestStream = await postRequest.GetRequestStreamAsync())
            {
                byte[] postDataArray = Encoding.UTF8.GetBytes("your data here"); // Data for the POST request here
                await requestStream.WriteAsync(postDataArray, 0, postDataArray.Length);
            }

            WebResponse postResponse = await postRequest.GetResponseAsync();

            if (postResponse != null)
            {
                var postResponseStream = postResponse.GetResponseStream();
                var postStreamReader = new StreamReader(postResponseStream);

                string response = await postStreamReader.ReadToEndAsync();

                return response;
            }
            return null;
        }
}
公共静态异步任务SendRequestPostResponse()
{
尝试
{
var postRequest=(HttpWebRequest)WebRequest.Create(“此处的Url”);
postRequest.ContentType=“application/x-www-form-urlencoded”;//您想发布的内容类型
postRequest.Method=“POST”;
使用(var requestStream=await postRequest.GetRequestStreamAsync())
{
byte[]postDataArray=Encoding.UTF8.GetBytes(“此处为您的数据”);//此处为POST请求提供数据
wait requestStream.WriteAsync(postDataArray,0,postDataArray.Length);
}
WebResponse postResponse=等待postRequest.GetResponseAsync();
if(postResponse!=null)
{
var postResponseStream=postResponse.GetResponseStream();
var postStreamReader=新的StreamReader(postResponseStream);
字符串响应=等待postStreamReader.ReadToEndAsync();
返回响应;
}
返回null;
}
}

我终于设法解决了这个问题。谢谢大家的帮助。下面是工作代码,它使用POST方法,生成的json对象存储为字符串

var values = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("email", "qewfwef"),
                    new KeyValuePair<string, string>("password", "qewfwef"),
                    new KeyValuePair<string, string>("firstname", "qewfwef"),
                    new KeyValuePair<string, string>("lastname", "qewfwef"),
                    new KeyValuePair<string, string>("picture", "123456")
                };

        var httpClient = new HttpClient(new HttpClientHandler());
        HttpResponseMessage response = await httpClient.PostAsync(url, new FormUrlEncodedContent(values));
        response.EnsureSuccessStatusCode();
        var responseString = await response.Content.ReadAsStringAsync();

        MessageBox.Show(responseString.ToString()); // just to see what we get
var值=新列表
{
新的KeyValuePair(“电子邮件”、“qewfwef”),
新的KeyValuePair(“密码”、“qewfwef”),
新的KeyValuePair(“名字”,“qewfwef”),
新的KeyValuePair(“姓氏”、“qewfwef”),
新的KeyValuePair(“图片”,“123456”)
};
var httpClient=newhttpclient(newhttpclienthandler());
HttpResponseMessage response=等待httpClient.PostAsync(url,新表单UrlEncodedContent(值));
response.EnsureSuccessStatusCode();
var responseString=await response.Content.ReadAsStringAsync();
MessageBox.Show(responseString.ToString());//看看我们能得到什么

您不是在发布数据,而是在执行GET,这可能会对您有所帮助:为我开发应用程序提供的文档中说:“所有参数都是通过POST方法传递的”。至少我试着发帖。但你是什么意思?我是在上面的代码中执行GET,还是我需要的是GET?当您在URL中传递参数时,这些参数是GET参数,POST参数将写入请求的主体中。看看前面的链接,这里有一个关于如何发布参数的示例。谢谢。我尝试了以下代码:var client=new HttpClient();var postData=新列表();添加(新的KeyValuePair(“名称”、“测试”);添加(新的KeyValuePair(“价格”,“100”));HttpContent=新的FormUrlEncodedContent(postData);client.PostAsync(“,content).ContinueWith((poststask)=>{poststask.Result.EnsureSuccessStatusCode();});它起作用了。如何获取响应(显示在屏幕上)。您可以从字符串responseBodyAsText=wait postTask.Result.Content.ReadAsStringAsync()获取响应;GetRequestStreamAsync(),GetResponseAsync()给出:'System.Net.HttpWebRequest'不包含定义。。。什么是“sessionCookie”?我发现GetRequestStreamAsync()和GetResponseAsync()在Windows Phone 8中不可用。不管怎样,谢谢你。我正在我的应用程序中使用它。可能您的.net framework是4,而不是4.5-此页面显示,支持Windows Phone 8.1、Silverlight 8.1。也许你的项目是针对Windows Phone 8.1的?(虽然Windows Phone 8在“平台”列表中,但我不知道它的确切含义)下面的平台部分显示Windows Phone 8.1、Windows Phone 8、Windows 8.1、Windows Server 2012 R2、Windows 8。我在我的应用程序windows phone 7.1中使用它。如果您看到.net框架,则它仅在4.5版本中可用