科大讯飞接口 ASP.NET C# 实现

using System;
using System.Text;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Security.Cryptography;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {







    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        AppSettings _settings = new AppSettings(); 
      
        string imgData = ReadImgToString(FileUpload1);
        Uri url = new Uri(_settings.Url);



        string body = "{\"common\":{\"app_id\":\""+_settings.AppID+ "\"}, \"business\":{ \"ent\":\"teach-photo-print\",  \"aue\" :\"raw\"   },\"data\":{\"image\":\""+ imgData+"\"}}";
        string digestBase64 = "SHA-256=" + HMACSha256(body);


        DateTime basic = DateTime.UtcNow;
        string date = basic.ToString("r");
        string signatureOrigin = "host: " + url.Host + "\ndate: " + date + "\nPOST /v2/itr HTTP/1.1\ndigest: " + digestBase64;
        string sha = HMAC_SHA256(signatureOrigin, _settings.ApiSecret);

        string authorization = "api_key=\"" + _settings.ApiKey + "\", algorithm=\"hmac-sha256\", headers=\"host date request-line digest\", signature=\"" + sha + "\"";
      



        HttpWebRequest request =  (HttpWebRequest)HttpWebRequest.Create(_settings.Url);
        request.Method = "POST";
        request.ContentType = "application/json";
        request.Host = url.Host;
        request.Date = basic;
        request.Headers.Set("Digest", digestBase64);  
        request.Headers.Set("Authorization", authorization); 

       
          

        byte[] postBytes = null;
        postBytes = Encoding.UTF8.GetBytes(body);
        request.ContentLength = postBytes.Length;

        using (Stream reqStream = request.GetRequestStream())
        {
            reqStream.Write(postBytes, 0, postBytes.Length);
        }

        string result = string.Empty;

        using (WebResponse response = request.GetResponse())
        {
            if (response != null)
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        result = reader.ReadToEnd();
                    }
                }

            }
        }

        Response.Write(result);
  


    }
 

      private string HMAC_SHA256(string message, string secret)
    {
  
        byte[] keyByte = System.Text.Encoding.UTF8.GetBytes(secret); 
        byte[] messageBytes = System.Text.Encoding.UTF8.GetBytes(message);

        HMACSHA256 hmacsha256 = new HMACSHA256(keyByte);
       
        byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
   
        return Convert.ToBase64String(hashmessage);
        
    }




    private static string HMACSha256(string body)
    {

        HMACSHA256 hMACSHA256 = new HMACSHA256(); 
        byte[] date = Encoding.UTF8.GetBytes(body);
        date = hMACSHA256.ComputeHash(date);
        hMACSHA256.Clear();
        return System.Convert.ToBase64String(date);
       
    }




    public string ReadImgToString(FileUpload f)
    {
        Stream fstream = f.PostedFile.InputStream;
        byte[] imgData = new byte[fstream.Length];
        fstream.Read(imgData, 0, imgData.Length);
        fstream.Close();
        string imgdata = Convert.ToBase64String(imgData);
        return imgdata;

    }



}

  下面是一个简单的类

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

 
    public class AppSettings
    {
 
    public string AppID = "5666666665"; 
    public string ApiSecret = "7c6667fd0fb0034e0d2df6263d678efd"; 
    public string ApiKey = "b9ed1e666d01f74905b17bc3b9a4063"; 
    public string Url  = "http://rest-api.xfyun.cn/v2/itr";

      
    }
 

  

posted @ 2022-03-15 07:29  启明星工作室  阅读(10)  评论(0编辑  收藏  举报