public virtual string Html(string url, string ip, string port)
{
string result = "";
try
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
System.Net.WebProxy proxy = new WebProxy(ip, Convert.ToInt32(port));
request.Proxy = proxy;
using (WebResponse response = request.GetResponse())
{
using (TextReader reader = new StreamReader(response.GetResponseStream()))
{
string line;
while ((line = reader.ReadLine()) != null)
{
result = result + line;
}
}
}
}
catch (Exception)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
System.Net.WebProxy proxy = new WebProxy(GetlocalIP(), 80);
request.Proxy = proxy;
using (WebResponse response = request.GetResponse())
{
using (TextReader reader = new StreamReader(response.GetResponseStream()))
{
string line;
while ((line = reader.ReadLine()) != null)
{
result = result + line;
}
}
}
}
}
catch
{
result = "";
}
return result;
}
{
string result = "";
try
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
System.Net.WebProxy proxy = new WebProxy(ip, Convert.ToInt32(port));
request.Proxy = proxy;
using (WebResponse response = request.GetResponse())
{
using (TextReader reader = new StreamReader(response.GetResponseStream()))
{
string line;
while ((line = reader.ReadLine()) != null)
{
result = result + line;
}
}
}
}
catch (Exception)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
System.Net.WebProxy proxy = new WebProxy(GetlocalIP(), 80);
request.Proxy = proxy;
using (WebResponse response = request.GetResponse())
{
using (TextReader reader = new StreamReader(response.GetResponseStream()))
{
string line;
while ((line = reader.ReadLine()) != null)
{
result = result + line;
}
}
}
}
}
catch
{
result = "";
}
return result;
}
本文介绍了一种通过C#编程语言使用指定或本地代理来获取网页HTML内容的方法。该方法首先尝试使用提供的代理服务器及端口进行网页内容抓取,若失败,则回退到使用本地IP和默认端口再次尝试。
4454

被折叠的 条评论
为什么被折叠?



