C#多线程环境下调用 HttpWebRequest 并发连接限制

.net 的 HttpWebRequest 或者 WebClient 在多线程情况下存在并发连接限制,这个限制在桌面操作系统如 windows xp , windows 7 下默认是2,在服务器操作系统上默认为10. 如果不修改这个并发连接限制,那么客户端同时可以建立的 http 连接数就只有2个或10个。对于一些诸如浏览器或网络蜘蛛的应用,2个或10个并发数量实在太少,大大影响应用的性能。之所以有这个并发连接限制,是因为 http 1.0 和 http 1.1 标准规定并发连接数最大为2. 不过目前主流的浏览器都已经不遵循这个规则了,但 .net framework 依然默认遵循这个规则。

很多文章说用异步方式访问 HttpWebRequest 可以提高并发性能,但我测试下来,如果不修改这个默认并发连接数,同步或异步方式访问性能都很不好。

调整这个默认并发连接限制的方法很简单

只要在程序中设置:

System.Net.ServicePointManager.DefaultConnectionLimit = 512;

这个值最好不要超过1024。

我们也可以在app.config 中对最大并发连接数进行设置,方法如下:

<configuration>
<system.net>
<connectionManagement>
<add address = "http://www.google.com" maxconnection = "512" />
<add address = "*" maxconnection = "512" />
</connectionManagement>
</system.net>
</configuration>
 

修改了这个设置后,并发性能明显提高,从原来每秒钟20次直接上升到每秒钟1000多次。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# WinForm 应用程序中,可以使用 HttpWebRequest 类来实现表单上传文件,具体步骤如下: 1. 创建 HttpWebRequest 对象,并设置请求参数。同时创建一个 NameValueCollection 对象用于存储表单数据。 ```csharp HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uploadUrl); request.Method = "POST"; request.ContentType = "multipart/form-data"; NameValueCollection formData = new NameValueCollection(); ``` 2. 将表单数据添加到 NameValueCollection 对象中。 ```csharp formData.Add("key1", "value1"); formData.Add("key2", "value2"); ``` 3. 构造要上传的文件数据。 ```csharp byte[] fileData = File.ReadAllBytes(filePath); string fileName = Path.GetFileName(filePath); string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n"; string formdata = string.Format(formdataTemplate, "file", fileName); byte[] formdataBytes = Encoding.UTF8.GetBytes(formdata); byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); ``` 4. 将表单数据和文件数据写入请求流中。 ```csharp Stream requestStream = request.GetRequestStream(); foreach (string key in formData.Keys) { string formItem = string.Format("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}", key, formData[key]); byte[] formItemBytes = Encoding.UTF8.GetBytes(formItem); requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); requestStream.Write(formItemBytes, 0, formItemBytes.Length); } requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); requestStream.Write(formdataBytes, 0, formdataBytes.Length); requestStream.Write(fileData, 0, fileData.Length); byte[] trailer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); requestStream.Write(trailer, 0, trailer.Length); requestStream.Close(); ``` 5. 发送请求并获取响应。 ```csharp HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string responseString = reader.ReadToEnd(); reader.Close(); response.Close(); ``` 完整的代码示例如下: ```csharp public void UploadFileWithForm(string uploadUrl, string filePath) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uploadUrl); request.Method = "POST"; request.ContentType = "multipart/form-data"; NameValueCollection formData = new NameValueCollection(); formData.Add("key1", "value1"); formData.Add("key2", "value2"); byte[] fileData = File.ReadAllBytes(filePath); string fileName = Path.GetFileName(filePath); string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n"; string formdata = string.Format(formdataTemplate, "file", fileName); byte[] formdataBytes = Encoding.UTF8.GetBytes(formdata); byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); Stream requestStream = request.GetRequestStream(); foreach (string key in formData.Keys) { string formItem = string.Format("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}", key, formData[key]); byte[] formItemBytes = Encoding.UTF8.GetBytes(formItem); requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); requestStream.Write(formItemBytes, 0, formItemBytes.Length); } requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); requestStream.Write(formdataBytes, 0, formdataBytes.Length); requestStream.Write(fileData, 0, fileData.Length); byte[] trailer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); requestStream.Write(trailer, 0, trailer.Length); requestStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string responseString = reader.ReadToEnd(); reader.Close(); response.Close(); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值