asp.net 需要登陆的网站上下载网页源代码和文件
程序员文章站
2024-03-09 12:39:17
这个是文件下载类:复制代码 代码如下:using system; using system.io; using system.net; using system.web;...
这个是文件下载类:
using system;
using system.io;
using system.net;
using system.web;
public class srwebclient
{
cookiecontainer cookie;
public srwebclient()
{
cookie = new cookiecontainer();
}
/// <tgdata>
/// <alias>下载web源代码</alias>
/// </tgdata>
public string downloadhtml(string url)
{
httpwebrequest request = httpwebrequest.create(url) as httpwebrequest;
request.cookiecontainer = cookie;
request.allowautoredirect = false;
webresponse res = request.getresponse();
string r = "";
streamreader s1 = new streamreader(res.getresponsestream(), system.text.encoding.default);
try
{
r = s1.readtoend();
}
catch (exception er)
{
log l = new log();
l.writelog("下载web错误", er.tostring());
}
finally
{
res.close();
s1.close();
}
return r;
}
/// <tgdata>
/// <alias>下载文件</alias>
/// </tgdata>
public long downloadfile(string fileurl, string filesavepath)
{
long filelength = 0;
httpwebrequest req = httpwebrequest.create(fileurl) as httpwebrequest;
req.cookiecontainer = cookie;
req.allowautoredirect = true;
httpwebresponse res = req.getresponse() as httpwebresponse;
system.io.stream stream = res.getresponsestream();
try
{
filelength = res.contentlength;
byte[] b = new byte[512];
int nreadsize = 0;
nreadsize = stream.read(b, 0, 512);
system.io.filestream fs = system.io.file.create(filesavepath);
try
{
while (nreadsize > 0)
{
fs.write(b, 0, nreadsize);
nreadsize = stream.read(b, 0, 512);
}
}
finally
{
fs.close();
}
}
catch (exception er)
{
log l = new log();
l.writelog("下载文件错误", er.tostring());
}
finally
{
res.close();
stream.close();
}
return filelength;
}
/// <tgdata>
/// <alias>提交数据</alias>
/// </tgdata>
public void request(string requestpageurl, requestdata data)
{
string strurl = requestpageurl;
httpwebrequest request = httpwebrequest.create(strurl) as httpwebrequest;
httpwebresponse response;
string postdata = data.getdata();
request.referer = requestpageurl;
request.allowautoredirect = false;
request.useragent = "mozilla/4.0 (compatible; msie 6.01; windows nt 5.0)";
request.cookiecontainer = cookie;
uri u = new uri(strurl);
if (postdata.length > 0) //包含要提交的数据 就使用post方式
{
//作为表单请求
request.contenttype = "application/x-www-form-urlencoded";
//方式就是post
request.method = "post";
//把提交的数据换成字节数组
byte[] b = system.text.encoding.default.getbytes(postdata);
request.contentlength = b.length;
stream sw = request.getrequeststream(); //开始提交数据
sw.write(b, 0, b.length);
sw.close();
}
response = request.getresponse() as httpwebresponse;
response.close();
}
}
这个是提交的数据类:
using system.collections;
using system.io;
public class requestdata
{
arraylist arr = new arraylist();
public requestdata()
{
}
public string getdata()
{
string r = "";
for (int i = 0; i < arr.count; i++)
{
data d = (data) arr[i];
if (r.length > 0)
r += "&";
r += d.field + "=" + d.value;
}
return r;
}
public void addfield(string field, string value)
{
data a = new data();
a.field = field;
a.value = value;
arr.add(a);
}
struct data
{
public string field, value;
}
}
代码贴完了,下面是测试代码,因为有些数据涉及到客户的资料,故隐去
using nunit.framework;
[testfixture]
public class testweb
{
[test]
public void testdownseorder()
{
requestdata data = new requestdata();
data.addfield("name", "abc");
data.addfield("password", "121");
srwebclient web = new srwebclient();
web.request("http://127.0.0.1/login.asp", data);
string s = web.downloadhtml("http://127.0.0.1/dingdan.asp");
system.console.writeline(s);
}
[test]
public void testdownfile()
{
requestdata data = new requestdata();
data.addfield("name", "aaa");
data.addfield("password", "bbb");
srwebclient web = new srwebclient();
web.request("http://127.0.0.1/login.asp", data);
web.downloadfile("http://127.0.0.1/download.asp?fileid=1", @"c:\a.txt");
}
}
复制代码 代码如下:
using system;
using system.io;
using system.net;
using system.web;
public class srwebclient
{
cookiecontainer cookie;
public srwebclient()
{
cookie = new cookiecontainer();
}
/// <tgdata>
/// <alias>下载web源代码</alias>
/// </tgdata>
public string downloadhtml(string url)
{
httpwebrequest request = httpwebrequest.create(url) as httpwebrequest;
request.cookiecontainer = cookie;
request.allowautoredirect = false;
webresponse res = request.getresponse();
string r = "";
streamreader s1 = new streamreader(res.getresponsestream(), system.text.encoding.default);
try
{
r = s1.readtoend();
}
catch (exception er)
{
log l = new log();
l.writelog("下载web错误", er.tostring());
}
finally
{
res.close();
s1.close();
}
return r;
}
/// <tgdata>
/// <alias>下载文件</alias>
/// </tgdata>
public long downloadfile(string fileurl, string filesavepath)
{
long filelength = 0;
httpwebrequest req = httpwebrequest.create(fileurl) as httpwebrequest;
req.cookiecontainer = cookie;
req.allowautoredirect = true;
httpwebresponse res = req.getresponse() as httpwebresponse;
system.io.stream stream = res.getresponsestream();
try
{
filelength = res.contentlength;
byte[] b = new byte[512];
int nreadsize = 0;
nreadsize = stream.read(b, 0, 512);
system.io.filestream fs = system.io.file.create(filesavepath);
try
{
while (nreadsize > 0)
{
fs.write(b, 0, nreadsize);
nreadsize = stream.read(b, 0, 512);
}
}
finally
{
fs.close();
}
}
catch (exception er)
{
log l = new log();
l.writelog("下载文件错误", er.tostring());
}
finally
{
res.close();
stream.close();
}
return filelength;
}
/// <tgdata>
/// <alias>提交数据</alias>
/// </tgdata>
public void request(string requestpageurl, requestdata data)
{
string strurl = requestpageurl;
httpwebrequest request = httpwebrequest.create(strurl) as httpwebrequest;
httpwebresponse response;
string postdata = data.getdata();
request.referer = requestpageurl;
request.allowautoredirect = false;
request.useragent = "mozilla/4.0 (compatible; msie 6.01; windows nt 5.0)";
request.cookiecontainer = cookie;
uri u = new uri(strurl);
if (postdata.length > 0) //包含要提交的数据 就使用post方式
{
//作为表单请求
request.contenttype = "application/x-www-form-urlencoded";
//方式就是post
request.method = "post";
//把提交的数据换成字节数组
byte[] b = system.text.encoding.default.getbytes(postdata);
request.contentlength = b.length;
stream sw = request.getrequeststream(); //开始提交数据
sw.write(b, 0, b.length);
sw.close();
}
response = request.getresponse() as httpwebresponse;
response.close();
}
}
这个是提交的数据类:
复制代码 代码如下:
using system.collections;
using system.io;
public class requestdata
{
arraylist arr = new arraylist();
public requestdata()
{
}
public string getdata()
{
string r = "";
for (int i = 0; i < arr.count; i++)
{
data d = (data) arr[i];
if (r.length > 0)
r += "&";
r += d.field + "=" + d.value;
}
return r;
}
public void addfield(string field, string value)
{
data a = new data();
a.field = field;
a.value = value;
arr.add(a);
}
struct data
{
public string field, value;
}
}
代码贴完了,下面是测试代码,因为有些数据涉及到客户的资料,故隐去
复制代码 代码如下:
using nunit.framework;
[testfixture]
public class testweb
{
[test]
public void testdownseorder()
{
requestdata data = new requestdata();
data.addfield("name", "abc");
data.addfield("password", "121");
srwebclient web = new srwebclient();
web.request("http://127.0.0.1/login.asp", data);
string s = web.downloadhtml("http://127.0.0.1/dingdan.asp");
system.console.writeline(s);
}
[test]
public void testdownfile()
{
requestdata data = new requestdata();
data.addfield("name", "aaa");
data.addfield("password", "bbb");
srwebclient web = new srwebclient();
web.request("http://127.0.0.1/login.asp", data);
web.downloadfile("http://127.0.0.1/download.asp?fileid=1", @"c:\a.txt");
}
}
上一篇: asp.net 分页潜谈
下一篇: 浅析java中遍历map的两种方式