test .
This commit is contained in:
@@ -6,13 +6,6 @@ using System.Text;
|
||||
|
||||
namespace HttpUtil
|
||||
{
|
||||
public class HikResponse<T>
|
||||
{
|
||||
public T Data { get; set; }
|
||||
public string Code { get; set; }
|
||||
public string Msg { get; set; }
|
||||
}
|
||||
|
||||
public class HttpUtillib
|
||||
{
|
||||
/// <summary>
|
||||
@@ -50,15 +43,15 @@ namespace HttpUtil
|
||||
initRequest(header, uri, "", false);
|
||||
|
||||
// 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);
|
||||
|
||||
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sb.ToString());
|
||||
req.KeepAlive = false;
|
||||
req.ProtocolVersion = HttpVersion.Version11;
|
||||
req.AllowAutoRedirect = false; // 不允许自动重定向
|
||||
req.AllowAutoRedirect = false; // 不允许自动重定向
|
||||
req.Method = "GET";
|
||||
req.Timeout = timeout * 1000; // 传入是秒,需要转换成毫秒
|
||||
req.Timeout = timeout * 1000; // 传入是秒,需要转换成毫秒
|
||||
req.Accept = header["Accept"];
|
||||
req.ContentType = header["Content-Type"];
|
||||
|
||||
@@ -76,7 +69,7 @@ namespace HttpUtil
|
||||
rsp = (HttpWebResponse)req.GetResponse();
|
||||
if (HttpStatusCode.OK == rsp.StatusCode)
|
||||
{
|
||||
Stream rspStream = rsp.GetResponseStream(); // 响应内容字节流
|
||||
Stream rspStream = rsp.GetResponseStream(); // 响应内容字节流
|
||||
StreamReader sr = new StreamReader(rspStream);
|
||||
string strStream = sr.ReadToEnd();
|
||||
long streamLength = strStream.Length;
|
||||
@@ -84,30 +77,30 @@ namespace HttpUtil
|
||||
rsp.Close();
|
||||
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
|
||||
WebRequest wreq = WebRequest.Create(reqUrl); // 重定向请求对象
|
||||
WebResponse wrsp = wreq.GetResponse(); // 重定向响应
|
||||
long streamLength = wrsp.ContentLength; // 重定向响应内容长度
|
||||
Stream rspStream = wrsp.GetResponseStream(); // 响应内容字节流
|
||||
string reqUrl = rsp.Headers["Location"].ToString(); // 获取重定向URL
|
||||
WebRequest wreq = WebRequest.Create(reqUrl); // 重定向请求对象
|
||||
WebResponse wrsp = wreq.GetResponse(); // 重定向响应
|
||||
long streamLength = wrsp.ContentLength; // 重定向响应内容长度
|
||||
Stream rspStream = wrsp.GetResponseStream(); // 响应内容字节流
|
||||
byte[] response = new byte[streamLength];
|
||||
rspStream.Read(response, 0, (int)streamLength); // 读取响应内容至byte数组
|
||||
rspStream.Read(response, 0, (int)streamLength); // 读取响应内容至byte数组
|
||||
rspStream.Close();
|
||||
rsp.Close();
|
||||
rsp.Close();
|
||||
return response;
|
||||
}
|
||||
|
||||
rsp.Close();
|
||||
}
|
||||
catch (WebException e)
|
||||
{
|
||||
{
|
||||
if (rsp != null)
|
||||
{
|
||||
rsp.Close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -133,9 +126,9 @@ namespace HttpUtil
|
||||
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sb.ToString());
|
||||
req.KeepAlive = false;
|
||||
req.ProtocolVersion = HttpVersion.Version11;
|
||||
req.AllowAutoRedirect = false; // 不允许自动重定向
|
||||
req.AllowAutoRedirect = false; // 不允许自动重定向
|
||||
req.Method = "POST";
|
||||
req.Timeout = timeout * 1000; // 传入是秒,需要转换成毫秒
|
||||
req.Timeout = timeout * 1000; // 传入是秒,需要转换成毫秒
|
||||
req.Accept = header["Accept"];
|
||||
req.ContentType = header["Content-Type"];
|
||||
|
||||
@@ -163,7 +156,7 @@ namespace HttpUtil
|
||||
{
|
||||
if (reqStream != null)
|
||||
{
|
||||
reqStream.Close();
|
||||
reqStream.Close();
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -184,11 +177,11 @@ namespace HttpUtil
|
||||
rsp.Close();
|
||||
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
|
||||
{
|
||||
string reqUrl = rsp.Headers["Location"].ToString(); // 如需要重定向URL,请自行修改接口返回此参数
|
||||
string reqUrl = rsp.Headers["Location"].ToString(); // 如需要重定向URL,请自行修改接口返回此参数
|
||||
rsp.Close();
|
||||
WebRequest wreq = WebRequest.Create(reqUrl);
|
||||
rsp = (HttpWebResponse)wreq.GetResponse();
|
||||
@@ -210,30 +203,30 @@ namespace HttpUtil
|
||||
|
||||
return response;
|
||||
}
|
||||
catch(Exception e)
|
||||
catch (Exception e)
|
||||
{
|
||||
rsp.Close();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rsp.Close();
|
||||
}
|
||||
catch (WebException e)
|
||||
{
|
||||
{
|
||||
if (rsp != null)
|
||||
{
|
||||
rsp.Close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void initRequest(Dictionary<string, string> header, string url, string body, bool isPost)
|
||||
{
|
||||
// Accept
|
||||
string accept = "application/json";// "*/*";
|
||||
string accept = "application/json"; // "*/*";
|
||||
header.Add("Accept", accept);
|
||||
|
||||
// ContentType
|
||||
@@ -245,7 +238,7 @@ namespace HttpUtil
|
||||
// content-md5,be careful it must be lower case.
|
||||
string contentMd5 = computeContentMd5(body);
|
||||
header.Add("content-md5", contentMd5);
|
||||
}
|
||||
}
|
||||
|
||||
// x-ca-timestamp
|
||||
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
|
||||
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(remoteCertificateValidate);
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
@@ -383,11 +377,13 @@ namespace HttpUtil
|
||||
{
|
||||
sb.Append(kvp.Value);
|
||||
}
|
||||
|
||||
sb.Append("\n");
|
||||
if (sbSignHeader.Length > 0)
|
||||
{
|
||||
sbSignHeader.Append(",");
|
||||
}
|
||||
|
||||
sbSignHeader.Append(kvp.Key);
|
||||
}
|
||||
}
|
||||
@@ -415,11 +411,11 @@ namespace HttpUtil
|
||||
/// <summary>
|
||||
/// 平台APPSecret
|
||||
/// </summary>
|
||||
private static string _secret;
|
||||
|
||||
private static string _secret;
|
||||
|
||||
/// <summary>
|
||||
/// 是否使用HTTPS协议
|
||||
/// </summary>
|
||||
private static bool _isHttps = true;
|
||||
private static bool _isHttps = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,20 +49,21 @@ public class GainfoFacesController : ControllerBase
|
||||
var key = "27390082";
|
||||
var secret = "MLl7Dv7p5Pgc9XYI3r4O";
|
||||
|
||||
// HttpUtillib.SetPlatformInfo(key, secret, "10.32.0.10");
|
||||
// HttpUtillib.HttpPost(url, body, 15);
|
||||
HttpUtillib.SetPlatformInfo(key, secret, "10.32.0.10");
|
||||
var r = HttpUtillib.HttpPost(url, body, 15);
|
||||
return Encoding.UTF8.GetString(r);
|
||||
|
||||
var headers = InitRequest(url, body, key, secret);
|
||||
client.DefaultRequestHeaders.Accept.Clear();
|
||||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(headers["Accept"]));
|
||||
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"))
|
||||
{
|
||||
content.Headers.Add(item.Key, item.Value);
|
||||
}
|
||||
|
||||
var r = await client.PostAsync(url, content);
|
||||
return await r.Content.ReadAsStringAsync();
|
||||
// var headers = InitRequest(url, body, key, secret);
|
||||
// client.DefaultRequestHeaders.Accept.Clear();
|
||||
// client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(headers["Accept"]));
|
||||
// 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"))
|
||||
// {
|
||||
// content.Headers.Add(item.Key, item.Value);
|
||||
// }
|
||||
//
|
||||
// var r = await client.PostAsync(url, content);
|
||||
// return await r.Content.ReadAsStringAsync();
|
||||
}
|
||||
|
||||
private static Dictionary<string, string> InitRequest(string url, string body, string key, string secret)
|
||||
|
||||
Reference in New Issue
Block a user