ASP.NET post JSON 数据到 HTTP API 使用身份验证 header
ASP.NET post JSON data to HTTP API with an authentication header
我有 C# 代码可以将 JSON 数据发送到 web API but I keep getting a 401 (Unauthorized) response. The code below should correctly make a POST request according to this function,对吗?我也尝试了相同结果的小变化。
这是发出请求的代码:
public async Task Create()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://zrh.cloudsigma.com/api/2.0/");
var testVM = new CS_VM("test");
var auth = string.Format("{0}:{1}", "mail@mail.com", "password");
var encoded = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encoded);
HttpResponseMessage response = await client.PostAsJsonAsync("servers", testVM);
if (response.IsSuccessStatusCode)
{
var a = "ok";
}
else
{
var a = "fail";
}
}
}
这是作为 JSON:
发送的 class
public class CS_VM
{
public CS_VM(string type)
{
if ("test" == type)
{
cpu = 1000;
mem = 536870912;
name = "testServer";
vcn_password = "testserver";
}
}
public string name { get; set; }
public int cpu { get; set; }
public int cores { get; set; }
public int mem { get; set; }
public string status { get; set; }
public Owner owner { get; set; }
public Uri resource_uri { get; set; }
public string uuid { get; set; }
public string vcn_password { get; set; }
}
请求headers:
Authorization: Basic bWFpbEBtYWlsLmNvbTpwYXNzd29yZA==
响应headers:
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Cookie
X-REQUEST-ID: 2584e232-5bb2-48c0-a307-67e6c03258c0
Date: Sun, 19 Jul 2015 21:39:21 GMT
Server: cloudflare-nginx
WWW-Authenticate: Digest nonce="1437341961.55:6967:0fd0a6b2dcde8f45a5ae288c3b73ee12", realm="users", algorithm="MD5",opaque="b228739d1711b0ff025703aea82ee2a208faaaa7", qop="auth", stale="false", Basic Realm="users"
CF-RAY: 2089941a6935168e-ARN
从WWW-Authenticate: Digest nonce="1437341...
看来是这样的,那就是摘要认证。你应该根据你得到的回应建立一个新的授权header。使用您链接的有关 web API 的方法,并使用 Digest Access Authentication
部分。 nonce
、realm
、qop
变量在第一个 401 响应中给出。
我有 C# 代码可以将 JSON 数据发送到 web API but I keep getting a 401 (Unauthorized) response. The code below should correctly make a POST request according to this function,对吗?我也尝试了相同结果的小变化。
这是发出请求的代码:
public async Task Create()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://zrh.cloudsigma.com/api/2.0/");
var testVM = new CS_VM("test");
var auth = string.Format("{0}:{1}", "mail@mail.com", "password");
var encoded = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encoded);
HttpResponseMessage response = await client.PostAsJsonAsync("servers", testVM);
if (response.IsSuccessStatusCode)
{
var a = "ok";
}
else
{
var a = "fail";
}
}
}
这是作为 JSON:
发送的 classpublic class CS_VM
{
public CS_VM(string type)
{
if ("test" == type)
{
cpu = 1000;
mem = 536870912;
name = "testServer";
vcn_password = "testserver";
}
}
public string name { get; set; }
public int cpu { get; set; }
public int cores { get; set; }
public int mem { get; set; }
public string status { get; set; }
public Owner owner { get; set; }
public Uri resource_uri { get; set; }
public string uuid { get; set; }
public string vcn_password { get; set; }
}
请求headers:
Authorization: Basic bWFpbEBtYWlsLmNvbTpwYXNzd29yZA==
响应headers:
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Cookie
X-REQUEST-ID: 2584e232-5bb2-48c0-a307-67e6c03258c0
Date: Sun, 19 Jul 2015 21:39:21 GMT
Server: cloudflare-nginx
WWW-Authenticate: Digest nonce="1437341961.55:6967:0fd0a6b2dcde8f45a5ae288c3b73ee12", realm="users", algorithm="MD5",opaque="b228739d1711b0ff025703aea82ee2a208faaaa7", qop="auth", stale="false", Basic Realm="users"
CF-RAY: 2089941a6935168e-ARN
从WWW-Authenticate: Digest nonce="1437341...
看来是这样的,那就是摘要认证。你应该根据你得到的回应建立一个新的授权header。使用您链接的有关 web API 的方法,并使用 Digest Access Authentication
部分。 nonce
、realm
、qop
变量在第一个 401 响应中给出。