Scene
Now the company's test environment some text log does not allow access to provide a network of HTTP server for direct access, this file is large, general 10MB a file, not the same LAN, speed limit 200K, to wait for a long time, access is very slow. . NET code requests the latest content of a text file (similar to tail file)
Namespace ConsoleApplication3 {class Program {static void Main (string[] args) {WHI
Le (True) {//var filelength = 0L;
var sposstion = 0;
int completedlength = 0; var FilePath = configurationmanager.appsettings["Txtfilepath"].
ToString ();
Console.WriteLine ("Network TXT file path: {0}", FilePath);
var lengthrequest = (HttpWebRequest) httpwebrequest.create (FilePath); Lengthrequest.method = "Head";//Get file headers only, do not get content, speed fast var contentlength = ((HttpWebResponse) lengthrequest.getre Sponse ()).
ContentLength;
try {lengthrequest.abort (); The catch (Exception ex) {Console.WriteLine (ex).
message); var getlastlength = Convert.ToInt32 (configurationmanager.appsettings["Getlastlength"). ToString ());
HttpWebRequest myrequest = (HttpWebRequest) httpwebrequest.create (FilePath); if (ContentLength > Getlastlength) {myrequest.addrange ((int) (Contentlength-getla STLENGTH);//Set range value}//request to server, get server response data stream HttpWebResponse WebResponse =
(HttpWebResponse) myrequest.getresponse (); Filelength = webresponse.contentlength + sposstion;//File size = downloaded + server returned Stream MyStream = Webresponse.get
Responsestream ();
byte[] btcontent = new Byte[getlastlength];
Save ();//Saving Download Information if ((Completedlength = Mystream.read (btcontent, 0, getlastlength)) > 0)
{Console.WriteLine (Encoding.UTF8.GetString (btcontent));
Fstream.write (btcontent, 0, completedlength);
Downloadedlength + = Completedlength; if (Bestop = = TRUE) break;
else {Console.WriteLine ("No Content");
}//fstream.close ();
Mystream.close ();
Console.WriteLine ("Last Refresh Time:" +datetime.now.tostring ("Yyyy-mm-dd hh:m:ss.fff"));
Console.WriteLine ("\ r \ n---------Press any key to refresh----------");
Console.readkey ();
Console.clear ();
}
}
}
}
Config file configuration:
<appSettings>
<add key= "Txtfilepath" value= "Http://www.text.com/Info/Info.log"/> <add key=
"Getlastlength" value= "2048"/>
</appSettings>
run the effect, less than 1 seconds out, refresh fast