After receiving a task, there are two machines, one of which is machine A which backs up the database every day. I want another machine B to copy from A to B's directory at regular intervals, A does not share the Database Backup Directory. Considering that the FTP method cannot cross NAT (Network Address Translation), and the HTTP method is not guaranteed in terms of security and reliability, there will also be some restrictions when the file is large. The client and server programs are written based on the HTTP protocol. A client uses an httpwebrequest object to send post and get requests to a specified URL, and the Server accepts the requests through the program, in this way, the sending and receiving operations are controlled by the programs you write, allowing you to conveniently implement functions such as identity verification, data encryption, and resumable data transfer, both firewall and Nat can be used. In this way, files can be encrypted across the Internet in. net.
In. net program source code for cross-Internet File Encryption Transmission 1. Data upload 1. client code // obtain the full name of the file from the text box, including the path string strfilepath = txupfile. text; // obtain the file length fileinfo Fi = new fileinfo (strfilepath); long lfilelength = Fi. length; // obtain the file name string strfilename = strfilepath. substring (strfilepath. lastindexof ("//") + 1); // create an httpwebrequest object, pass in the path name, and pass the file name and length as parameters to the server httpwebrequest objrequest = (httpwebrequest) httpwebrequest. create (string. format (@ "{0 }? Filename = {1} & filelength = {2} ", txupurl. text, strfilename, lfilelength. tostring (); // The method for defining the request object is "Post" objrequest. method = "Post"; // defines the content type of the request object as "application/octet-stream" objrequest. contenttype = "application/octet-stream"; // defines the length of the request content. contentlength = (INT) (lfilelength/8 + 1) * 8; // defines the stream reqstream = objrequest of a request. getrequeststream (); // use the DES algorithm to encrypt the request stream byte [] bytes ey = {,}; byte [] desiv = {1, 2, 3, 4, 5, 6, 7,8}; Des = new descryptoserviceprovider ();
Cryptostream encstream = new cryptostream (reqstream, Des. createencrytor (Cipher ey, desiv), cryptostreammode. Write );
// Define the memory buffer for file read/write
Int ibuffersize = 4095; byte [] buffer = new byte [ibuffersize]; // open the file and prepare to read filestream = new filestream (strfilepath, filemode. open, fileaccess. read); int ireadlength = 0; // read the file content into the buffer zone
Ireadlength = filestream. Read (buffer, 0, buffer. Length );
While (ireadlength! = 0 ){
// Write the read file content to the encrypted stream
Encstream. Write (buffer, 0, ireadlength );
Ireadlength = filestream. Read (buffer, 0, buffer. Length );
}
// Close the request stream
Encstream. Close ();
Reqstream. Close ();
Filestream. Close ();
// Request to the server and obtain result 3
Httpwebresponse sp = (httpwebresponse) objrequest. getresponse ();
String strcontent = "";
Int ilen = (INT) sp. contentlength;
If (ilen> 0 ){
// Write the result stream to the binary stream bcontent
Stream resstream = sp. getresponsestream ();
Byte [] bcontent = new byte [sp. contentlength];
Resstream. Read (bcontent, 0, ilen );
Sp. Close ();
// Because the result is a binary data, the result must be decoded into a string.
Char [] charcontent = new char [sp. contentlength];
Text. decoder Dc = text. encoding. utf8.getdecoder ();
Int charlen = Dc. getchars (bcontent, 0, bcontent. length, charcontent, 0 );
Foreach (char C in charcontent) strcontent + = C. tostring ();
}
2. server code
// Determine the contenttype of the request and filter illegal requests
If (request. contenttype! = "Application/octet-stream") return;
// Obtain the uploaded file name and length
String strfilename = request. querystring ["FILENAME"];
Int ifilelength = request. totalbytes;
// Construct the Server File Name
String strfilepath = server. mappath (".") + IO. Path. directoryseparatorchar + "Upload" + IO. Path. directoryseparatorchar + strfilename;
// Determine whether a file exists
If (file. exists (strfilepath )){
// If the object exists, the file name and length are returned.
Fileinfo Fi = new fileinfo (strfilepath );
Long lfilelength = Fi. length;
Response. Write ("file [" + strfilename + "] exists! Length = "+ lfilelength. tostring ());
Response. End ();
}
// If the file does not exist, open the file stream to create the file.
Filestream = file. Create (strfilepath, ifilelength );
// Use the DES algorithm to decrypt the file stream
Byte [] cipher ey = {,}; byte [] desiv = {,}; Des = new descryptoserviceprovider (); cryptostream desstream = new cryptostream (filestream, Des. createdecryptor (Cipher ey, desiv), cryptostreammode. write); // defines the memory buffer size int ibuffersize = 4095; // reads the request stream and writes the byte [] buffer = new byte [ibuffersize] to the file; int ireadlength = 0; ireadlength = request. inputstream. read (buffer, 0, buffer. length); While (ireadlength> 0) {desstre Am. write (buffer, 0, ireadlength); desstream. flush (); ireadlength = request. inputstream. read (buffer, 0, buffer. length);} desstream. flush (); desstream. close (); filestream. close (); // response result response. write ("file [" + strfilename + "] uploadsuccess! "); Response. end (); 2. Data download 1. client code // obtain the download file name string strfilename = txdownfile. text; // define the file offset int ioffset = 0; // create an httpwebrequest object, pass in the path name, and pass the file name and offset as parameters to the server httpwebrequest objrequest = (httpwebrequest) httpwebrequest. create (string. format (@ "{0 }? Filename = {1} & offset = {2} ", txdownurl. text, strfilename, ioffset); // defines the request object method as "get" objrequest. method = "get"; // defines the content type of the request object as "application/octet-stream" objrequest. contenttype = "application/octet-stream" // obtain the request result httpwebresponse sp = (httpwebresponse) objrequest. getresponse (); stream resstream = sp. getresponsestream (); string strfilepath = txdownpath. text; // if the object exists, delete if (file. exists (strfilepath) {file. delete (Strfilepath);} // create the file int ibuffersize = 4095; filestream = file. create (strfilepath); // decrypt the returned stream byte [] bytes ey = {,}; byte [] desiv = }; des = new descryptoserviceprovider (); cryptostream decstream = new cryptostream (resstream, Des. createdecryptor (Cipher ey, desiv), cryptostreammode. read); byte [] buffer = new byte [ibuffersize]; int ireadlength = 0; // read the returned stream ireadlength = decstream. read (Buffer, 0, buffer. length); While (ireadlength> 0) {// write filestream into the file stream. write (buffer, 0, ireadlength); ireadlength = decstream. read (buffer, 0, buffer. length);} filestream. flush (); decstream. close (); filestream. close (); resstream. close (); Return "file [" + strfilename + "] Download success! ";
2. server code
// Obtain the uploaded file name and offset
String strfilename = request. querystring ["FILENAME"];
Long loffset = convert. toint64 (request. querystring ["offset"]);
// Construct the full path name of the file
String strfilepath = server. mappath (".") + IO. Path. directoryseparatorchar + "Download" + IO. Path. directoryseparatorchar + strfilename;
// If the object does not exist, an error message is returned.
If (! File. exists (strfilepath )){
Response. Clear ();
Response. Write ("file [" + strfilename + "] Not exists! ");
Response. End ();
}
// Open the file
Io. filestream = new IO. filestream (strfilepath, Io. filemode. Open, Io. fileaccess. Read, Io. fileshare. Read );
// Set the offset based on the uploaded offset parameter
If (loffset> 0)
Filestream. Seek (loffset, Io. seekorigin. Begin );
// Define the output stream
Stream resstream = response. outputstream;
// Use the DES algorithm to encrypt the data stream
Byte [] cipher ey = {,}; byte [] desiv = {,}; Des = new descryptoserviceprovider (); cryptostream encstream = new cryptostream (resstream, Des. createdecryptor (Cipher ey, desiv), cryptostreammode. write); // define the header response of the output file stream. contenttype = "application/octet-stream"; response. addheader ("content-disposition", "attachment; filename =" + strfilename); int ibuffersize = 4095; byte [] buffer = new byte [ibuffersize]; int ireadlength = 0; // read the file ireadlength = filestream. read (buffer, 0, buffer. length); While (ireadlength> 0) {// write the output stream encstream. write (buffer, 0, ireadlength); ireadlength = filestream. read (buffer, 0, buffer. length);} encstream. flush (); resstream. flush (); // close the stream encstream. close (); resstream. close (); filestream. close (); // response. flush (); // end the output response. end ();