一、文字内容检验
普通的pos提交就行,utf-8编码格式
/// <summary>
/// 执行内容安全验证
/// </summary>
/// <param name="content"></param>
public bool CheckMsg(string content)
{
string url = new LinkManage().GetImg_Sec_Check();
string data = NetHelper.Post(url, new { content = content }.ToJsonString());
JObject result = JObject.Parse(data);
string code = result.Root.SelectToken("errcode").ToString();
if (code == "87014")
return false;
return true;
}
二、图片检验、图片鉴黄等
需要form提交,提交二进制文件数据
特别说明:content-type 的值:application/octet-stream
1.使用HttpClient
/// <summary>
/// 执行内容安全验证
/// </summary>
/// <param name="content"></param>
public bool CheckImg(string imgPath)
{
string url = new LinkManage().GetImg_Sec_Check();
//读取文件内容
if (File.Exists(imgPath) == false)
throw new Exception("图片文件不存在");
byte[] imgData = File.ReadAllBytes(imgPath);
using (HttpClient client = new HttpClient())
{
ByteArrayContent array = new ByteArrayContent(imgData);
//数据类型,需要对应
array.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
HttpResponseMessage res = client.PostAsync(url, array).Result;
string data = res.Content.ReadAsStringAsync().Result;
JObject result = JObject.Parse(data);
string code = result.Root.SelectToken("errcode").ToString();
if (code == "87014")
return false;
return true;
}
}
2.使用HttpWebRequest
/// <summary>
/// 执行内容安全验证
/// </summary>
/// <param name="content"></param>
public bool CheckImg(string imgPath)
{
string url = new LinkManage().GetImg_Sec_Check();
//读取文件内容
if (File.Exists(imgPath) == false)
throw new Exception("图片文件不存在");
byte[] imgData = File.ReadAllBytes(imgPath);
Uri uriurl = new Uri(url);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uriurl);//HttpWebRequest req = (HttpWebRequest)
req.Method = "Post";
req.Timeout = 120 * 1000;
req.ContentType = "application/octet-stream";
req.ContentLength = imgData.Length;
using (Stream reqStream = req.GetRequestStream())//using 使用可以释放using段内的内存
{
reqStream.Write(imgData, 0, imgData.Length);
reqStream.Flush();
}
try
{
using (WebResponse res = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
Stream resStream = res.GetResponseStream();
StreamReader resStreamReader = new StreamReader(resStream, System.Text.Encoding.UTF8);
string data = resStreamReader.ReadToEnd();
resStream.Close();
resStreamReader.Close();
JObject result = JObject.Parse(data);
string code = result.Root.SelectToken("errcode").ToString();
if (code == "87014")
return false;
return true;
}
}
catch (Exception ex)
{
throw ex;
}
}
更多:
来源:oschina
链接:https://my.oschina.net/tianma3798/blog/3192437