如何取得指定网址的源文件?

wsssir 2004-07-07 05:13:40
我想做一个程序,在输入框输入一个网址,在下面的文本框里显示这个网址的源文件.
...全文
109 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
捏造的信仰 2004-07-08
  • 打赏
  • 举报
回复
通过URL建立一个WebRequest,
然后通过WebRequest.GetResponse方法得到其WebResponse,
然后通过WebResponse.GetResponseStream来得到一个流,
最后是将这个流显示出来还是写到文件里面去都随你啦。
wsssir 2004-07-08
  • 打赏
  • 举报
回复
获得固定URL的源文件我也知道的,但有的网站是要求先登录才能查看的,如果不能在自动登录进去之后获得网页的源文件就没有意义了。关键在于你获得的网页是你自己自动登录进去的。必须是同一个SessionID才行。
hesicong 2004-07-07
  • 打赏
  • 举报
回复
我的主页的“编程心得”上有关于这个问题源代码,很简洁的:)欢迎来看看!

———————————————
欢迎浏览我的主页
http://DreamWorld2004.yeah.net
webmasterss 2004-07-07
  • 打赏
  • 举报
回复
可以用自动登陆程序配合以下代码结合使用,自动登陆在本网站文档中有介绍,下面是取得源码程序
在做无线项目的时候,与通讯公司的数据通讯有一部分是通过XML交互的,所以必须要动态抓取通讯公司提供的固定的Internet上的数据,便研究了一下如何抓取固定url上的数据,现与大家分享一下。
  类名GetPageCode,有一个方法GetSource,通过属性传递参数,入参控制的是要取得URL的地址,代理服务器的设置及输出方式的控制,这里大家可以再扩展自己的需要,我这里只提供了两种方式,一种是直接写到本地的某个文件中,另外一种就是返回字符串的。类里已经作了比较详细的注释,我想大家很容易就看明白了,如果实在不明白,那就msn上问吧,MSN:yubo@x263.net。

调用方式:
#region 测试获取远程网页
GetPageCode gpc = new GetPageCode();
gpc.Url="http://ppcode.com";
gpc.ProxyState=1;//使用代理服务器,0为不使用,设置为1后下面的代理设置才起作用
gpc.ProxyAddress="http://proxyName.com";//代理服务器地址
gpc.ProxyPort="80";//代理服务器的端口
gpc.ProxyAccount="proxy";//代理服务器账号
gpc.ProxyPassword="password";//代理服务器密码
gpc.ProxyDomain="bqc";//代理服务器域
gpc.OutFilePath=filePath;//设置输出文件路径的地方,如果不设置,则返回字符串
gpc.GetSource();//处理
string tempErr=gpc.NoteMessage;//如果出错,这里会提示
string tempCode=gpc.OutString;//返回的字符串
#endregion
类代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Web;


namespace Test.Com
{
/// <summary>
/// 功能:取得Internet上的URL页的源码
/// 创建:2004-03-22
/// 作者:Rexsp MSN:yubo@x263.net
/// </summary>
public class GetPageCode
{
#region 私有变量
/// <summary>
/// 网页URL地址
/// </summary>
private string url=null;
/// <summary>
/// 是否使用代码服务器:0 不使用 1 使用代理服务器
/// </summary>
private int proxyState=0;
/// <summary>
/// 代理服务器地址
/// </summary>
private string proxyAddress=null;
/// <summary>
/// 代理服务器端口
/// </summary>
private string proxyPort=null;
/// <summary>
/// 代理服务器用户名
/// </summary>
private string proxyAccount=null;
/// <summary>
/// 代理服务器密码
/// </summary>
private string proxyPassword=null;
/// <summary>
/// 代理服务器域
/// </summary>
private string proxyDomain=null;
/// <summary>
/// 输出文件路径
/// </summary>
private string outFilePath=null;
/// <summary>
/// 输出的字符串
/// </summary>
private string outString=null;
/// <summary>
/// 提示信息
/// </summary>
private string noteMessage;

#endregion

#region 公共属性
/// <summary>
/// 欲读取的URL地址
/// </summary>
public string Url
{
get{return url;}
set{url=value;}
}
/// <summary>
/// 是否使用代理服务器标志
/// </summary>
public int ProxyState
{
get{return proxyState;}
set{proxyState=value;}
}
/// <summary>
/// 代理服务器地址
/// </summary>
public string ProxyAddress
{
get{return proxyAddress;}
set{proxyAddress=value;}
}
/// <summary>
/// 代理服务器端口
/// </summary>
public string ProxyPort
{
get{return proxyPort;}
set{proxyPort=value;}
}
/// <summary>
/// 代理服务器账号
/// </summary>
public string ProxyAccount
{
get{return proxyAccount;}
set{proxyAccount=value;}
}
/// <summary>
/// 代理服务器密码
/// </summary>
public string ProxyPassword
{
get{return proxyPassword;}
set{proxyPassword=value;}
}
/// <summary>
/// 代理服务器域
/// </summary>
public string ProxyDomain
{
get{return proxyDomain;}
set{proxyDomain=value;}
}
/// <summary>
/// 输出文件路径
/// </summary>
public string OutFilePath
{
get{return outFilePath;}
set{outFilePath=value;}
}
/// <summary>
/// 返回的字符串
/// </summary>
public string OutString
{
get{return outString;}

}
/// <summary>
/// 返回提示信息
/// </summary>
public string NoteMessage
{
get{return noteMessage;}

}

#endregion

#region 构造函数
public GetPageCode()
{
}
#endregion

#region 公共方法
/// <summary>
/// 读取指定URL地址,存到指定文件中
/// </summary>
public void GetSource()
{
WebRequest request = WebRequest.Create(this.url);
//使用代理服务器的处理
if(this.proxyState==1)
{
//默认读取80端口的数据
if(this.proxyPort==null)
this.ProxyPort="80";

WebProxy myProxy=new WebProxy();
myProxy = (WebProxy)request.Proxy;
myProxy.Address = new Uri(this.ProxyAddress+":"+this.ProxyPort);
myProxy.Credentials = new NetworkCredential(this.proxyAccount, this.proxyPassword, this.ProxyDomain);
request.Proxy = myProxy;
}
try

{
//请求服务
WebResponse response = request.GetResponse();
//返回信息
Stream resStream = response.GetResponseStream();
StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
string tempCode= sr.ReadToEnd();
resStream.Close();
sr.Close();

//如果输出文件路径为空,便将得到的内容赋给OutString属性
if(this.outFilePath==null)
{
this.outString=tempCode;
}
else
{

FileInfo fi = new FileInfo(this.outFilePath);
//如果存在文件则先干掉
if(fi.Exists)
fi.Delete();

StreamWriter sw = new StreamWriter(this.outFilePath,true,Encoding.Default);
sw.Write(tempCode);
sw.Flush();
sw.Close();
}
}
catch
{
this.noteMessage="出错了,请检查网络是否连通;";
}


}
#endregion

}
}
wsssir 2004-07-07
  • 打赏
  • 举报
回复
这个网页是要先登录的.
jiezhi 2004-07-07
  • 打赏
  • 举报
回复
http://dotnet.aspx.cc/ShowDetail.aspx?id=0A6660CE-4138-41EF-B882-15DB65564709

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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