C# 用HttpWebRequest进行最大并发数的问题 100
再窗体那边设置了1条线程来,执行下面的POST方法往某服务器提交数据,速度大概是每秒1条但是我设置了10条线程来POST数据后,速度并没有什么提升把ServicePoin...
再窗体那边设置了1条线程来,执行下面的POST方法往某服务器提交数据,速度大概是每秒1条
但是我设置了10条线程来POST数据后,速度并没有什么提升
把 ServicePointManager.DefaultConnectionLimit 改为了512也没什么改变,请问该如何解决这样的问题呢?不胜感激
//#Http.cs#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
namespace QKB
{
class Http
{
public static string cookie = "";
public static string Post(string postUrl, string paramData, Encoding dataEncode)
{
string ret = string.Empty;
try
{
byte[] byteArray = dataEncode.GetBytes(paramData); //转化
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
webReq.ServicePoint.Expect100Continue = false;
webReq.KeepAlive = false;
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.ContentLength = byteArray.Length;
webReq.AllowAutoRedirect = true;
webReq.Headers.Set(HttpRequestHeader.Cookie, cookie);
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);//写入参数
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
//如果有cookie,则保存
if (response.Headers.AllKeys.Contains("Set-Cookie"))
{
cookie = response.Headers["Set-Cookie"];
Console.WriteLine("cookie : " + cookie);
}
//webReq.Abort();
ret = sr.ReadToEnd();
sr.Close();
response.Close();
newStream.Close();
}
catch{}
return ret;
}
}
} 展开
但是我设置了10条线程来POST数据后,速度并没有什么提升
把 ServicePointManager.DefaultConnectionLimit 改为了512也没什么改变,请问该如何解决这样的问题呢?不胜感激
//#Http.cs#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
namespace QKB
{
class Http
{
public static string cookie = "";
public static string Post(string postUrl, string paramData, Encoding dataEncode)
{
string ret = string.Empty;
try
{
byte[] byteArray = dataEncode.GetBytes(paramData); //转化
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
webReq.ServicePoint.Expect100Continue = false;
webReq.KeepAlive = false;
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.ContentLength = byteArray.Length;
webReq.AllowAutoRedirect = true;
webReq.Headers.Set(HttpRequestHeader.Cookie, cookie);
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);//写入参数
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
//如果有cookie,则保存
if (response.Headers.AllKeys.Contains("Set-Cookie"))
{
cookie = response.Headers["Set-Cookie"];
Console.WriteLine("cookie : " + cookie);
}
//webReq.Abort();
ret = sr.ReadToEnd();
sr.Close();
response.Close();
newStream.Close();
}
catch{}
return ret;
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询