当前位置: 首页 > 工具软件 > RestSharp > 使用案例 >

RestSharp用法(一)

蔺宇
2023-12-01

     需求:用RestSharp访问Http代理接口

  遇到的问题:1.网站用了简单的反爬虫(看你的请求有没有‘UserAgent’)

       2.返回的包含html

  解决方法1.在client中加上UserAgent(有多种)

     2.切割html(不知道还有什么方法).

 

public HttpAgencyModule() : base("/HttpAgency/")
        {
            Get("GetAgency", x =>
            {
                HttpInput HttpInput = this.Bind<HttpInput>();
                List<string> list = new List<string>();
                switch (HttpInput.Platform)
                {
                    case Platform.Free:list = FreeHttpAgency(HttpInput);
                        break;
                    default:
                        break;
                }
                return list;
            });
        }        

 

 

public List<string> FreeHttpAgency(HttpInput Input)
        {
            RestClient client = new RestClient
            {
                BaseUrl = new Uri("http://www.89ip.cn/tqdl.html?api=1"),
                UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
            };
            var request = new RestRequest();
            request.AddObject(Input);
            IRestResponse respose = client.Execute(request);
            var content = respose.Content.ToString();
            string[] htmlArray = content.Split('\n');
            string html = htmlArray[12].Replace("<br", "");
            List<string> list = new List<string>();
            Array.Clear(htmlArray, 0, htmlArray.Length);
            htmlArray = html.Split('>');
            foreach (var item in htmlArray)
            {
                list.Add(item);
            }
            list.RemoveAt(list.Count - 1);
            return list;
        }                

 

转载于:https://www.cnblogs.com/weiruanojbk/p/11393046.html

 类似资料: