除非 fiddler 运行,否则 HttpWebRequest 不起作用
这可能是我遇到过的最奇怪的问题。我有一段代码可以将 POST 提交到 url。当 fiddler 未运行时,代码不起作用,也不会引发任何异常,但是,当 fiddler 运行时,代码会成功发布数据。我可以访问帖子页面,因此我知道数据是否已发布。这可能很无意义,但这是我遇到的情况,我很困惑。
byte[] postBytes = new ASCIIEncoding().GetBytes(postData);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://myURL);
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10";
req.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
req.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
req.Headers.Add("Accept-Language", "en-US,en;q=0.8");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = postBytes.Length;
req.CookieContainer = cc;
Stream s = req.GetRequestStream();
s.Write(postBytes, 0, postBytes.Length);
s.Close();
This is probably the weirdest problem I have run into. I have a piece of code to submit POST to a url. The code doesn't work neither throws any exceptions when fiddler isn't running, However, when fiddler is running, the code posts the data successfuly. I have access to the post page so I know if the data has been POSTED or not. This is probably very non-sense, But it's a situation I am running into and I am very confused.
byte[] postBytes = new ASCIIEncoding().GetBytes(postData);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://myURL);
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10";
req.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
req.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
req.Headers.Add("Accept-Language", "en-US,en;q=0.8");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = postBytes.Length;
req.CookieContainer = cc;
Stream s = req.GetRequestStream();
s.Write(postBytes, 0, postBytes.Length);
s.Close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您不调用
GetResponseStream()
,则无法关闭响应。如果您不关闭响应,那么.NET
中的套接字最终会处于错误状态。您必须关闭响应以防止干扰您以后的请求。If you don't call
GetResponseStream()
then you can't close the response. If you don't close the response, then you end up with a socket in a bad state in.NET
. You MUST close the response to prevent interference with your later request.获取后关闭
HttpWebResponse
。我遇到了同样的问题,然后我开始在每个请求后关闭响应,然后繁荣,不需要运行 fiddler。
这是同步代码的伪代码:
Close the
HttpWebResponse
after getting it.I had the same problem, then I started closing the responses after each request, and Boom, no need to have fiddler running.
This is a pseudo of a synchronous code:
我最近也遇到了类似的问题。 Wireshark 会显示 HTTPWebRequest 不会离开客户端计算机,除非 Fiddler 正在运行。我尝试删除代理设置,但这并没有解决我的问题。我尝试了一切,从将请求设置为 HttpVersion.Version10、启用/禁用 SendChuck、KeepAlive 以及许多其他设置。这些都不起作用。
最终,我只是检查 .Net 是否检测到代理并让请求尝试忽略它。这解决了我的 request.GetResponse() 抛出立即异常的问题。
I had a similar problem recently. Wireshark would show the HTTPWebRequest not leave the client machine unless Fiddler was running. I tried removing proxy settings, but that didn't fix the problem for me. I tried everything from setting the request to HttpVersion.Version10, enabling/disabling SendChuck, KeepAlive, and a host of other settings. None of which worked.
Ultimately, I just checked if .Net detected a proxy and had the request attempt to ignore it. That fixed my issue with request.GetResponse() throwing an immediate exception.
就我而言,当我遇到相同的情况时(POST 仅在 Fiddler 运行时有效),代码将 POST 从代理后面的开发环境中的 IISExpress 上运行的应用程序发送到外部服务器。显然,即使您在 Internet 选项中配置了代理设置,IIS 运行的环境也可能无法访问它们。在我的工作环境中,我只需使用代理配置脚本的路径更新 web.config 即可。 您可能需要调整其他代理设置。在这种情况下,您的朋友就是这个 MSDN 页面,它解释了它们是什么: http:// msdn.microsoft.com/en-us/library/sa91de1e.aspx。
最终,我将以下内容包含在应用程序的 web.config 中,然后完成 POST。
In my case when I had the same situation (POST only works when Fiddler is running) the code was sending the POST from an application running on IISExpress in a development environment behind a proxy to an external server. Apparently even if you have proxy settings configured in Internet Options the environment IIS is running in may not have access to them. In my work environment I simply had to update web.config with the path to our proxy's configuration script. You may need to tweak other proxy settings. In that case your friend is this MSDN page that explains what they are: http://msdn.microsoft.com/en-us/library/sa91de1e.aspx.
Ultimately I included the following in the application's web.config and then the POST went through.
好吧,几周前我也遇到过类似的问题,原因是当 fiddler 运行时,它会更改代理设置以通过 Fiddler 传递请求,但是当其关闭时,代理仍以某种方式保留,因此不允许您的请求继续进行互联网。
我尝试将 IE 和 Firefox 的网络设置设置为不使用任何代理,结果成功了。
试试这个,可能是同样的问题...
Well i have faced similar problem few weeks back and the reason was that when fiddler is running it changes the proxy settings to pass the request through Fiddler but when its closed the proxy somehow still remains and thus doesn't allow your request to go ahead on internet.
I tried by setting the IE's and Firefox's network settings to not to take any proxy and it worked.
Try this, may it be the same problem...
我在使用 Python 时遇到了同样的问题 - 对本地服务器的请求因 404 而失败,但当我使用 Fiddler 运行它们时,它们工作正常。
这里问题的真正线索是 Fiddler 通过充当 HTTP 流量的代理来工作,以便来自本地计算机的所有请求都通过 Fiddler,而不是直接进入网络。
在我所处的确切情况下,我向本地服务器发出请求,常规流量通过代理,并在
局域网 (LAN) 设置
中进行网络连接代理服务器< /code> 窗格中选中了
绕过本地地址的代理服务器
选项。我怀疑“绕过本地地址的代理服务器”不一定是由编程语言拾取的,但代理服务器的详细信息却是。 Fiddler 知道该策略,因此通过 Fiddler 的请求可以工作,但直接来自编程语言的请求则不能。
通过将本地服务器请求的代理设置为空,它可以从代码正常工作。显然,如果您发现自己在部署期间从内部服务器转移到外部服务器,这可能是一个陷阱。
I ran into the same problem with Python - requests to a local server were failing with a 404, but then when I ran them with Fiddler running they were working correctly.
The real clue to the problem here is that Fiddler works by acting as a proxy for HTTP traffic so that all requests from the local machine go through Fiddler rather than straight out into the network.
In the exact situation I was in, I was making requests to a local server, regular traffic passes through a proxy and in
Local Area Network (LAN) Settings
for the network connection theProxy server
pane theBypass proxy server for local addresses
option was checked.My suspicion is that the "Bypass proxy server for local addresses" is not necessarily picked up by the programming language, but the proxy server details are. Fiddler is aware of that policy, so requests through Fiddler work but requests direct from the programming language don't.
By setting the proxy for the request for the local server to nothing, it worked correctly from code. Obviously, that could be a gotcha if you find yourself moving from an internal to external server during deployment.
我遇到了同样的情况:我正在 POSTing 到 Windows 身份验证后面的端点。
Fiddler保留一个打开的连接池,但您的 C# 测试或 powershell 脚本在没有 fiddler 的情况下运行时不会。
因此,您可以通过将
HttpWebRequest
上的属性UnsafeAuthenticatedConnectionSharing
设置为 true,使测试/脚本也维护一个开放的经过身份验证的连接池。请阅读此处了解更多信息,Microsoft 知识库。在该文章的两种情况下,您都可以看到他们发出两个请求。第一个是简单的 GET 或 HEAD,用于获取身份验证标头(完成握手),第二个是 POST,它将使用之前获得的标头。显然你不能(悲伤)直接使用 POST http 请求进行握手。
I faced the same scenario : I was POSTing to an endpoint behind Windows Authentication.
Fiddler keeps a pool of open connections, but your C# test or powershell script does not when it runs without fiddler.
So you can make the test/script to also maintain a pool of open authenticated connections, by setting the property
UnsafeAuthenticatedConnectionSharing
to true on yourHttpWebRequest
. Read more about it here, microsoft KB. In both cases in that article, you can see that they are making two requests. The first one is a simple GET or HEAD to get the authentication header (to complete the handshake), and the second one is the POST, that will use the header obtained before.Apparently you cannot (sadness) directly do the handshake with POST http requests.
始终使用 using 构造。它确保调用后释放所有资源
,将以下条目添加到 webconfig 文件
Always use using construct. it make sure all resource release after call
add following entries to webconfig file
我找到了增加默认连接数的解决方案
I found the solution in increasing the default number of connections