C# webclient无法从https协议获取响应
当我尝试通过 https 从服务器加载 html 时,它返回错误代码 500:但是当我在浏览器中打开相同的链接时,它工作正常:有什么方法可以做到这一点吗?我正在使用 Webclient 并向服务器发送用户代理信息:
HttpWebRequest req1 = (HttpWebRequest)WebRequest.Create("mobile.unibet.com/";);
req1.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5";
var response1 = req1.GetResponse();
var responsestream1 = response1.GetResponseStream();
When i trying to load html from server by https, it returning an error code 500: but when i open same link in browser it works fine: is there any way to do this? I'm using Webclient and also sending a useragent information to the server:
HttpWebRequest req1 = (HttpWebRequest)WebRequest.Create("mobile.unibet.com/";);
req1.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5";
var response1 = req1.GetResponse();
var responsestream1 = response1.GetResponseStream();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
大卫是正确的,当服务器期望某些未通过的标头时,通常会发生这种情况,在您的情况下,接受
此代码现在可以工作
David is correct, this generally happens when the server is expecting some headers that is not passed through, in your case Accept
this code works now
这可能应该是一条评论,但评论中没有足够的空间来容纳所有问题......我认为这个问题没有足够的信息来以任何程度的信心回答。
500 错误 表示服务器出现问题。简而言之,浏览器正在发送一些 WebClient 没有发送的内容。
WebClient 可能未发送服务器期望的标头。服务器需要身份验证吗?这是与您签订合同的公司的页面,该公司可能为您提供了凭据或 API 密钥,您是否需要添加 HTTP 授权?
如果这是您与有合作伙伴关系的公司一起做的事情,您应该能够要求他们帮助追踪您收到 500 错误的原因。否则,您可能需要向我们提供代码示例和更多详细信息,以便我们提供更多建议。
This should probably be a comment but there's not enough room in the comment for all the questions... I don't think the question has enough information to answer with any level of confidence.
A 500 error means a problem at the server. The short answer is that the browser is sending some content that the WebClient is not.
The WebClient may not be sending headers that are expected by the server. Does the server require authentication? Is this a page on a company that you've contracted with that perhaps provided you with credentials or an API key that was Do you need to add HTTP Authorization?
If this is something you're doing with a company that you've got a partnership with, you should be able to ask them to help trace why you're getting a 500 error. Otherwise, you may need to provide us with a code sample and more details so we can offer more suggestions.