本文整理汇总了C#中System.Net.Http.HttpRequestMessage.SignRequest方法的典型用法代码示例。如果您正苦于以下问题:C# HttpRequestMessage.SignRequest方法的具体用法?C# HttpRequestMessage.SignRequest怎么用?C# HttpRequestMessage.SignRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpRequestMessage
的用法示例。
在下文中一共展示了HttpRequestMessage.SignRequest方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendAsync
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
string payloadHash = null;
if (this.includePayloadHash &&
request.Method != HttpMethod.Get &&
request.Content != null)
{
var hmac = System.Security.Cryptography.HMAC.Create(credential.Algorithm);
hmac.Key = Encoding.ASCII.GetBytes(credential.Key);
var task = request.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
var payload = task.GetAwaiter().GetResult();
payloadHash = Convert.ToBase64String(hmac.ComputeHash(payload));
}
request.Headers.Host = request.RequestUri.Host;
request.SignRequest(credential,
this.ext,
this.ts,
this.nonce,
payloadHash);
return base.SendAsync(request, cancellationToken);
}
示例2: ShouldSignRequest
public void ShouldSignRequest()
{
var credential = new HawkCredential
{
Id = "456",
Algorithm = "hmacsha256",
Key = "werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn",
User = "steve"
};
var date = DateTime.Now;
var ts = Hawk.ConvertToUnixTimestamp(date);
var mac = Hawk.CalculateMac("example.com:8080", "get", new Uri("http://example.com:8080/resource/4?filter=a"), "hello", ts.ToString(), "k3j4h2", credential, "header");
var authorization = string.Format("id=\"456\", ts=\"{0}\", nonce=\"k3j4h2\", mac=\"{1}\", ext=\"hello\"",
ts, mac);
var request = new HttpRequestMessage(HttpMethod.Get, "http://example.com:8080/resource/4?filter=a");
request.SignRequest(credential, "hello", date, "k3j4h2", null);
Assert.IsNotNull(request.Headers.Authorization != null);
Assert.AreEqual(request.Headers.Authorization.Scheme, "Hawk");
Assert.AreEqual(request.Headers.Authorization.Parameter, authorization);
}
示例3: SendAsync
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
string payloadHash = null;
if (this.includePayloadHash &&
request.Method != HttpMethod.Get &&
request.Content != null)
{
payloadHash = Hawk.CalculatePayloadHash(await request.Content.ReadAsStringAsync().ConfigureAwait(false),
request.Content.Headers.ContentType.MediaType,
credential);
}
request.Headers.Host = request.RequestUri.Host;
request.SignRequest(credential,
this.ext,
this.ts,
this.nonce,
payloadHash);
return await base.SendAsync(request, cancellationToken);
}
示例4: Tweet
public void Tweet(string message)
{
string resourceUrl = Twitter.API_V1_1 + "statuses/update.json";
if (string.IsNullOrEmpty(message))
{
throw new TwitterClientException("Tweet cannot be empty.");
}
if (message.Length > 140)
{
throw new TwitterClientException("Tweet cannot be longer than 140 characters.");
}
// This is the content
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("status", message));
// Load content
HttpContent content = new FormUrlEncodedContent(postData);
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
// create a new Request Message
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, resourceUrl);
requestMessage.Content = content;
// Sign Request
requestMessage.SignRequest(postData, this.authorization);
var response = _client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead).Result;
HandleErrors(response);
}