废话不多说 直接上代码
这里使用的是人工智能里面的智能图像--万象优图 没有用天御的(太贵了) 首先要开通智能图像的功能
注意:这里要绑定Bucket 使用主账号的appid,不是协作者的
var urlList = new List
urlList.Add(url);
//请求参数
var req = new { appid = ConfigurationManager.AppSettings["AppId"], url_list = urlList };
var psotJson = JsonConvert.SerializeObject(req);
TencentCloundPicHelper tencentHelper = new TencentCloundPicHelper();
var info = tencentHelper.SendPost(psotJson);
TencentJianHuangResponse response = JsonConvert.DeserializeObject
if (response != null && response.result_list.Any())
{
if (response.result_list.FirstOrDefault().code == 0)//成功
{
if (response.result_list.FirstOrDefault().data.result != 0)//是黄图
{
responseMessage.Message = CommonConst.PicNolegal;
responseMessage.ResultCode = "555";
responseMessage.Info = new { Url = url, TencentJson = info };
return responseMessage;
}
}
}
public class TencentCloundPicHelper
{
///
/// 发送Post请求腾讯云
///
public string SendPost(string postDataStr)
{
var url = @"http://service.image.myqcloud.com/detection/porn_detect";
try
{
System.Net.ServicePointManager.Expect100Continue = false;
var request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
SetHeaderValue(request.Headers, "content-type", "application/json");
SetHeaderValue(request.Headers, "content-length", postDataStr.Length.ToString());
SetHeaderValue(request.Headers, "Host", "service.image.myqcloud.com");
SetHeaderValue(request.Headers, "authorization", GetSign());
var memStream = new MemoryStream();
var jsonByte = Encoding.GetEncoding("utf-8").GetBytes(postDataStr);
memStream.Write(jsonByte, 0, jsonByte.Length);
request.ContentLength = memStream.Length;
var requestStream = request.GetRequestStream();
memStream.Position = 0;
var tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
var response = request.GetResponse();
using (var s = response.GetResponseStream())
{
var reader = new StreamReader(s, Encoding.UTF8);
return reader.ReadToEnd();
}
}
catch (WebException we)
{
if (we.Status == WebExceptionStatus.ProtocolError)
{
using (var s = we.Response.GetResponseStream())
{
var reader = new StreamReader(s, Encoding.UTF8);
return reader.ReadToEnd();
}
}
else
{
throw we;
}
}
catch (Exception e)
{
throw e;
}
}
///
/// 获取签名
///
public static string GetSign()
{
var appId = ConfigurationManager.AppSettings["AppId"];//开发者的 APPID,接入智能图像时由系统生成
var bucket = "tengxunyun";//Bucket,空间名称,即图片资源的组织管理单元
var secretId = ConfigurationManager.AppSettings["SecretId"];//Secret ID
var secretKey = ConfigurationManager.AppSettings["SecretKey"];//secretKey
var ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
var currentTime = Convert.ToInt64(ts.TotalSeconds);//当前时间戳,是一个符合 UNIX Epoch 时间戳规范的数值,单位为秒,多次签名时,e 应大于 t
var expiredTime = Convert.ToInt64((DateTime.UtcNow.AddMinutes(180) - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds);//签名的有效期,是一个符合 UNIX Epoch 时间戳规范的数值,单位为秒;单次签名时,e 必须设置为 0
var rand = GetRandom();//随机串,无符号 10 进制整数,用户需自行生成,最长 10 位
var userid = 0;//历史遗留字段,请填写为 0
var fileid = "";//资源存储的唯一标识,单次签名必填;多次签名选填,如填写则会验证与当前操作的文件路径是否一致。
var encryptText = $"a={appId}&b={bucket}&k={secretId}&e={expiredTime}&t={currentTime}&r={rand}&u=0&f=";
var sign = Hmacsha1Encrypt(encryptText, secretKey);
return sign;
}
///
/// 设置Http post请求头
///
public static void SetHeaderValue(WebHeaderCollection header, string name, string value)
{
var property = typeof(WebHeaderCollection).GetProperty("InnerCollection", BindingFlags.Instance | BindingFlags.NonPublic);
if (property != null)
{
var collection = property.GetValue(header, null) as NameValueCollection;
collection[name] = value;
}
}
///
/// HMACSHA1算法加密
///
private static string Hmacsha1Encrypt(string encryptText, string encryptKey)
{
using (HMACSHA1 mac = new HMACSHA1(Encoding.UTF8.GetBytes(encryptKey)))
{
var hash = mac.ComputeHash(Encoding.UTF8.GetBytes(encryptText));
var pText = Encoding.UTF8.GetBytes(encryptText);
var all = new byte[hash.Length + pText.Length];
Array.Copy(hash, 0, all, 0, hash.Length);
Array.Copy(pText, 0, all, hash.Length, pText.Length);
return Convert.ToBase64String(all);
}
}
///
/// 获取随机数
///
private static int GetRandom()
{
var random = new Random();
var rand = random.Next(10000, 999999999);
return rand;
}
}