WebRequest C# 403 错误
好吧,这就是我正在做的。 我正在向服务器发出请求以下载文件。 我通过向网站发出 WebRequest 来获取响应,就像您通常所做的那样,尽管我收到 403 错误,指出我没有权限。
问题是,当我将 URL 插入 Google Chrome 时,我会被重定向,并且我请求的文件会下来。我在其他浏览器上尝试过该 URL,但收到 403 错误。
Google Chrome 做了什么才能绕过 403 错误?我尝试过使用 Google Chrome 用户代理,但这对我没有帮助。
帮助
Okay so here's what I'm doing.
I'm making a request to a server to pull down a file.
I do this by making a WebRequest to the website the getting the response just as you usually would, although i get a 403 error saying i don't have permissions.
Problem is when i plug the URL into Google Chrome I get redirected and the file i requested comes down. I've tried the URL on other browsers and get the 403 error.
What is Google Chrome doing that allows it to bypass the 403 error? I've tried using the Google Chrome User Agent, but that doesn't help me.
Help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
网络服务器可能会识别 Chrome 用户代理并允许其下载文件,但不允许其他用户代理下载文件。将您的 WebRequest 设置为使用与 Google Chrome 相同的用户代理字符串和设置 - 默认情况下它可能使用 IE 设置。
编辑:这是来自 MSDN 的 有关如何设置 WebRequest 的说明用户代理字符串。
要查找任何浏览器的用户代理,请在其地址栏中输入:
Possibly the web server recognizes the Chrome user agent and allows the file to be downloaded by it, but not by other user agents. Set up your WebRequest to use the same user agent strings and settings as Google Chrome - by default it might be using the IE settings.
Edit: Here's instruction from MSDN about how to set the WebRequest user agent string.
To find out the user agent of any browser, enter this into its address bar:
403 状态代码 表示您尝试访问的资源已被禁止。就像,不要问,因为你没有得到它。这与 401 代码的不同之处在于,在服务器确认资源交付之前会出现授权质询。
虽然这可能是编程配置,但这也可能是通过 Web 服务器配置的资源访问限制来解释的。
您能否验证 Google Chrome 中下载的资源是否是您尝试访问的预期资源?
The 403 status code indicates the resource you're attempting to reach is Forbidden. As in, don't ask because you're not getting it. This differs from the 401 code in that an Authorization challenge is presented before the server will confirm delivery of the resource.
While this could be programmatic configuration, this might also be explained by access restrictions on the resource as configured through the web server.
Can you verify that the downloaded resource in Google Chrome is the expected resource you're attempting to reach?
感谢您的帮助。您的回答都没有直接帮助我,但感谢您的尝试。
我的程序试图从服务器上提取文件。我已经弄清楚如何在使用 Chrome 时绕过身份验证问题并在服务器上查找文件。事实证明,他们的 Web 服务使用用户会话更加先进。
基本上我无法访问 Web 服务命令,因此我所能做的就是使用基本的 http url 来获取预定义的响应。我用来获取服务器文件的文件 url 不是唯一的,它是动态的,具体取决于网络浏览器/会话。我使用 HTTPRequest 对象来获取 HTML 文件,以便我可以解析它。使用 html 文件,我解析它以找出该文件的动态 ID(我认为它是唯一的)。然后我会将其附加到 URL 的末尾。在 Chrome 中,我会被重定向并显示下载提示。问题是,当我想下拉文件时,我需要网络浏览器对象。知道这一点后,我使用 webbrowser 对象来获取文件 ID,然后使用相同的 webbrowser 对象(技术上根据网络服务器的相同会话)来下载文件。
这相当复杂。基本上我的程序是一个小黑客,提供服务器尝试块的功能。
希望这对你们未来的项目有所帮助。
Thanks for the help. None of your answers helped me directly, but thanks for trying.
My Program was trying to pull a file off a server. I had figured out how to get around the authentication issues and finding the file on the server while using Chrome. It turns out that their web service is more advanced using user sessions.
Basically I didn't have access to the webservice commands, so all i could do is use the basic http urls to get a predefined response. The files url that i used to get the file of the server was not unique, it was dynamic depending on the webbrowser / the session. I was using the HTTPRequest object to get the HTML file so i could parse it. With the html file i parsed it to find out the dynamic ID of the file (I thought it was unique). Then I'd attach that to the end of a URL. In Chrome I would be redirected and presented with the download prompt. Problem was that I needed the webbrowser object when i wanted to pull down the file. Knowing this i used the webbrowser object to get the file ID, then used the same webbrowser object (Technically same sessions according the webserver) to pull down the file.
It's rather complicated. Basically my program is a little hack that provides functionality that the server is try block.
Hope this assisted you guys in any of your future projects.