• 抓取登录后的数据


        这次是应一个客户需要,抓取另外一个网站的数据,包括数据提交。这些操作需要在登录之后完成。技术上没有什么难点。关键都是用fiddler找到参数和url。

    记住登录状态

        HttpClient能够记住登录状态的,登录完了可以讲Httpclient保存起来。

     private HttpClient _client;
            public HttpClient HttpClient
            {
                get
                {
                    if (_client == null)
                    {
                        if (Session["Client"]!= null)
                        {
                            _client = Session["Client"] as HttpClient;
                        }
                        else
                        {
                            var handler = new HttpClientHandler
                            {
                                AutomaticDecompression = DecompressionMethods.GZip,
                                UseCookies = true,
                                Proxy =
                                    new WebProxy("http://ip:8080/", true, null,
                                        new NetworkCredential("username", "pwd", "domain"))
                            };//代理 
                            _client = new HttpClient(handler);
                            _client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");
                            ClientLogin(new ClientLogoModel());
                            Session["Client"] = _client;
                        }
                      
                    }
    
                    return _client;
                }
            }

     因为目标网站都是用的json传的参数。也是用json返回的参数。不是form提交的格式。所以post之前也要将参数转成json。

     public object ClientLogin(ClientLogoModel logoModel)
            {
                if (logoModel == null)
                {
                    logoModel=new ClientLogoModel();
    
                }
                var data = JsonConvert.SerializeObject(logoModel); ;
                var logoParams = new List<KeyValuePair<string, string>>();
                logoParams.Add(new KeyValuePair<string, string>("data", data));
                var response = _client.PostAsync(new Uri(LogonUrl), new FormUrlEncodedContent(logoParams)).Result;
                var result = response.Content.ReadAsStringAsync().Result;
                return result;
            }

    返回数据转化

    从Fiddler左边获得Url,右边TextView上方是参数格式,下方是返回的数据格式。 

    每次都要转换,写成泛型。

        public T GetTList<T>(object obj, string url)
            {
                var data = JsonConvert.SerializeObject(obj); ;
                var paramList = new List<KeyValuePair<String, String>> { new KeyValuePair<string, string>("data", data) };
                var response = HttpClient.PostAsync(new Uri(url), new FormUrlEncodedContent(paramList)).Result;
    
                var result = response.Content.ReadAsStringAsync().Result;
                return JsonConvert.DeserializeObject<T>(result);
            }

    调用:

        public ActionResult TradePage(TradeQueryParm param)
            {
           var data = GetTList<TradeRequstResult>(obj, tradeListUrl);
                return PartialView(data);
            }

     前端再将参数传递过来。

      $.post("/Trade/TradePage", {
                        agentName: agentName, shortName: shortName,
                        startDate: startDate, endDate: endDate, page: cpage
                    }, function (data) {
                        $("#mtable").html(data);
      }
    HttpClient 上传图片:
       private string UploadImage(string fileName,string path)
            {
                FileStream aFile = new FileStream(path, FileMode.Open);
                MultipartFormDataContent form = new MultipartFormDataContent();
                var  content = new StreamContent(aFile);
                content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
                content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                {
                    Name = "protocolFile",
                    FileName = fileName
                };
                form.Add(content);
                var response = HttpClient.PostAsync(imgLoadUrl, form).Result;
                return response.Content.ReadAsStringAsync().Result;
            }

  • 相关阅读:
    回答提出的问题1-17章
    《构建之法》第13-17章读书笔记
    读《一个程序员的生命周期》有感
    构建之法的第十、十一、十二章读书笔记
    阅读《构建之法》第8,9,10章
    5.2-5.3
    作业5.1测试与封装
    读《构建之法》5.6.7 思考
    读《构建之法》的思考
    作业2 结对思则运算
  • 原文地址:https://www.cnblogs.com/stoneniqiu/p/5005890.html
Copyright © 2020-2023  润新知