Yytan-BK

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

C# 调用接口(三)

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml.Linq;

namespace _23_0820_2218
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string barcode = "6974088970323";
            GetWineMatinfoNetwork(barcode);
        }
        public static DataTable GetWineMatinfoNetwork(string barcode)
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("MATNAME");
            dataTable.Columns.Add("BOSPECNO");
            dataTable.Columns.Add("SPECNAME");
            dataTable.Columns.Add("MATGP");
            dataTable.Columns.Add("VAT", typeof(DateTime));
            dataTable.Columns.Add("BRAND");
            dataTable.Columns.Add("SKU");

            string host = "https://jisutxmcx.market.alicloudapi.com";
            string path = "/barcode2/query";
            string method = "POST";
            string appcode = "xxxxxxxxxxxxxxxxxxxxxxxx";

            String querys = "barcode=0"+barcode;
            String bodys = "null";
            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);
            //根据API的要求,定义相对应的Content-Type
            httpRequest.ContentType = "application/json; charset=UTF-8";
            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)
            {
                httpResponse = (HttpWebResponse)ex.Response;
            }

            Stream st = httpResponse.GetResponseStream();
            StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
            string response = reader.ReadToEnd();

            if (!string.IsNullOrEmpty(response))
            {
                JObject jsonData = JObject.Parse(response);
                JObject items = jsonData["result"] as JObject;

                DataRow row = dataTable.NewRow();

                row["MATNAME"] = items["name"].ToString();//名称
                row["BOSPECNO"] = items["type"].ToString();//规格(重量/ml)
                row["SPECNAME"] = items["type"].ToString();//净含量(重量/ml)
                row["MATGP"] = items["unspsc"].ToString();//类型
                                                         //row["VAT"] = item["saledate"].ToString(); ;//上市日期
                row["BRAND"] = items["keyword"].ToString();//品牌
                row["SKU"] = items["barcode"].ToString();//商品条码
                dataTable.Rows.Add(row);
            }

            return dataTable;
        }

        public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }
    }
}

 

posted on   孤幽影暗  阅读(67)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示