复制代码 代码如下:
class Program {
private volatile static int errorCount = 0;
private volatile static int rightCount = 0;
static void Main(string[] args) {
ServicePointManager.DefaultConnectionLimit = 10000;
int count = 0;
int all = 0;
while (true) {
all++; count++;
CreateThread();
Thread.Sleep(10);
if (count >= 200) {
Console.WriteLine(string.Format("sucess:{0};error:{1}", all - errorCount, errorCount));
count = 0;
}
if (all > 1800)
break;
}
Console.ReadKey();
}
static void CreateThread() {
Thread thread = new Thread(ActiveRequest);
thread.IsBackground = true;
thread.Start();
}
static void ActiveRequest() {
RequestClient client = new RequestClient("http://192.168.18.2/default.aspx?d=" + Guid.NewGuid());
client.RequestProcess();
if (client.IsError) {
errorCount++;
Console.WriteLine(string.Format("错误消息:{0}", client.Messages));
} else {
rightCount++;
//Console.WriteLine(client.Messages);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace MaxLinked {
/// <summary>
///
/// </summary>
public class RequestClient {
HttpWebRequest request;
WebResponse response;
public RequestClient(string url) {
request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Timeout = int.MaxValue;
request.KeepAlive = true;
ErrorCode = -1;
}
public void AddHeader(string name, string value) {
request.Headers.Add(name, value);
}
private bool isError = false;
private StringBuilder buffer = new StringBuilder();
public int ErrorCode { get; set; }
public bool IsError {
get { return isError; }
}
public string Messages {
get { return buffer.ToString(); }
}
public void RequestProcess() {
try {
response = request.GetResponse();
} catch (WebException ex) {
ErrorCode = (int)ex.Status;
buffer.Append(ex.Message);
isError = true;
}
if (response != null) {
Stream stream = null;
StreamReader reader = null;
try {
//stream = response.GetResponseStream();
//reader = new StreamReader(stream, Encoding.UTF8);
//buffer.Append(reader.ReadToEnd());
} catch (Exception ex) {
buffer.Append(ex.Message);
isError = true;
} finally {
//if (reader != null)
// reader.Close();
//if (stream != null)
// stream.Close();
}
} else {
isError = true;
buffer.Append("建立连接失败!");
}
}
public void Close() {
if (response != null)
response.Close();
request.Abort();
}
}
}
新闻热点
疑难解答
图片精选