【问题标题】:HttpClient post with parameters in bodyHttpClient 帖子,正文中带有参数
【发布时间】:2018-08-05 05:58:45
【问题描述】:

我成功使用邮递员进行身份验证,但似乎无法从 C# 中运行它,得到 401。密码和用户名的验证与邮递员相同。有任何想法吗?这是我的代码:

            var url = AppConstants.ApiLoginUrl;
            var uriRequest = new Uri(url);
            string httpResponseBody;

            using (var httpClient = new Windows.Web.Http.HttpClient())
            { 
              var content = new HttpStringContent(string.Format("username={0}&password={1}", email, password), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/x-www-form-urlencoded");
                try
                {
                    var httpResponse = await httpClient.PostAsync(uriRequest, content); 

... }

这里是 Postman 中标题和正文的设置。

现在将这段代码用于内容参数:

                var content = new HttpFormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("username", email),
                    new KeyValuePair<string, string>("password", password)
                });

仔细查看后,用户名似乎在 Fiddler 中针对 Postman 和代码请求进行了编码。所以,我关于使用编码用户名的理论并不完全正确。这里是 Fiddler 请求的快照...还有什么建议/想法吗?

邮递员标题:

代码头:

原始视图 Postman * 显示编码的用户名字段:

原始视图代码

【问题讨论】:

  • 您是否运行 fiddler 来实际查看发送的内容?
  • 尝试使用User-Agent 标头。
  • @OldProgrammer 是的,我做到了,并且发现用户名正在被编码。因此,russ@domain.com 以 russ%40domain.com 的形式通过网络,我需要将其设置为不以某种方式进行编码。
  • 用户代理不是问题@crowcoder
  • 因为您尝试过,或者其他方法有效,或者您不相信?一些服务会检查用户代理,即使它是自定义值。

标签: c# postman dotnet-httpclient


【解决方案1】:

尝试将您的内容更改为以下内容

var content = new FormUrlEncodedContent(new[]
{
    new KeyValuePair<string, string>("username", email),
    new KeyValuePair<string, string>("password", password)
});

【讨论】:

  • 这非常接近我的需要,但是正在对用户名进行编码。因此,user@domain.com 的用户名作为 user%40domain.com 通过网络传输,我需要将其设置为不以某种方式编码,因为服务器正在验证 user@domain.com 并且在 user%40domain.com 上失败...
【解决方案2】:

我正在使用此功能发送带有参数的发布请求,其中Dictionary&lt;string, string&gt; data 作为 Web 服务所期望的键值

public static T PostCast<T>(string url, Dictionary<string, string> data)
{
    using (HttpClient httpClient = new HttpClient())
    {
        var response = httpClient.PostAsync(url, new FormUrlEncodedContent(data)).Result;
        return response.Content.ReadAsAsync<T>().Result;
    }
}

希望对你有帮助:)

【讨论】:

  • 我将 Windows.Web.Http.HttpClient 用于 UWP 应用程序,因此方法与您上面的系统版本略有不同。例如,用 HttpFormUrlEncodedContent 代替 FormUrlEncodedContent。所以底线是在编码后很难修改内容以用“@”替换编码的“%40”。似乎找不到等效的 HttpFormUrlContent 方法(没有编码)。我需要用户名按原样通过电线。想法?
  • @user3810967 使用系统版本不是一个选项吗?因为System.Net.Http 似乎更通用,使用更广泛
【解决方案3】:

我的理解是,对于 UWP 应用程序,Windows.Web.Http.HttpClient() 是执行此操作的推荐方法,并确保您的 URL 没有拼写错误。 :)

         var url = AppConstants.ApiLoginUrl;
            var uriRequest = new Uri(url);

            var content = new HttpFormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("username", email),
                new KeyValuePair<string, string>("password", password)
            });

            using (var httpClient = new Windows.Web.Http.HttpClient())
            {
                try
                {
                    var httpResponse = await httpClient.PostAsync(uriRequest, content); 

【讨论】:

    【解决方案4】:

    我遇到了同样的问题,并通过从 HttpClient 发布到的 URL 中删除尾随“/”来修复它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      相关资源
      最近更新 更多