因此,代碼:爲什麼我不能重複使用WebClient來做同樣的請求兩次?
const long testCount = 2;
const string jsonInput = "{\"blockId\":\"1\",\"userId\":\"{7c596b41-0dc3-45df-9b4c-08840f1da780}\",\"sessionId\":\"{46cd1b39-5d0a-440a-9650-ae4297b7e2e9}\"}";
Stopwatch watch = Stopwatch.StartNew();
using (var client = new WebClient())
{
client.Headers["Content-type"] = "application/json";
client.Encoding = Encoding.UTF8;
for (int i = 0; i < testCount; i++)
{
var response = client.UploadString("http://localhost:8080/BlocksOptimizationServices/dataPositions", "POST", jsonInput);
}
watch.Stop();
double speed = watch.ElapsedMilliseconds/(double)testCount;
Console.WriteLine("Avg speed: {0} request/ms", testCount);
測試性能時,我只是想打電話給client.UploadString("http://localhost:8080/BlocksOptimizationServices/dataPositions", "POST", jsonInput)
多次。但經過它總是第一個請求失敗,
"The remote server returned an error: (400) Bad Request."
如果我處理Web客戶端和重建 - 作品,但這種增加額外的性能損失。爲什麼我不能重複使用的WebClient兩次?
你是否在運行Fiddler時運行過兩次並比較了請求?後續請求顯然會到達服務器以接收400錯誤。 –