首页
知识
文库
登录
|
注册
首页
最近更新
所有主题
我的主题
我的收藏
首页
·
知识
·
编程语言
从Internet上抓取指定URL的源码的方案
拍拍尘土
http://blog.tao123.com/
.NET
编辑:
dezai
图片来源:网络
作者:3cts | 来源:doc.4kiki.net引言: 在做无线项目的时候,与通讯公司的数据通讯有一部分是通过XML交互的,所以必须要动态抓取通讯公司
作者:3cts | 来源:doc.4kiki.net
引言:
在做无线项目的时候,与通讯公司的数据通讯有一部分是通过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
{
///
/// 功能:取得Internet上的URL页的源码
/// 创建:2004-03-22
/// 作者:Rexsp MSN:yubo@x263.net
///
public class GetPageCode
{
#region 私有变量
///
/// 网页URL地址
///
private string url=null;
///
/// 是否使用代码服务器:0 不使用 1 使用代理服务器
///
private int proxyState=0;
///
/// 代理服务器地址
///
private string proxyAddress=null;
///
/// 代理服务器端口
///
private string proxyPort=null;
///
/// 代理服务器用户名
///
private string proxyAccount=null;
///
/// 代理服务器密码
///
private string proxyPassword=null;
///
/// 代理服务器域
///
private string proxyDomain=null;
///
/// 输出文件路径
///
private string outFilePath=null;
///
/// 输出的字符串
///
private string outString=null;
///
/// 提示信息
///
private string noteMessage;
#endregion
#region 公共属性
///
/// 欲读取的URL地址
///
public string Url
{
get{return url;}
set{url=value;}
}
///
/// 是否使用代理服务器标志
///
public int ProxyState
{
get{return proxyState;}
set{proxyState=value;}
}
///
/// 代理服务器地址
///
public string ProxyAddress
{
get{return proxyAddress;}
set{proxyAddress=value;}
}
///
/// 代理服务器端口
///
public string ProxyPort
{
get{return proxyPort;}
set{proxyPort=value;}
}
///
/// 代理服务器账号
///
public string ProxyAccount
{
get{return proxyAccount;}
set{proxyAccount=value;}
}
///
/// 代理服务器密码
///
public string ProxyPassword
{
get{return proxyPassword;}
set{proxyPassword=value;}
}
///
/// 代理服务器域
///
public string ProxyDomain
{
get{return proxyDomain;}
set{proxyDomain=value;}
}
///
/// 输出文件路径
///
public string OutFilePath
{
get{return outFilePath;}
set{outFilePath=value;}
}
///
/// 返回的字符串
///
public string OutString
{
get{return outString;}
}
///
/// 返回提示信息
///
public string NoteMessage
{
get{return noteMessage;}
}
#endregion
#region 构造函数
public GetPageCode()
{
}
#endregion
#region 公共方法
///
/// 读取指定URL地址,存到指定文件中
///
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
}
}
本文作者:拍拍尘土 来源:http://blog.tao123.com/
CIO之家 www.ciozj.com 微信公众号:imciow
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读
浅谈系统性能提升的经验和方法
SpringBoot 实现异步记录复杂日志
Linux 实时查看日志文件的 4 种方法
也许感兴趣的
.
AGIC和软件生命周期:远不止是编码辅助
.
营销增长:如何从0到1设计并搭建你的会员体系?
.
制造企业MES功能架构及实施规划流程
.
如何做好私域运营
.
数据脱敏技术
.
数据中台建设的目标、流程及四大要点
.
数智时代,业财融合分析平台如何重新定义商业智能
我们推荐的
.
Spring Boot 构建多租户SaaS平台核心技术指南
.
关于Java异常设计和处理
.
Nginx学习看这一篇就够了
.
社会化海量数据采集爬虫框架搭建
.
ASP.NET多文件批量打包下载
.
Razor基础语法简介
.
ASP.NET中在不同的子域中共享Session
.
C#实现工作日的计算(排班系统常用)
主题最新
.
一文搞懂微服务架构演进
.
一文详解微服务架构
.
迄今为止最完整的DDD实践
.
全链路压测自动化实践
.
多维度规划业务架构
.
企业架构之业务架构
.
需求管理完整指南
.
软件安全设计原则
.
应用部署初探:微服务的3大部署模式
看看其它的
收藏至微信