在调用 [顺序请求] C# 之前授权 HTTP 请求
Authorize HTTP requests before calling [in-sequence requests] C#
我需要在调用每个 HTTP 请求之前对其进行授权,除非访问令牌尚未过期。
我想:
1.第一次获取access token
2.检查令牌是否没有过期
3. 在下一个 HTTP 请求中包装 returned 密钥
代码:
首先,我使用 System.Net.WebRequest 进行 HTTP 请求,
然后请求 return 一个令牌,我将其包装在请求中。
HTTP 请求:
/// <summary>
/// HTTP request without authintcation
/// </summary>
/// <param name="response">Returned HTTP Response from the request</param>
/// <param name="req_url">Request URL to call</param>
/// <param name="req_method">Request Method [GET, POST, PUT, DELETE, etc ...]</param>
/// <param name="req_body">Request Body [JSON]</param>
/// <returns>True if the request was successful; false otherwise.</returns>
private static bool HTTP_Request_Web(out HttpWebResponse response, string req_url,
string req_method, string req_body)
{
response = null;
try
{
//Create request to URL.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(req_url);
//Set request headers.
request.KeepAlive = true;
//request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36";
request.Headers.Set(HttpRequestHeader.CacheControl, "no-cache");
request.ContentType = "application/json";
request.Accept = "*/*";
request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.9,ar;q=0.8");
//Set request method
request.Method = req_method;
// Disable 'Expect: 100-continue' behavior.
request.ServicePoint.Expect100Continue = false;
//Set request body.
//string body = @"{
// ""username"":""XXXX"",
// ""password"":""XXXXXXX""
// }";
byte[] postBytes = Encoding.UTF8.GetBytes(req_body);
request.ContentLength = postBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();
//Get response to request.
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException e)
{
//ProtocolError indicates a valid HTTP response, but with a non-200 status code (e.g. 304 Not Modified, 404 Not Found)
if (e.Status == WebExceptionStatus.ProtocolError) response = (HttpWebResponse)e.Response;
else return false;
}
catch (Exception)
{
if (response != null) response.Close();
return false;
}
return true;
}
之后我使用令牌,在下一个请求中,如下图所示:
我使用了来自 Magento 的固定集成令牌。
我需要在调用每个 HTTP 请求之前对其进行授权,除非访问令牌尚未过期。
我想: 1.第一次获取access token 2.检查令牌是否没有过期 3. 在下一个 HTTP 请求中包装 returned 密钥
代码: 首先,我使用 System.Net.WebRequest 进行 HTTP 请求, 然后请求 return 一个令牌,我将其包装在请求中。
HTTP 请求:
/// <summary>
/// HTTP request without authintcation
/// </summary>
/// <param name="response">Returned HTTP Response from the request</param>
/// <param name="req_url">Request URL to call</param>
/// <param name="req_method">Request Method [GET, POST, PUT, DELETE, etc ...]</param>
/// <param name="req_body">Request Body [JSON]</param>
/// <returns>True if the request was successful; false otherwise.</returns>
private static bool HTTP_Request_Web(out HttpWebResponse response, string req_url,
string req_method, string req_body)
{
response = null;
try
{
//Create request to URL.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(req_url);
//Set request headers.
request.KeepAlive = true;
//request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36";
request.Headers.Set(HttpRequestHeader.CacheControl, "no-cache");
request.ContentType = "application/json";
request.Accept = "*/*";
request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.9,ar;q=0.8");
//Set request method
request.Method = req_method;
// Disable 'Expect: 100-continue' behavior.
request.ServicePoint.Expect100Continue = false;
//Set request body.
//string body = @"{
// ""username"":""XXXX"",
// ""password"":""XXXXXXX""
// }";
byte[] postBytes = Encoding.UTF8.GetBytes(req_body);
request.ContentLength = postBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();
//Get response to request.
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException e)
{
//ProtocolError indicates a valid HTTP response, but with a non-200 status code (e.g. 304 Not Modified, 404 Not Found)
if (e.Status == WebExceptionStatus.ProtocolError) response = (HttpWebResponse)e.Response;
else return false;
}
catch (Exception)
{
if (response != null) response.Close();
return false;
}
return true;
}
之后我使用令牌,在下一个请求中,如下图所示:
我使用了来自 Magento 的固定集成令牌。