所以我有一个程序必须发出 POST 请求。请求时间太长了!代码是:
string entity = "some text in here";
byte[] _entity = Encoding.UTF8.GetBytes(entity);
Stopwatch sw = new Stopwatch();
sw.Start();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.ContentLength = _entity.Length;
request.UserAgent = UserAgent;
request.ContentType = "application/json";
request.Accept = "application/json";
request.Headers["CustomHeader"] = "value";
request.Headers["AnotherCustom"] = "valueAgain";
request.Headers["AnotherHeader"] = "value";
request.Headers["AnotherOne"] = "value";
request.Referer = "referer";
using (Stream stream = request.GetRequestStream())
{
stream.Write(_entity, 0, _entity.Length);
}
WebResponse response = request.GetResponse();
string responseString = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
responseString = reader.ReadToEnd();
}
sw.Stop();
Console.WriteLine("took: " + sw.ElapsedMilliseconds);
当我的程序加载时,我设置了这些:
ServicePointManager.DefaultConnectionLimit = 1000;
ServicePointManager.UseNagleAlgorithm = false;
ServicePointManager.Expect100Continue = false;
WebRequest.DefaultWebProxy = null;
我还能做些什么来提高速度?对我发布的网站的 ping 也只有 5 毫秒,我的上传速度为 10mb/s,但每次请求大约需要 400 毫秒。
发送更少的标头会起作用吗?