tnblog
首页
视频
资源
登录
愿你出走半生,归来仍是少年
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术

c#调用阿里云实现图像打标

4082人阅读 2020/2/17 17:04 总访问:1601215 评论:0 收藏:0 手机
分类: 人工智能

url方式:

public void Invoke()
{
    //ak,sk信息设置
    string ak_id = "your ak_id";
    string ak_secret = "your ak_secret";

    string method = "POST";
    string accept = "application/json";
    string contentType = accept;
    string body = "{\"type\":0, \"image_url\":\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1581938602023&di=d760709c2b6602b0b7d470de2e1f691d&imgtype=0&src=http%3A%2F%2Fimg.mm4000.com%2Ffile%2Fc%2F7d%2F0e31e767b7_800.jpg\"}";
    byte[] data = Encoding.ASCII.GetBytes(body);
    string date = DateTime.UtcNow.GetDateTimeFormats('r')[0].ToString();
    string urlpra = "/image/tag";
    string bodyMd5 = Convert.ToBase64String(new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(body)));
    string result = "";

    //待加密
    string stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + contentType + "\n" + date + "\n" + urlpra;

    //stringToSign计算 HMAC-SHA1得到signature
    string signature = Convert.ToBase64String(new System.Security.Cryptography.HMACSHA1(Encoding.UTF8.GetBytes(ak_secret)).ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));

    //得到 authorization header
    string authHeader = "Dataplus " + ak_id + ":" + signature;
    WebRequest req = WebRequest.Create("https://dtplus-cn-shanghai.data.aliyuncs.com/image/tag");
    req.Method = method;
    req.ContentType = accept;

    req.GetRequestStream().Write(data, 0, data.Length);

    //利用解决:"此标头必须使用适当的属性进行修改" 异常
    MethodInfo priMethod = req.Headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic);
    priMethod.Invoke(req.Headers, new[] { "Date", date });
    priMethod.Invoke(req.Headers, new[] { "Authorization", authHeader });
    priMethod.Invoke(req.Headers, new[] { "Accept", accept });

    string t = DateTime.UtcNow.GetDateTimeFormats('r')[0].ToString();
    try
    {
        result = new StreamReader(req.GetResponse().GetResponseStream()).ReadToEnd();
        Console.WriteLine("result:" + result);
    }
    catch (WebException e)
    {
        Console.WriteLine(e.GetBaseException());
        Console.WriteLine(new StreamReader(e.Response.GetResponseStream()).ReadToEnd());
    }
}

base64方式:




欢迎加群讨论技术,群:677373950(满了,可以加,但通过不了),2群:656732739

评价