c# - 为什么在使用 Postman 工作时使用 C# HttpClient 调用 WebAPI 返回 401?
问题描述
这是 WebAPI 端点: https ://mysoylplus-apptest.frontierag.co.uk/api/someendpoint
我在 Postman 中以键值对形式发送 formData,如下所示:
authenticationData[b64EncryptedSoylSenseInfo] : 'somekey', authenticationData[b64iv] : 'some key'
上面的东西在 Postman 中工作,但是当我HttpClient
在 C# 代码中使用时,这给了我401 exception
.
我究竟做错了什么?我使用 Postman 得到 200 OK,但使用下面的代码得到 401。
string url = "https://mysoylplus-apptest.frontierag.co.uk/api/someendpoint";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Accept", "application/x-www-form-urlencoded");
using (var httpContent = new HttpRequestMessage(HttpMethod.Post, url))
{
client.Timeout = TimeSpan.FromMinutes(20);
var values = new Dictionary<string, string>();
values.Add("authenticationData[b64EncryptedSoylSenseInfo]", "somekey");
values.Add("authenticationData[b64iv]", "somekey");
httpContent.Content = new FormUrlEncodedContent(values);
var response = client.PostAsync(url, httpContent.Content);
// here is the hash as string
var result = response.Result.ToString();
}
}
解决方案
推荐阅读
- c++ - C++ 迭代模板参数
- linux - 如何根据文件的第一个值的条件在 Linux 中对输出文件进行排序?
- python - 在 Django 中用块加载 css
- jquery - 删除输入值时动态添加的标签文本
- java - Springboot 无法使用自定义上下文路径将客户端应用程序注册到 Eureka 服务器
- html - 两列布局,右列有 3 行
- android - 使用 Spotify API 时如何解析 JSON 数组?
- python - 更新中带有小部件的 Django Form DateInput:丢失初始值
- sql - TSQL:如何使用 charindex ?- 在特定字符之间的变量字符串中查找特定单词
- javascript - 使用 Nightwatch 框架的 url() 函数时,onbeforeunload 事件不显示警报