C# 通过阿里云 API 实现企业工商数据查询

 

目录

应用场景

关于阿里云企业工商数据查询API

开发运行环境

类设计

类属性

类方法

实现代码

创建 Company 类

调用举例 

小结


应用场景

在企业会员后台注册系统中,为验证企业名称是否输入完整且是有效存在的,则可以通过云API服务的方式进行验证及提取相关的基本信息,自动化提取的企业工商其它信息如法人、企业性质、经营地址等也可以提高录入效率和准确率。

本文将以阿里云提供的 API 服务,实现通过企业名称查询工商数据的功能。

关于阿里云企业工商数据查询API

官方介绍其每天更新全国企业、个体工商户的数据。

更多信息内容请参照:https://market.aliyun.com/products/57000002/cmapi029998.html?spm=5176.21213303.J_qCOwPWspKEuWcmp8qiZNQ.32.3ec42f3dzQ6CjW&scm=20140722.S_market@@%E6%95%B0%E6%8D%AE%E4%B8%8EAPI@@cmapi029998._.ID_market@@%E6%95%B0%E6%8D%AE%E4%B8%8EAPI@@cmapi029998-RL_%E4%BC%81%E4%B8%9A%E5%B7%A5%E5%95%86%E6%95%B0%E6%8D%AEapi%E6%8E%A5%E5%8F%A3-LOC_llm-OR_ser-V_3-RE_new2-P0_0#sku=yuncode2399800001开发前请准备如下操作:

1. 注册阿里云账号。

2. 获取开发者 AppCode,后继开发会用到。

开发运行环境

操作系统: Windows Server 2019 DataCenter

.net版本: .netFramework4.0 或以上

开发工具:VS2019  C#

类设计

类 Company (企业类) 设计见下表:

类属性

序号属性名类型说明
1ErrorMessagestring发生任何异常返回的错误信息
2ResultJsonstring请求返回结果Json完整数据
3creditCodestring社会统一信用代码
4faRenstring法人
5addressstring注册地址
6bussinessDesstring经营范围
7regTypestring公司类型
8regMoneystring注册资金
9bussinessstring营业期限

类方法

queryName 和 queryName2 方法均可以查询(调用地址和方式不同,参数一致),调用均返回对应的类属性数据,参数见如下表格:

序号参数名类型说明
1CompanyNamestring传递完整的企业名称

本方法返回 string 类型的对应属性值(如果成功的话)。

实现代码

创建 Company 类

public class Company
{
            public string ResultJson="";
            public string ErrorMessage = "";
            public string creditCode = "";
            public string faRen = "";
            public string address = "";
            public string bussinessDes = "";
            public string regType = "";
            public string regMoney = "";
            public string bussiness = "";
            public void queryName(string CompanyName)
            {
                
                String host = "http://qianzhan1.market.alicloudapi.com";
                String path = "/CommerceAccurate";
                String method = "GET";
                String appcode = "您的AppCode";
                String querys = "comName=" + System.Web.HttpUtility.UrlEncode(CompanyName);
                String bodys = "";
                String url = host + path;
                HttpWebRequest httpRequest = null;
                HttpWebResponse httpResponse = null;
                if (0 < querys.Length)
                {
                    url = url + "?" + querys;
                }
                if (host.Contains("https://"))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
                }
                else
                {
                    httpRequest = (HttpWebRequest)WebRequest.Create(url);
                }
                httpRequest.Method = method;
                httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
                if (0 < bodys.Length)
                {
                    byte[] data = Encoding.UTF8.GetBytes(bodys);
                    using (Stream stream = httpRequest.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
                }
                try
                {
                    httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                }
                catch (WebException ex)
                {
                    ErrorMessage = ex.Message;
                    httpResponse = (HttpWebResponse)ex.Response;
                    return;
                }
                Stream st = httpResponse.GetResponseStream();
                StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                ResultJson=(reader.ReadToEnd());
                if (ResultJson.IndexOf("查询成功") == -1)
                {
                    return;
                }
                Newtonsoft.Json.Linq.JObject jsonObj = Newtonsoft.Json.Linq.JObject.Parse(ResultJson);
                creditCode = jsonObj["result"]["creditCode"].ToString();
                faRen = jsonObj["result"]["faRen"].ToString();
                address = jsonObj["result"]["address"].ToString();
                bussinessDes = jsonObj["result"]["bussinessDes"].ToString();
                regType = jsonObj["result"]["regType"].ToString();
                regMoney = jsonObj["result"]["regMoney"].ToString();
                bussiness = jsonObj["result"]["bussiness"].ToString();
            }
            public void queryName2(string CompanyName)
            {
                //Zm1qSqdjl2296ixnA6ODXpglwQKYxkSD
                String host = "https://cardnotwo.market.alicloudapi.com";
                String path = "/company";
                String method = "POST";
                String appcode = ""您的AppCode";";
                String querys = "com=" + System.Web.HttpUtility.UrlEncode(CompanyName);
                String bodys = "";
                String url = host + path;
                HttpWebRequest httpRequest = null;
                HttpWebResponse httpResponse = null;
                if (0 < querys.Length)
                {
                    url = url + "?" + querys;
                }
                if (host.Contains("https://"))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
                }
                else
                {
                    httpRequest = (HttpWebRequest)WebRequest.Create(url);
                }
                httpRequest.Method = method;
                httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
                if (0 < bodys.Length)
                {
                    byte[] data = Encoding.UTF8.GetBytes(bodys);
                    using (Stream stream = httpRequest.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
                }
                try
                {
                    httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                }
                catch (WebException ex)
                {
                    ErrorMessage = ex.Message;
                    httpResponse = (HttpWebResponse)ex.Response;
                    return;
                }
                Stream st = httpResponse.GetResponseStream();
                StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                ResultJson = (reader.ReadToEnd());
                if (ResultJson.IndexOf("查询成功") == -1)
                {
                    return;
                }
                Newtonsoft.Json.Linq.JObject jsonObj = Newtonsoft.Json.Linq.JObject.Parse(ResultJson);
                creditCode = jsonObj["result"]["creditCode"].ToString();
                faRen = jsonObj["result"]["faRen"].ToString();
                address = jsonObj["result"]["address"].ToString();
                bussinessDes = jsonObj["result"]["bussinessDes"].ToString();
                regType = jsonObj["result"]["regType"].ToString();
                regMoney = jsonObj["result"]["regMoney"].ToString();
                bussiness = jsonObj["result"]["bussiness"].ToString();
            }
}

调用举例 

调用判断是否返回社会统一信用代码,示例代码如下:

Company cp = new Company();
cp.queryName2("天津XXXX数码有限公司");
if (cp.creditCode != "")
{
    Response.Write("社会统一信用代码:" + cp.creditCode + "
"); Response.Write("法人:" + cp.faRen + "
"); Response.Write("注册地址:" + cp.address + "
"); Response.Write("营业范围:" + cp.bussinessDes + "
"); Response.Write("企业性质:" + cp.regType + "
"); Response.Write("注册资金:" + cp.regMoney + "
"); Response.Write("营业期限:" + cp.bussiness + "
"); } else { Response.Write("错误信息:" + cp.ErrorMessage + "
"); Response.Write("JSON返回信息:" + cp.ResultJson + "
"); }

小结

调用云接口服务需要费用,我们需要根据实际应用进行成本考虑,官方说明如果查询失败则不扣除费用,具体内容可参考本文第二小节关于阿里云企业工商数据查询API中的链接。

感谢您的阅读,希望本文能够对您有所帮助。