【问题标题】:Post Method in Windows Phone 8Windows Phone 8 中的发布方法
【发布时间】:2014-06-06 02:14:53
【问题描述】:

我需要使用 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 发送数据的例子。错误可能与调用方法有关。看了几十个示例代码并尝试了一切,我真的很困惑。任何帮助表示赞赏。

【问题讨论】:

  • 您不是在发布数据,而是在执行 GET,这可能会对您有所帮助:stackoverflow.com/questions/10304863/…
  • 提供给我开发应用程序的文档说:“所有参数都通过 POST 方法传递”。至少我尝试过发布。但你是什么意思?我是在上面的代码中做 GET 还是我需要的是 GET?
  • 当您在 URL 中传递参数时,那些是 GET 参数,POST 参数将写入请求的主体中。看前面的链接,有一个如何POST参数的例子。
  • 谢谢。我尝试了代码: var client = new HttpClient(); var postData = new List>(); postData.Add(new KeyValuePair("Name", "test")); postData.Add(new KeyValuePair("Price", "100")); HttpContent 内容 = 新 FormUrlEncodedContent(postData); client.PostAsync("localhost:44268/api/test", content).ContinueWith( (postTask) => { postTask.Result.EnsureSuccessStatusCode(); });有用。我怎样才能得到响应(显示在屏幕上。)
  • 你可以从字符串 responseBodyAsText = await postTask.Result.Content.ReadAsStringAsync(); 得到响应

标签: c# windows-phone-8 http-post


【解决方案1】:

我终于设法解决了这个问题。感谢大家的帮助。工作代码如下,它使用 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

【讨论】:

    【解决方案2】:

    你可以试试这个

    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;
            }
    }
    

    【讨论】:

    • GetRequestStreamAsync(), GetResponseAsync() 给出:'System.Net.HttpWebRequest' 不包含定义......而“sessionCookie”是什么?
    • 我发现 GetRequestStreamAsync() 和 GetResponseAsync() 在 Windows Phone 8 中不可用。无论如何,谢谢。
    • 我在我的应用程序中使用它。可能是您的 .net 框架是 4 而不是 4.5
    • msdn.microsoft.com/en-us/library/… - 此页面显示,支持:Windows Phone 8.1、Silverlight 8.1。也许您的项目适用于 Windows Phone 8.1? (虽然 Windows Phone 8 在“Platforms”列表中,但我不知道它到底是什么意思)
    • 在下面的平台部分它说 Windows Phone 8.1、Windows Phone 8、Windows 8.1、Windows Server 2012 R2、Windows 8。我在我的应用程序中使用它,即 Windows Phone 7.1。如果您看到 .net 框架工作,它仅在 4.5 版本中可用
    【解决方案3】:

    HTTPWebRequest 将是另一种选择。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      相关资源
      最近更新 更多