所以我有2個HTTP POST請求,消耗網絡API如下: -C#消耗與多個HTTP POST網頁API請求
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://your_url.com:8443/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// 1st request //
var datatobeSent = new ApiSendData()
{
UserID = "xxx",
UserPsw = "yyy",
ApiFunc = "zzz",
clearData = "x1y1z1"
};
HttpResponseMessage response = await client.PostAsJsonAsync("WebApi", datatobeSent);
var resultData = await response.Content.ReadAsStringAsync();
#region Extract Secured Data response from WebApi
JObject JsonObject = JObject.Parse(resultData);
datatobeSent.SecureData = (string)JsonObject["SecureResult"];
#endregion
// 2nd request //
var datatobeSent2 = new ApiSendData()
{
UserID = "xxx",
UserPsw = "yyy",
ApiFunc = "zzz",
SecureData = datatobeSent.SecureData
};
HttpResponseMessage response2 = await client.PostAsJsonAsync("WebApi", datatobeSent2);
var resultData2 = await response2.Content.ReadAsStringAsync();
}
所以現在我需要一些澄清...
1)我的http POST請求是否都是通過相同的SSL會話發送的? 2)如果他們不是,那麼我怎樣才能將這兩者結合起來並通過單個連接/會話發送2個請求?
3)如何提高此代碼的性能?目前100個請求需要11secs來處理和響應。 (我剛剛使用for循環,計數上述2個樣本的100個http post請求)
我相信他們是。據我所知,它們在同一個SSL會話中作爲不同的請求發送。你爲什麼不嘗試使用示例服務器? –
你是什麼樣的服務器? – Philo
託管一個免費託管服務的簡單服務器並測試您的代碼? –