首页 商品条码查询 商品条码查询示例代码 条码查询[C#]

条码查询示例代码C#

作者: 阅读数:1174 上传时间:2025-04-01

条码查询

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

//await Barcode2.QueryAsync("6953502812005");
class Barcode2
{
    // 替换成你的AppKey
    private const string AppKey = "YOUR_APPKEY_HERE";

    public static async Task QueryAsync(string barcode)
    {
        try
        {
            using (HttpClient client = new HttpClient())
            {
                // 构造请求URL
                string url = $"https://api.jisuapi.com/barcode2/query?appkey={AppKey}&barcode={barcode}";

                // 发送GET请求
                HttpResponseMessage response = await client.GetAsync(url);
                
                // 确保成功响应
                response.EnsureSuccessStatusCode();

                // 读取响应内容
                string responseBody = await response.Content.ReadAsStringAsync();
                JObject result = JObject.Parse(responseBody);

                // 处理API返回状态
                if ((int)result["status"] == 0)
                {
                    // 解析信息
					JToken data = result["result"];	
                    Console.WriteLine(data["barcode"]+" "+data["name"]+" "+data["ename"]+" "+data["unspsc"]);
					Console.WriteLine(data["brand"]+" "+data["specification"]+" "+data["width"]+" "+data["height"]);
					Console.WriteLine(data["depth"]+" "+data["origincountry"]+" "+data["originplace"]+" "+data["assemblycountry"]);
					Console.WriteLine(data["barcodetype"]+" "+data["catena"]+" "+data["isbasicunit"]+" "+data["packagetype"]);
					Console.WriteLine(data["grossweight"]+" "+data["netcontent"]+" "+data["netweight"]+" "+data["description"]);
					Console.WriteLine(data["keyword"]+" "+data["pic"]+" "+data["price"]+" "+data["licensenum"]+" "+data["healthpermitnum"]);
                }
                else
                {
                    Console.WriteLine($"查询失败:{result["status"]} {result["msg"]}");
                }
            }
        }
        catch (HttpRequestException ex)
        {
            Console.WriteLine($"请求异常:{ex.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生错误:{ex.Message}");
        }
    }
	
	static async Task Main(string[] args)
    {
        //await QueryAsync("6953502812005");
    }
}