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;
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?