test .
This commit is contained in:
@@ -6,13 +6,6 @@ using System.Text;
|
|||||||
|
|
||||||
namespace HttpUtil
|
namespace HttpUtil
|
||||||
{
|
{
|
||||||
public class HikResponse<T>
|
|
||||||
{
|
|
||||||
public T Data { get; set; }
|
|
||||||
public string Code { get; set; }
|
|
||||||
public string Msg { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class HttpUtillib
|
public class HttpUtillib
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -50,15 +43,15 @@ namespace HttpUtil
|
|||||||
initRequest(header, uri, "", false);
|
initRequest(header, uri, "", false);
|
||||||
|
|
||||||
// build web request object
|
// build web request object
|
||||||
var sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.Append(_isHttps ? "https://" : "http://").Append(_ip).Append(":").Append(_port.ToString()).Append(uri);
|
sb.Append(_isHttps ? "https://" : "http://").Append(_ip).Append(":").Append(_port.ToString()).Append(uri);
|
||||||
|
|
||||||
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sb.ToString());
|
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sb.ToString());
|
||||||
req.KeepAlive = false;
|
req.KeepAlive = false;
|
||||||
req.ProtocolVersion = HttpVersion.Version11;
|
req.ProtocolVersion = HttpVersion.Version11;
|
||||||
req.AllowAutoRedirect = false; // 不允许自动重定向
|
req.AllowAutoRedirect = false; // 不允许自动重定向
|
||||||
req.Method = "GET";
|
req.Method = "GET";
|
||||||
req.Timeout = timeout * 1000; // 传入是秒,需要转换成毫秒
|
req.Timeout = timeout * 1000; // 传入是秒,需要转换成毫秒
|
||||||
req.Accept = header["Accept"];
|
req.Accept = header["Accept"];
|
||||||
req.ContentType = header["Content-Type"];
|
req.ContentType = header["Content-Type"];
|
||||||
|
|
||||||
@@ -76,7 +69,7 @@ namespace HttpUtil
|
|||||||
rsp = (HttpWebResponse)req.GetResponse();
|
rsp = (HttpWebResponse)req.GetResponse();
|
||||||
if (HttpStatusCode.OK == rsp.StatusCode)
|
if (HttpStatusCode.OK == rsp.StatusCode)
|
||||||
{
|
{
|
||||||
Stream rspStream = rsp.GetResponseStream(); // 响应内容字节流
|
Stream rspStream = rsp.GetResponseStream(); // 响应内容字节流
|
||||||
StreamReader sr = new StreamReader(rspStream);
|
StreamReader sr = new StreamReader(rspStream);
|
||||||
string strStream = sr.ReadToEnd();
|
string strStream = sr.ReadToEnd();
|
||||||
long streamLength = strStream.Length;
|
long streamLength = strStream.Length;
|
||||||
@@ -84,30 +77,30 @@ namespace HttpUtil
|
|||||||
rsp.Close();
|
rsp.Close();
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
else if (HttpStatusCode.Found == rsp.StatusCode || HttpStatusCode.Moved == rsp.StatusCode) // 302/301 redirect
|
else if (HttpStatusCode.Found == rsp.StatusCode || HttpStatusCode.Moved == rsp.StatusCode) // 302/301 redirect
|
||||||
{
|
{
|
||||||
string reqUrl = rsp.Headers["Location"].ToString(); // 获取重定向URL
|
string reqUrl = rsp.Headers["Location"].ToString(); // 获取重定向URL
|
||||||
WebRequest wreq = WebRequest.Create(reqUrl); // 重定向请求对象
|
WebRequest wreq = WebRequest.Create(reqUrl); // 重定向请求对象
|
||||||
WebResponse wrsp = wreq.GetResponse(); // 重定向响应
|
WebResponse wrsp = wreq.GetResponse(); // 重定向响应
|
||||||
long streamLength = wrsp.ContentLength; // 重定向响应内容长度
|
long streamLength = wrsp.ContentLength; // 重定向响应内容长度
|
||||||
Stream rspStream = wrsp.GetResponseStream(); // 响应内容字节流
|
Stream rspStream = wrsp.GetResponseStream(); // 响应内容字节流
|
||||||
byte[] response = new byte[streamLength];
|
byte[] response = new byte[streamLength];
|
||||||
rspStream.Read(response, 0, (int)streamLength); // 读取响应内容至byte数组
|
rspStream.Read(response, 0, (int)streamLength); // 读取响应内容至byte数组
|
||||||
rspStream.Close();
|
rspStream.Close();
|
||||||
rsp.Close();
|
rsp.Close();
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
rsp.Close();
|
rsp.Close();
|
||||||
}
|
}
|
||||||
catch (WebException e)
|
catch (WebException e)
|
||||||
{
|
{
|
||||||
if (rsp != null)
|
if (rsp != null)
|
||||||
{
|
{
|
||||||
rsp.Close();
|
rsp.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,9 +126,9 @@ namespace HttpUtil
|
|||||||
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sb.ToString());
|
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sb.ToString());
|
||||||
req.KeepAlive = false;
|
req.KeepAlive = false;
|
||||||
req.ProtocolVersion = HttpVersion.Version11;
|
req.ProtocolVersion = HttpVersion.Version11;
|
||||||
req.AllowAutoRedirect = false; // 不允许自动重定向
|
req.AllowAutoRedirect = false; // 不允许自动重定向
|
||||||
req.Method = "POST";
|
req.Method = "POST";
|
||||||
req.Timeout = timeout * 1000; // 传入是秒,需要转换成毫秒
|
req.Timeout = timeout * 1000; // 传入是秒,需要转换成毫秒
|
||||||
req.Accept = header["Accept"];
|
req.Accept = header["Accept"];
|
||||||
req.ContentType = header["Content-Type"];
|
req.ContentType = header["Content-Type"];
|
||||||
|
|
||||||
@@ -163,7 +156,7 @@ namespace HttpUtil
|
|||||||
{
|
{
|
||||||
if (reqStream != null)
|
if (reqStream != null)
|
||||||
{
|
{
|
||||||
reqStream.Close();
|
reqStream.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@@ -184,11 +177,11 @@ namespace HttpUtil
|
|||||||
rsp.Close();
|
rsp.Close();
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
else if (HttpStatusCode.Found == rsp.StatusCode || HttpStatusCode.Moved == rsp.StatusCode) // 302/301 redirect
|
else if (HttpStatusCode.Found == rsp.StatusCode || HttpStatusCode.Moved == rsp.StatusCode) // 302/301 redirect
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string reqUrl = rsp.Headers["Location"].ToString(); // 如需要重定向URL,请自行修改接口返回此参数
|
string reqUrl = rsp.Headers["Location"].ToString(); // 如需要重定向URL,请自行修改接口返回此参数
|
||||||
rsp.Close();
|
rsp.Close();
|
||||||
WebRequest wreq = WebRequest.Create(reqUrl);
|
WebRequest wreq = WebRequest.Create(reqUrl);
|
||||||
rsp = (HttpWebResponse)wreq.GetResponse();
|
rsp = (HttpWebResponse)wreq.GetResponse();
|
||||||
@@ -210,30 +203,30 @@ namespace HttpUtil
|
|||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
catch(Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
rsp.Close();
|
rsp.Close();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rsp.Close();
|
rsp.Close();
|
||||||
}
|
}
|
||||||
catch (WebException e)
|
catch (WebException e)
|
||||||
{
|
{
|
||||||
if (rsp != null)
|
if (rsp != null)
|
||||||
{
|
{
|
||||||
rsp.Close();
|
rsp.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void initRequest(Dictionary<string, string> header, string url, string body, bool isPost)
|
private static void initRequest(Dictionary<string, string> header, string url, string body, bool isPost)
|
||||||
{
|
{
|
||||||
// Accept
|
// Accept
|
||||||
string accept = "application/json";// "*/*";
|
string accept = "application/json"; // "*/*";
|
||||||
header.Add("Accept", accept);
|
header.Add("Accept", accept);
|
||||||
|
|
||||||
// ContentType
|
// ContentType
|
||||||
@@ -245,7 +238,7 @@ namespace HttpUtil
|
|||||||
// content-md5,be careful it must be lower case.
|
// content-md5,be careful it must be lower case.
|
||||||
string contentMd5 = computeContentMd5(body);
|
string contentMd5 = computeContentMd5(body);
|
||||||
header.Add("content-md5", contentMd5);
|
header.Add("content-md5", contentMd5);
|
||||||
}
|
}
|
||||||
|
|
||||||
// x-ca-timestamp
|
// x-ca-timestamp
|
||||||
string timestamp = ((DateTime.Now.Ticks - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0)).Ticks) / 1000).ToString();
|
string timestamp = ((DateTime.Now.Ticks - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0)).Ticks) / 1000).ToString();
|
||||||
@@ -270,7 +263,8 @@ namespace HttpUtil
|
|||||||
// set remote certificate Validation auto pass
|
// set remote certificate Validation auto pass
|
||||||
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(remoteCertificateValidate);
|
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(remoteCertificateValidate);
|
||||||
// FIX:修复不同.Net版对一些SecurityProtocolType枚举支持情况不一致导致编译失败等问题,这里统一使用数值
|
// FIX:修复不同.Net版对一些SecurityProtocolType枚举支持情况不一致导致编译失败等问题,这里统一使用数值
|
||||||
ServicePointManager.SecurityProtocol = (SecurityProtocolType)48 | (SecurityProtocolType)3072 | (SecurityProtocolType)768 | (SecurityProtocolType)192;
|
//(SecurityProtocolType)48 |
|
||||||
|
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072 | (SecurityProtocolType)768 | (SecurityProtocolType)192;
|
||||||
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
|
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -383,11 +377,13 @@ namespace HttpUtil
|
|||||||
{
|
{
|
||||||
sb.Append(kvp.Value);
|
sb.Append(kvp.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.Append("\n");
|
sb.Append("\n");
|
||||||
if (sbSignHeader.Length > 0)
|
if (sbSignHeader.Length > 0)
|
||||||
{
|
{
|
||||||
sbSignHeader.Append(",");
|
sbSignHeader.Append(",");
|
||||||
}
|
}
|
||||||
|
|
||||||
sbSignHeader.Append(kvp.Key);
|
sbSignHeader.Append(kvp.Key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -415,11 +411,11 @@ namespace HttpUtil
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 平台APPSecret
|
/// 平台APPSecret
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static string _secret;
|
private static string _secret;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 是否使用HTTPS协议
|
/// 是否使用HTTPS协议
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static bool _isHttps = true;
|
private static bool _isHttps = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -49,20 +49,21 @@ public class GainfoFacesController : ControllerBase
|
|||||||
var key = "27390082";
|
var key = "27390082";
|
||||||
var secret = "MLl7Dv7p5Pgc9XYI3r4O";
|
var secret = "MLl7Dv7p5Pgc9XYI3r4O";
|
||||||
|
|
||||||
// HttpUtillib.SetPlatformInfo(key, secret, "10.32.0.10");
|
HttpUtillib.SetPlatformInfo(key, secret, "10.32.0.10");
|
||||||
// HttpUtillib.HttpPost(url, body, 15);
|
var r = HttpUtillib.HttpPost(url, body, 15);
|
||||||
|
return Encoding.UTF8.GetString(r);
|
||||||
|
|
||||||
var headers = InitRequest(url, body, key, secret);
|
// var headers = InitRequest(url, body, key, secret);
|
||||||
client.DefaultRequestHeaders.Accept.Clear();
|
// client.DefaultRequestHeaders.Accept.Clear();
|
||||||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(headers["Accept"]));
|
// client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(headers["Accept"]));
|
||||||
var content = new StringContent(body, Encoding.UTF8, "application/json");
|
// var content = new StringContent(body, Encoding.UTF8, "application/json");
|
||||||
foreach (var item in headers.Where(x => x.Key != "Accept").Where(x => x.Key != "Content-Type"))
|
// foreach (var item in headers.Where(x => x.Key != "Accept").Where(x => x.Key != "Content-Type"))
|
||||||
{
|
// {
|
||||||
content.Headers.Add(item.Key, item.Value);
|
// content.Headers.Add(item.Key, item.Value);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
var r = await client.PostAsync(url, content);
|
// var r = await client.PostAsync(url, content);
|
||||||
return await r.Content.ReadAsStringAsync();
|
// return await r.Content.ReadAsStringAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Dictionary<string, string> InitRequest(string url, string body, string key, string secret)
|
private static Dictionary<string, string> InitRequest(string url, string body, string key, string secret)
|
||||||
|
|||||||
Reference in New Issue
Block a user