×

Asp.Net使用代理IP远程获取数据

hqy hqy 发表于2021-06-25 09:53:16 浏览1334 评论0

抢沙发发表评论

/// <summary>
    /// 远程获取数据
    /// </summary>
    /// <param name="url">url</param>
    /// <param name="code">编码</param>
    /// <param name="ProxyStr">代理IP,格式:10.20.30.40:8888</param>
    /// <returns></returns>
    public static string SendUrl(string url, Encoding code, string ProxyStr)
    {
        string html = string.Empty;
        try
        {
            HttpWebRequest WebReques = (HttpWebRequest)HttpWebRequest.Create(url);
            WebReques.Method = "GET";
            WebReques.Timeout = 20000;
            if (ProxyStr.Length > 0)
            {
                WebProxy proxy = new WebProxy(ProxyStr, true);
                WebReques.Proxy = proxy;
            }

            HttpWebResponse WebRespon = (HttpWebResponse)WebReques.GetResponse();
            if (WebRespon != null)
            {
                StreamReader sr = new StreamReader(WebRespon.GetResponseStream(), code);
                html = sr.ReadToEnd();
                sr.Close();
                sr.Dispose();
                WebRespon.Close();
            }
        }
        catch
        {
    html = “err”;
        }
        return html;
    }


 您阅读本篇文章共花了: 

打赏

本文链接:https://kinber.cn/post/1769.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

群贤毕至

访客