Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WebRequest.GetResponse()占用的时间太长_C#_.net_Web Services - Fatal编程技术网

C# WebRequest.GetResponse()占用的时间太长

C# WebRequest.GetResponse()占用的时间太长,c#,.net,web-services,C#,.net,Web Services,我正在编写一个函数来查询本地托管在我的计算机上的Web服务。它工作得很好,但是GetResponse方法比我预期的要花更多的时间。更具体地说,当我在浏览器上执行请求时,大约需要10毫秒,而GetResponse方法则需要300毫秒 我是不是在代码上做错了什么?我可以改进吗 public static string CargarListaRutas() { WebRequest request = HttpWebRequest.Create("http://loca

我正在编写一个函数来查询本地托管在我的计算机上的Web服务。它工作得很好,但是GetResponse方法比我预期的要花更多的时间。更具体地说,当我在浏览器上执行请求时,大约需要10毫秒,而GetResponse方法则需要300毫秒

我是不是在代码上做错了什么?我可以改进吗

    public static string CargarListaRutas()
    {
        WebRequest request = HttpWebRequest.Create("http://localhost:8080/services/rest/184108301/listaRutas/");
        request.Timeout = 2000;
        WebResponse response;
        string responseFromServer;
        try
        {
            using (response = request.GetResponse())
            {
                Stream dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);
                responseFromServer = reader.ReadToEnd();
            }
        }
        catch
        {
            responseFromServer = String.Empty;
        }

        return responseFromServer;
    }

您的浏览器可能会更快,因为它使用了缓存。尝试CTRL+F5以强制浏览器重新加载页面,而不使用缓存来测试计时。

您确定这不是因为浏览器使用的是缓存版本吗?您可以尝试CTRL+F5强制浏览器重新加载页面,而不使用缓存来测试计时。如果itsme86的注释没有帮助。这可能是由自动代理检测引起的。尝试设置请求。代理=null。看到了吗?86,你是对的。当我清理缓存时,请求需要大约300毫秒,就像我的应用程序一样。谢谢你的帮助。如果你想回答,我会接受的。谢谢