using System; using System.Net; using System.IO; using System.Threading; using System.Timers; using System.Text.RegularExpressions; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.ComponentModel; namespace TestUrl {
internal class HiPerfTimer { [DllImport("Kernel32.dll")] private static extern bool QueryPerformanceCounter( out long lpPerformanceCount); [DllImport("Kernel32.dll")] private static extern bool QueryPerformanceFrequency( out long lpFrequency); private long startTime, stopTime; private long freq; // Constructor public HiPerfTimer() { startTime = 0; stopTime = 0; if (QueryPerformanceFrequency(out freq) == false) { // high-performance counter not supported throw new Win32Exception(); } } // Start the timer public void Start() { // lets do the waiting threads there work Thread.Sleep(0); QueryPerformanceCounter(out startTime); } // Stop the timer public void Stop() { QueryPerformanceCounter(out stopTime); } // Returns the duration of the timer (in seconds) public double Duration { get { return (double)(stopTime - startTime) / (double)freq; } } } class Program { static long rt = 0; static long wr = 0; public static int PostData(string url, string data, out string info) { info = ""; CookieContainer cc = new CookieContainer(); HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.CookieContainer = cc; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; Stream requestStream = request.GetRequestStream(); byte[] byteArray = Encoding.UTF8.GetBytes(data); requestStream.Write(byteArray, 0, byteArray.Length); requestStream.Close(); HttpWebResponse response = request.GetResponse() as HttpWebResponse; Uri responseUri = response.ResponseUri; Stream receiveStream = response.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); StreamReader readStream = new StreamReader(receiveStream, encode); string result = readStream.ReadToEnd(); info = result; return 0; } public static bool GetBool(string html) { Regex regtable = new Regex(@"http://img.dotad.net/lg.gif", RegexOptions.Singleline); Match ma = regtable.Match(html); if (ma.Success) { return true; } else return false; } public static void ThreadProc() { string info = ""; string data = ""; string url = "http://203.86.46.53/lg/index.aspx"; long i=0; HiPerfTimer pt = new HiPerfTimer(); // create a new PerfTimer object pt.Start(); // start the timer while (i++<100) { PostData(url, data, out info); //System.Console.WriteLine(info); if (GetBool(info) == true) { rt++; } else { wr++; } } pt.Stop(); System.Console.WriteLine("成功" + rt + " 次,失败 " + wr + " 次\n"); Console.WriteLine("用时: {0} sec\n", pt.Duration); // print the duration of the timed code } static void Main(string[] args) { Console.WriteLine("Test\n"); // the code to be timed long j = 0; while(j++<15) { Thread t = new Thread(new ThreadStart(ThreadProc)); t.Start(); } } } }
|