![](/img/trans.png)
[英]ASP.NET post JSON data to HTTP API with an authentication header
[英]HTTP Post with header and parameters ASP.NET
我想将POST请求发送到文档中描述的API,但仅包含PHP示例。
现在,我的代码如下所示:
string URI = "https://api.monetivo.com/v1/auth/login";
string myParameters = "login=2DY9&password=346f417edb495e484b67";
using (WebClient wc = new WebClient())
{
wc.Headers.Add("X-API-Token", "test_d7b8ddfe-fae6-404c-84fe-02c6d5d292e4");
string HtmlResult = wc.UploadString(URI, myParameters);
}
我总是收到错误401 No authorization
。 我需要有关如何编码它以发送带有参数和特定标头的请求的帮助。
查看数据结构,我认为您必须指定x-www-form-urlencoded
作为内容类型。
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
wc.Headers["X-API-Token"] = "test_d7b8ddfe-fae6-404c-84fe-02c6d5d292e4";
string HtmlResult = wc.UploadString(URI, myParameters);
}
但是除此之外,我建议您考虑将数据作为NameValueCollection传递(请参阅msdn) 。
var param = new System.Collections.Specialized.NameValueCollection();
param.Add("login", "2DY9");
param.Add("password", "346f417edb495e484b67");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.