我在Windows商店应用程序项目中实现了其他同事与Apiary.io的api.
他们展示了我必须实现的方法的这个例子
var baseAddress = new Uri("https://private-a8014-xxxxxx.apiary-mock.com/");
using (var httpClient = new HttpClient{ BaseAddress = baseAddress })
{
using (var response = await httpClient.GetAsync("user/list{?organizationId}"))
{
string responseData = await response.Content.ReadAsStringAsync();
}
}
Run Code Online (Sandbox Code Playgroud)
在这个和其他一些方法中,我需要一个带有我之前获得的标记的标题
下面是一个邮递员(镀铬扩展)的图像,标题我正在谈论

如何将该授权标头添加到请求中?
c# windows-runtime dotnet-httpclient windows-store-apps apiary.io
RFC2617表示将用户名和密码编码为base64,但没有说明在创建用于输入base64算法的八位字节时要使用的字符编码.
我应该假设US-ASCII或UTF8吗?或者有人已经在某处解决了这个问题?
什么时候我们应该在HttpClient中的头文件中使用HttpRequestMessage对象中的头文件?
我们需要添加授权(始终更改)和少量自定义标头(始终更改)
问题
我应该在HttpClient和HttpRequestMessage对象的基于请求的头部添加公共头(在所有请求中相同)吗?
//HttpRequestMessage Code
HttpRequestMessage reqmsg =new HttpRequestMessage();
reqmsg.Headers.Authorization =new AuthenticationHeaderValue("some scheme");
reqmsg.Headers.Add("name","value");
//HttpClient Code
HttpClient client =new HttpClient();
client.DefaultRequestHeaders.Authorization =new AuthenticationHeaderValue("some scheme");
client.DefaultRequestHeaders.Add("name", "value");
Run Code Online (Sandbox Code Playgroud)我正在尝试使用 HttpClient 为 HTTP Post 或 HTTP Get 编写客户端。当谷歌搜索时,我遇到了这些在 HttpClient 对象中设置这些身份验证的方法。一个使用 NetworkCredential,另一个使用 AuthenticationHeaderValue
HttpClient sClient;
HttpClientHandler sHandler = new HttpClientHandler();
sHandler.Credentials = new NetworkCredential("UserName", "Password");
sClient = new HttpClient(sHandler);
Run Code Online (Sandbox Code Playgroud)
或者
HttpClient sClient new HttpClient();
sClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic",Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("UserName:Password")));
Run Code Online (Sandbox Code Playgroud)
在 MSDN 上阅读并没有给我关于它们之间差异的明确答案。在这种情况下,除了身份验证信息的存储方式之外,两者都会执行相同的操作吗?例如 AuthenticationHeaderValue 将其放入标头,而另一个则没有?就我的用例或最佳实践而言,其中一个比另一个更好吗?
在使用简单的HTTPClient时,我可以在哪里设置标头到REST服务调用?
我做 :
HttpClient client = new HttpClient();
var values = new Dictionary<string, string>
{
{"id", "111"},
{"amount", "22"}
};
var content = new FormUrlEncodedContent(values);
var uri = new Uri(@"https://some.ns.restlet.uri");
var response = await client.PostAsync(uri, content);
var responseString = await response.Content.ReadAsStringAsync();
Run Code Online (Sandbox Code Playgroud)
UPD
我不会添加的标题是:
{
"Authorization": "NLAuth nlauth_account=5731597_SB1, nlauth_email=xxx@xx.com, nlauth_signature=Pswd1234567, nlauth_role=3",
"Content-Type": "application/json"
}
Run Code Online (Sandbox Code Playgroud)
我应该遵循:
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", "NLAuth nlauth_account=5731597_SB1, nlauth_email=xxx@xx.com, nlauth_signature=Pswd1234567, nlauth_role=3","Content-Type":"application/json");
Run Code Online (Sandbox Code Playgroud) 如何在下面的上下文中使用webclient发出POST请求?
我能够成功验证身份并jwt从ADFS 检索令牌:
using (var client = new WebClient())
{
var data = Encoding.UTF8.GetBytes(rstXml);
client.Headers.Add("Content-Type", "application/soap+xml; charset=utf-8");
var responseData =
client.UploadData($"https://{adfsServer}/adfs/services/trust/13/usernamemixed", data);
var rstr = Encoding.UTF8.GetString(responseData);
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(rstr);
var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("wsse",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
nsmgr.AddNamespace("wsu",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
jwt = xmlDoc.SelectSingleNode("//wsse:BinarySecurityToken", nsmgr).InnerText; // JWT
}
Run Code Online (Sandbox Code Playgroud)
如何在相同的上下文中发出POST请求?
我正在尝试在 ASP.NET Core MVC 应用程序中使用 Reddit API ( https://github.com/reddit-archive/reddit/wiki/OAuth2 ),并且要获取令牌,我必须向具有 HTTP 基本授权的 URI(用户名和密码是客户端 ID 和密码)。目前我使用这段代码:
public async Task<HttpResponseMessage> HttpPost(string uri, string value)
{
HttpClient httpClient = new HttpClient();
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(uri, new StringContent(value));
return httpResponseMessage;
}
Run Code Online (Sandbox Code Playgroud)
但是,这不使用授权。如何添加授权?HttpClient.PostAsync我尝试查看和的文档HttpContent,但没有看到任何相关内容。
大家好,我想要在 GraphQL 客户端的请求正文中传递oauth_token和。client_id那么我该如何传递它们,因为 GraphQLRequest 只有三个字段(即 Query 、 Variables 和 OperationName )。请建议。
using GraphQL.Client;
var heroRequest = new GraphQLRequest{ Query = Query };
var graphQLClient = new GraphQLClient("URL");
var graphQLResponse = await graphQLClient.PostAsync(heroRequest);
Run Code Online (Sandbox Code Playgroud) 我使用的是.NET Framework 4.6.1.
我的web api中有一个控制器,我有静态HttpClient来处理所有的http请求.我在IIS上托管我的应用程序后,大约每月一次,对于我的应用程序的所有传入请求,我收到以下异常:
System.ArgumentNullException: Value cannot be null.
at System.Threading.Monitor.Enter(Object obj)
at System.Net.Http.Headers.HttpHeaders.ParseRawHeaderValues(String name, HeaderStoreItemInfo info, Boolean removeEmptyHeader)
at System.Net.Http.Headers.HttpHeaders.AddHeaders(HttpHeaders sourceHeaders)
at System.Net.Http.Headers.HttpRequestHeaders.AddHeaders(HttpHeaders sourceHeaders)
at System.Net.Http.HttpClient.PrepareRequestMessage(HttpRequestMessage request)
at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.PutAsync(Uri requestUri, HttpContent content, CancellationToken cancellationToken)
at Attributes.Controllers.AttributesBaseController.<UpdateAttributes>d__6.MoveNext() in D:\Git\PortalSystem\Attributes\Controllers\AttributesBaseController.cs:line 42
Run Code Online (Sandbox Code Playgroud)
如果我在IIS上重新启动应用程序池,一切都会再次开始正常工作.这是我的代码:
public class AttributesBaseController : ApiController
{
[Inject]
public IPortalsRepository PortalsRepository { get; set; }
private static HttpClient Client = new HttpClient(new HttpClientHandler { Proxy = null, …Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×2
http ×2
httpclient ×2
apiary.io ×1
asp.net-core ×1
async-await ×1
dynamics-crm ×1
graphql ×1
iis ×1
jwt ×1
netsuite ×1
post ×1