0
当它只是授权头文件时,我有此工作。但现在我需要添加一个自定义标题。 tokenResponse中出现错误,标题为“HEADER OUT OF RANGE”。向HttpClient添加两个头文件
这里的端点信息:
URI: https://api.xyzcompany.com/api/v10/proxyUserToken
METHOD: POST
Params: email
Required headers: X-iMem-Date, Authorization
我的代码。
//get current date and time
DateTime dt = DateTime.Now;
string date = string.Format("{0:ddd}, {0: dd MMM yyyy HH:mm:ss} GMT", dt);
//hash together for header
string strToHash = secret + date + "[email protected]";
string hash = SHA.Generate256string(strToHash);
//setup the values we need to post
var values2 = new Dictionary<string, string>();
values2.Add("email", "[email protected]");
var content2 = new FormUrlEncodedContent(values2);
//setup the auth header
string auth = string.Format("IMEM {0}:{1}", token, hash);
//setup client and add the headers
HttpClient client2 = new HttpClient();
client2.BaseAddress = new Uri(postURL);
client2.DefaultRequestHeaders.Add("X-iMem-Date", date);
client2.DefaultRequestHeaders.Add("Authorization", auth);
//post and get responses
HttpResponseMessage response2 = await client2.PostAsync(new Uri(postURL), content2);
var tokenresponse = await response2.Content.ReadAsStringAsync();
if (response2.IsSuccessStatusCode)
{
...do stuff...
}
使这些变化,但仍然获得头超出范围形成
Request
,新Uri(postURL)); request.Headers.Add(“X-iMem-Date”,date); request.Headers.Add(“Authorization”,auth); request.Content = content; response.Content.ReadAsStringAsync(); var response = await httpclient.SendAsync(request); –是您的日期字符串格式正确,服务器预期的格式? –
也使用'DateTime.UtcNow'而不是'DateTime.Now' –