如何通过HTTP下载HTML文件

guo_dongliang 2003-08-29 03:33:31
如何通过HTTP下载HTML文件
...全文
329 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
guo_dongliang 2003-08-29
  • 打赏
  • 举报
回复
thank you,结帖
amagic 2003-08-29
  • 打赏
  • 举报
回复
string strHtml = null;
Encoding strEncode = System.Text.Encoding.GetEncoding("UTF-8");
string strAction = "http://...";
string strData = "userID=" + strUserID + "&pwd=" + strPwd;

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strAction);
req.UserAgent = "MSIE6.0";
req.Method = "POST";
byte[] PostData = System.Text.Encoding.ASCII.GetBytes(strData);
req.ContentLength = PostData.Length;
Stream tempStream = req.GetRequestStream();
tempStream.Write(PostData, 0, PostData.Length);
tempStream.Close();

HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), strEncode);
strHtml = sr.ReadToEnd();
sr.Close();
res.Close();
guo_dongliang 2003-08-29
  • 打赏
  • 举报
回复
也就是需要一个类似Connect(userID,pwd)的函数去连接,
如何做???
guo_dongliang 2003-08-29
  • 打赏
  • 举报
回复
如果需要用户名和密码,怎么办?
panyee 2003-08-29
  • 打赏
  • 举报
回复
//string strUrl = "http://www.sina.com.cn";
panyee 2003-08-29
  • 打赏
  • 举报
回复
using System.Net;

string strUrl = "http://gather.sohu.com/life/forecast/query.asp?cityname=";
strUrl = strUrl + System.Web.HttpUtility.UrlEncode("北京", System.Text.Encoding.GetEncoding("gb2312"));
string strUrl = "http://www.sina.com.cn";
HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(strUrl);

HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();
StreamReader sr = new StreamReader(oResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));

string sResultContents = sr.ReadToEnd();
oResponse.Close();
byte[] bytes = System.Text.Encoding.GetEncoding("gb2312").GetBytes(sResultContents);
FileStream fs = new FileStream("c:\\2.htm", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
fs.Close();

110,526

社区成员

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

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

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