我有一個使用20周左右的螺紋連接到遠程Web服務器併發送任意HTTP請求的大小相當小,通過SSL 100%的控制檯應用程序做一個C#應用程序。遠程Web服務器實際上是一個完整的負載均衡數據中心,其中包含高可用性系統,每秒可處理數十萬個請求。這不是服務器或帶寬問題。說到這裏,我不會運行它,也不會對它的配置有任何影響,所以即使我願意,我也無法進行服務器端更改。如何使用HttpWebRequest的行爲很像提琴手
當使用fiddler運行應用程序時,應用程序執行得非常快。當不在小提琴手中運行時,它真的非常慢,從而無法完成手頭的任務。它似乎也在這個過程的早期鎖定在某個時刻,但這可能只是一個僵局問題,我還不確定。
總之,小提琴手是一個代理,無疑是改變我的請求/以某種方式確保精彩吞吐量連接,但是我不知道它在做。我想弄明白,這樣我可以強迫我的.NET應用程序模仿小提琴手連接處理行爲而實際上不必通過提琴手
我已經粘貼了以下連接代碼來運行它。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace Redacted
{
public class HiveCommunicator
{
public static IResponse SendRequest(IRequest request) {
ServicePointManager.DefaultConnectionLimit = 60;
ServicePointManager.Expect100Continue = false;
string hostUrlString = string.Empty;
if (request.SiteID <= 0)
hostUrlString = string.Format("{0}://{1}{2}", request.UseSSL ? "https" : "http", DataCenters.GetCenter(request.DataCenter), request.Path);
else
hostUrlString = string.Format("{0}://{1}{2}", request.UseSSL ? "https" : "http", DataCenters.GetCenter(request.DataCenter), string.Format(request.Path, request.SiteID));
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(hostUrlString);
switch (request.ContentType)
{
default:
case ContentTypes.XML:
webRequest.ContentType = "application/xml";
break;
case ContentTypes.JSON:
webRequest.ContentType = "application/json";
break;
case ContentTypes.BINARY:
webRequest.ContentType = "application/octet-stream";
break;
}
if (request.RequiresAuthorizationToken)
{
AuthorizationToken tok = HiveAuthentication.GetToken(request.SiteID);
if (tok == null)
{
return null;
}
webRequest.Headers.Add(HttpRequestHeader.Authorization, tok.Token);
}
bool UsesRequestBody = true;
switch (request.HttpVerb)
{
case HttpVerbs.POST:
webRequest.Method = "POST";
break;
case HttpVerbs.DELETE:
webRequest.Method = "DELETE";
UsesRequestBody = false;
break;
case HttpVerbs.PUT:
webRequest.Method = "PUT";
break;
default:
case HttpVerbs.GET:
webRequest.Method = "GET";
UsesRequestBody = false;
break;
}
HttpWebResponse webResponse = null;
Stream webRequestStream = null;
byte[] webRequestBytes = null;
if (UsesRequestBody)
{
webRequestBytes = request.RequestBytes;
webRequest.ContentLength = webRequestBytes.Length;
webRequestStream = webRequest.GetRequestStream();
for (int i = 0; i < webRequest.ContentLength; i++)
{
webRequestStream.WriteByte(webRequestBytes[i]);
}
}
try
{
webResponse = (HttpWebResponse)webRequest.GetResponse();
}
catch (WebException ex)
{
webResponse = (HttpWebResponse)ex.Response;
}
if (UsesRequestBody)
{
webRequestStream.Close();
webRequestStream.Dispose();
}
IResponse respReturn = request.ParseResponse(webResponse);
webResponse.Close();
return respReturn;
}
}
}
我也遇到了這個頭的問題。我無法理解MS爲何如此難以改變。 –