谁给个HttpWebRequest或者webclient获取https网页内容的代码!

liying520 2010-06-12 03:37:50
如题。。求一个用上面类获取https网页内容的代码。只是确定功能无需导入证书等功能
...全文
198 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2010-06-12
  • 打赏
  • 举报
回复
System.Net.WebClient wc = new System.Net.WebClient();
wc.Credentials = System.Net.CredentialCache.DefaultCredentials;
Byte[] pageData = wc.DownloadData("");
string content= System.Text.Encoding.Default.GetString(pageData);

System.Net.WebRequest request = System.Net.WebRequest.Create("");
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream resStream = response.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(resStream, System.Text.Encoding.Default);
string content= sr.ReadToEnd();
resStream.Close();
sr.Close();
捷哥1999 2010-06-12
  • 打赏
  • 举报
回复
这个方法,你可以直接调用!

/// <summary>
/// 获得页面的html代码
/// </summary>
/// <param name="url">页面地址</param>
protected string getHtml(string url)
{
string html = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "*/*";
HttpWebResponse response = null;
Stream stream = null;
StreamReader reader = null;
try
{
response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
reader = new StreamReader(stream, Encoding.UTF8);
html = reader.ReadToEnd();
}
catch (Exception excpt)
{
}
finally
{
if (reader != null)
{
reader.Close();
reader.Dispose();
}
if (stream != null)
{
stream.Close();
stream.Dispose();
}
if (response != null)
{
response.Close();
}
}
return html;
}
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 liying520 的回复:]

楼上别直接抄袭CSDN上的啊。给个具体确实能用的例子。。包含调用的类
[/Quote]

这例子还不够详细么?哪些类不明明白白写着


[Quote=引用 3 楼 baimerde 的回复:]

补充一个问题,如果要先登录系统,如何抓取登陆后的网页信息
期待例子……
[/Quote]

要先登录的就用WebRequest模拟post方法,用myHttpWebRequest.CookieContainer.GetCookieHeader(myHttpWebRequest.RequestUri)得到对方set的cookie,然后带着这个cookie去访问需要登录才能访问的页面
baimerde 2010-06-12
  • 打赏
  • 举报
回复
补充一个问题,如果要先登录系统,如何抓取登陆后的网页信息
期待例子……
liying520 2010-06-12
  • 打赏
  • 举报
回复
楼上别直接抄袭CSDN上的啊。给个具体确实能用的例子。。包含调用的类
  • 打赏
  • 举报
回复

public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
//直接确认,否则打不开
return true;
}

private void button1_Click(object sender, EventArgs e)
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest req = (HttpWebRequest)WebRequest.CreateDefault(new Uri("https://zu14.cn/"));
req.Method = "GET";
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
//正常使用了,和访问普通的 http:// 地址一样了
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧