HttpRequest.GetResponse()花费了太多时间

时间:2016-09-07 14:04:10

标签: c# performance http system.net.httpwebrequest

我有一个outlook addin应用程序,我与Salesforce连接。 我尝试从SF API请求一些数据,这些数据通常在不到1秒的时间内返回结果,但是我的代码需要5-15秒才能完成。

我也尝试将代理设置为null。

这是我的代码:

    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
    System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
    if (includeCustomHeader == true)
    {
        req.Headers.Add("Authorization: OAuth " + ServiceManager.Token.access_token);
        req.Headers.Add("X-PrettyPrint:1");
        req.ContentType = "application/json";
    }
    else
    {
        req.ContentType = "application/x-www-form-urlencoded";
    }
    req.Method = "POST";
    req.Proxy = null;

    byte[] data = System.Text.Encoding.ASCII.GetBytes(payload);
    req.ContentLength = data.Length;
    using (var responseStream = req.GetRequestStream())
    {
        responseStream.Write(data, 0, data.Length);
    }

     //here it's taking from 5-15 seconds, each request gets a batch of 200 records
    using (var response = req.GetResponse())
    {
        return new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();
    }

不知道我错过了什么,或者还有其他原因吗? 建议?

0 个答案:

没有答案