【发布时间】:2016-12-23 06:07:30
【问题描述】:
所以我有 2 个使用 web api 的 http Post 请求,如下所示:-
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 个请求需要 11 秒来处理和响应。 (我只是使用了一个 for 循环来计算上述 2 个样本的 100 个 http post 请求)
【问题讨论】:
-
我相信他们是。据我所知,它们在同一个 SSL 会话中作为不同的请求发送。为什么不使用示例服务器尝试一下?
-
示例服务器是什么意思?
-
使用免费托管服务托管一个简单的服务器并测试您的代码?
-
我确实在专用的 webapi 上测试了我的代码
-
HttpClient 被设计为可重复用于多次调用,甚至跨多个线程
标签: c# session asp.net-web-api