【问题标题】:how to perform post method in windows 8 metro?如何在 Windows 8 Metro 中执行 post 方法?
【发布时间】:2012-04-25 09:38:59
【问题描述】:

我遵循了 HttpClient 示例,但不知道如何发布带有 2 个参数的方法。

以下是我尝试过的,但它返回错误的网关错误:

        private async void Scenario3Start_Click(object sender, RoutedEventArgs e)
    {
        if (!TryUpdateBaseAddress())
        {
            return;
        }

        Scenario3Reset();
        Scenario3OutputText.Text += "In progress";

       string resourceAddress =  "http://music.api.com/api/search_tracks";
        try
        {
            MultipartFormDataContent form = new MultipartFormDataContent();
        //    form.Add(new StringContent(Scenario3PostText.Text), "data");
            form.Add(new StringContent("Beautiful"), "track");
            form.Add(new StringContent("Enimem"), "artist");

            HttpResponseMessage response = await httpClient.PostAsync(resourceAddress, form);
        }
        catch (HttpRequestException hre)
        {
            Scenario3OutputText.Text = hre.ToString();
        }
        catch (Exception ex)
        {
            // For debugging
            Scenario3OutputText.Text = ex.ToString();
        }
    }

我浏览了整个互联网,但找不到任何显示如何执行 http post 方法的工作示例或文档。任何材料或样品都会对我有很大帮助。

【问题讨论】:

  • 你确定服务器期待multipart/form-data吗?您是否尝试过 ByteArrayContent 包含类似 artist=Enimem&track=Beautiful 的内容?
  • 我认为 music.api.com 不是真实的,所以我无法验证它是否需要表单数据。但总的来说,rest apis 不期望表单数据。表单数据主要用于用户提交的表单和文件上传。
  • 嗯,也许我在 multipart/form 上错了,因为那是我找到的样本。如果是 byteArray,我该如何传入参数?谢谢大家

标签: http-post windows-8 microsoft-metro


【解决方案1】:

尝试使用 FormUrlEncodedContent 代替 MultipartFormDataContent:

var content = new FormUrlEncodedContent(
    new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string>("track", "Beautiful"),
        new KeyValuePair<string, string>("artist", "Enimem")
    }
);

【讨论】:

    【解决方案2】:

    我更喜欢采用以下方法,将 POST 数据设置到请求内容正文中。调试起来要容易得多!

    使用您发布到的 URL 创建您的 HttpClient 对象:

    string oauthUrl = "https://accounts.google.com/o/oauth2/token";
    HttpClient theAuthClient = new HttpClient();
    

    使用 Post 方法将您的请求发送到您的网址

    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, oauthUrl); 
    

    使用以 POST 数据格式明确设置的参数创建一个内容字符串,并在请求中设置这些:

    string content = "track=beautiful" +
      "&artist=eminem"+
      "&rating=explicit";
    
    request.Method = HttpMethod.Post;
    request.Content = new StreamContent(new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(content)));
    request.Content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    

    发送请求并得到响应:

    try
    {                
        HttpResponseMessage response = await theAuthClient.SendAsync(request);
        handleResponse(response);
    }
    catch (HttpRequestException hre)
    {
    
    }            
    

    一旦请求返回,您的处理程序将被调用,并将从您的 POST 中获取响应数据。以下示例显示了一个处理程序,您可以在其中设置断点以查看响应内容是什么,此时您可以解析它或对它做任何您需要做的事情。

    public async void handleResponse(HttpResponseMessage response)
    {
        string content = await response.Content.ReadAsStringAsync();
    
        if (content != null)
        {
            // put your breakpoint here and poke around in the data
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      相关资源
      最近更新 更多