Web请求失败,但仅适用于本地主机以及使用System.Net.HttpWebRequest时 [英] Web request fails, but only for localhost and when using System.Net.HttpWebRequest

查看:412
本文介绍了Web请求失败,但仅适用于本地主机以及使用System.Net.HttpWebRequest时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在System.Net.HttpWebRequest上遇到了一个非常奇怪的问题,这让我发疯了:当我使用localhost地址运行它时,它要么非常慢(大约30秒),要么在大多数情况下会变得很慢完全超时.仅针对以localhost为目标的请求,并且仅来自C#代码.

I've got a very weird problem with System.Net.HttpWebRequest, that's driving me nuts: When I run it with a localhost address, then it is either extremely slow (around 30 sec) or, in most cases, it times out entirely. This happens only with requests that target localhost, and only from C# code.

具体来说,当我有以下代码时,以ASP.NET Web API项目为例:

Concretely, taking an ASP.NET Web API project as an example, when I have this code:

[Test]
public void TestRequest()
{
    var request = WebRequest.Create("http://localhost:64497/api/values");
    WebResponse response = request.GetResponse();
}

..它在大多数情况下都会因以下异常而失败:

..it will most of the time fail with this exception:

System.Net.WebException:无法连接到远程服务器
----> System.Net.Sockets.SocketException:连接尝试失败,因为经过一段时间后被连接方未正确响应 时间或建立的连接失败,因为连接的主机有 未能回复127.0.0.1:64497

System.Net.WebException : Unable to connect to the remote server
----> System.Net.Sockets.SocketException : A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond 127.0.0.1:64497

通过检查.NET代码,我将错误跟踪到

By inspecting the .NET code, I tracked down the error to the WSAConnect API returning a non-null value, but I can't see that this is of much help. (However, this is a 32bit API, whereas I'm running on a 64bit system. Could there be some strange 32/64bit problem?)

当我使用本地主机以外的地址运行上述代码时(例如 http://www.google.com ),那么它就会正常工作,并且我得到大约1秒的响应时间. 另外,当我从其他客户端提交上述请求时(我使用了Chrome的 Advanced REST Client ),那么我的行为便是正常的:由于初始编译,第一个请求的延迟时间约为5-8秒,而所有后续请求的响应时间都非常短(20-40毫秒).

When I run the above code with an address other than localhost (say http://www.google.com), then it behaves normally and I get response times around 1 sec. Also, when I submit the above request from a different client (I used Chrome's Advanced REST Client), then I get the normal behaviour: A delay time of about 5-8 sec for the first request due to initial compilation, and very short response times (20-40 msec) for all subsequent requests.

基于这些发现,在我看来,问题很明显必须来自特定于System.Net.HttpWebRequest类实现的问题...

Given these findings, it seems quite obvious to me that the problem must come from something that's specific to the implementation of the System.Net.HttpWebRequest class...

Web应用程序本身托管在IIS Express上,但是我非常确定这是不相关的,因为上述请求从未到达Web服务器(我通过从命令行运行IIS Express,然后检查请求,也取决于ProcessMonitor).

The web application itself is hosted on IIS Express, but I'm quite sure that this is irrelevant, since the above request never reaches the web server (I checked that both by running IIS Express from the command line and then inspecting the requests coming through and also per ProcessMonitor).

其他信息: 我正在运行Win 8.1 64bit和VS 2013 SP4 pro. 系统的HOST文件未修改,即不包含任何条目.

Further information: I'm running Win 8.1 64bit and VS 2013 SP4 pro. The system's HOST file is unmodified, i.e. it does not contain any entries.

到目前为止我已经尝试过的事情:

Things I've tried so far:

  • 以管理员模式运行VS
  • 关闭所有防火墙和防病毒软件
  • 使用IIS而不是IIS Express
  • 将每个代码和每个配置的proxysettings显式设置为null
  • 根据 ProcessMonitor 检查IIS Express进程-无结果(如我说过,我很确定Web服务器不会直接参与此问题)
  • localhost更改为127.0.0.1
  • Running VS in admin mode
  • Switching off all firewall and antivirus software
  • using IIS instead of IIS Express
  • setting proxysettings explicitly to null, both per code and per configuration
  • inspecting the IIS Express process per ProcessMonitor - no result (as I said, I'm quite sure that the Web server is not directly involved in this problem)
  • changing localhost to 127.0.0.1

我不明白问题是什么,并且我在网络上找不到任何相关信息.我什至没有更多的想法可以找到该错误的方法.换句话说:我完全坚持这一点,这有效地阻止了我针对本地托管的Web编写任何集成测试.任何帮助/提示/提示都将受到高度赞赏.

I don't understand what the problem is, and I can't find any relevant info on the web. I don't even have any more ideas what I could try to track down the error. In other words: I'm totally stuck on this, and it effectively prevents me from writing any integration tests against locally hosted webs. Any help/hint/tip is highly appreciated.

推荐答案

只需检查以下部分: 通过检查.NET代码,我找到了返回非空值的WSAConnect API的错误,但是我看不到这有很大帮助.(但是,这是32位API,而我在64位系统上运行.是否会出现一些奇怪的32/64位问题?)"

Just to check this part: "By inspecting the .NET code, I tracked down the error to the WSAConnect API returning a non-null value, but I can't see that this is of much help. (However, this is a 32bit API, whereas I'm running on a 64bit system. Could there be some strange 32/64bit problem?)"

确保在IIS中为应用程序池启用了32位(默认为禁用).

Make sure that in IIS, for the application pool, that you have 32-bit enabled (default is disabled).

此外,我假设绑定设置正确. ([绑定]的最后部分应该是评论,但我没有代表-它已被删除,显然-或将是)

Also, I assume the binding is set correctly. (that last part [binding] was supposed to be a comment, but I didn't have the rep - it was deleted, apparently-or will be)

这篇关于Web请求失败,但仅适用于本地主机以及使用System.Net.HttpWebRequest时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆