【问题标题】:HTTPClient post not working windows phone silverlightHTTPClient 帖子无法正常工作 windows phone silverlight
【发布时间】:2015-08-28 13:24:31
【问题描述】:

我正在制作一个 Silverlight 应用程序。现在我有这个功能来做一个 POST

  public async Task<Webservice> addEvent()
        {
                 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();

        }

但是我在FormUrlEncodedContent 上出现 gpt 构建错误,有人可以帮忙吗?

这是错误:

The type or namespace name 'FormUrlEncodedContent' could not be found (are you missing a using directive or an assembly reference?)

【问题讨论】:

    标签: c# silverlight windows-phone httpclient


    【解决方案1】:

    我遇到了同样的问题,我最终不得不从 NuGet 安装 Microsoft.Net.Http 包。

    在解决方案资源管理器中右键单击项目名称。
    管理 NuGet 包。
    搜索“microsoft.net.http”。
    安装。

    string site = "https://www.yoursite.com";
    var pairs = 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 client = new System.Net.Http.HttpClient();
    var content = new FormUrlEncodedContent(pairs);
    
    System.Net.Http.HttpResponseMessage response = await client.PostAsync(site, content);
    
    if (response.IsSuccessStatusCode)
    {
    
    }
    

    (在 windows phone 8.1 和 10 上测试)

    【讨论】:

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