This is the file download class:
Copy Code code as follows:
Using System;
Using System.IO;
Using System.Net;
Using System.Web;
public class Srwebclient
{
Cookiecontainer cookies;
Public Srwebclient ()
{
cookie = new Cookiecontainer ();
}
<TgData>
<Alias> download Web source code </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 ("Download Web Error", er.) ToString ());
}
Finally
{
Res. Close ();
S1. Close ();
}
return R;
}
<TgData>
<Alias> Download File </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 ("Download file Error", er. ToString ());
}
Finally
{
Res. Close ();
Stream. Close ();
}
return filelength;
}
<TgData>
<Alias> Submit Data </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)///include the data to be submitted using the Post method
{
As a form request
Request. ContentType = "application/x-www-form-urlencoded";
The way is post.
Request. method = "POST";
Convert the submitted data to a byte array
byte[] B = System.Text.Encoding.Default.GetBytes (postdata);
Request. ContentLength = B.length;
Stream SW = Request. GetRequestStream (); Start submitting data
SW. Write (B, 0, b.length);
SW. Close ();
}
Response = Request. GetResponse () as HttpWebResponse;
Response. Close ();
}
}
This is the submitted data class:
Copy Code code as follows:
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;
}
}
Code posted, the following is the test code, because some data related to customer information, so the hidden
Copy Code code as follows:
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");
}
}