模拟网页请求

网络请求

public string GetHsnfResult( string cookie)
        {
            var authorization = cookie;
            HttpHelper helper = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = "https://接口地址“,
                Encoding = null,
                Accept =  "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*",
                Method = "Get",
                ContentType= "application/json",
                UserAgent= "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T)  AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Mobile Safari/537.36"
            };
            item.Header.Add("Cookie", authorization);
            item.Header.Add("Accept-Encoding", "gzip, deflate, br");
            var httpResult = helper.GetHtml(item);
            var resultStr = httpResult.Html;
            return resultStr;
        }

请求返回处理

1.返回的数据是Html

public Item UpdateHsnfLog(string Sn, string cookie)
        {
            try
            {
                var Str= GetHsnfResult(Sn, cookie);
                HtmlAgilityPack.HtmlDocument document = new  HtmlAgilityPack.HtmlDocument();
                document.LoadHtml(Str);
                var infoList =  document.DocumentNode.SelectNodes("//div[@class='box-tit']/*[self::span]");//读取样式class='box-tit' 下的span 标签
                var boxList =  document.DocumentNode.SelectNodes("//div[@class='box-list']/*[self::p]");
                var ProductRange = boxList[0].InnerText.Replace("产品规格:",  "");
               
            }
            catch (Exception ex)
            {
            }
            return null;
        }

2.返回Json字符串

public Item UpdatePsnLog(string Sn, string cookie)
        {
            try
            {
               var Str= GetHsnfResult(Sn, cookie);
                var responseData =  JsonConvert.DeserializeObject<BCClientResponseData>(Str);//BCClientResponseData 是对象:将Json转换成对象
                if (responseData.code == 0&& responseData.data != null)
                {
                    var ProductName = responseData.data.bomInfo.pF_NAME;
                }               
            }
            catch(Exception ex)
            {
            }
            return null;
        }

 

posted @ 2024-03-01 14:10  绽放的菖蒲  阅读(19)  评论(0编辑  收藏  举报