HttpWebRequest.GetResponse() 返回错误 500 内部服务器错误
我正在使用 HttpWebRequest 向 url 发出请求:
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(urlAddress);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
但它会抛出错误 500(内部服务器错误),但是当我使用浏览器访问 URLAddress 时,它工作正常,urlAddress= www.khademnews.com
这是一个简单的 GET 操作,但它为我抛出异常我该如何解决这个问题?
I'm using HttpWebRequest to make a request to a url:
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(urlAddress);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
but it throws error 500 (Internal Server Error) but when i visit the URLAddress with browser it works fine, urlAddress= www.khademnews.com
it is a simple GET operation but it throws an exception for me how can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要设置用户代理,因为某些站点可能需要它。您还可以使用 WebClient 来简化您的代码:
服务器可能还需要其他标头。您可以使用 FireBug 检查在浏览器中执行请求并添加这些标头时发送了哪些标头。
You might need to set up the user agent as some sites might require it. Also you could use a WebClient to simplify your code:
The server might expect other headers as well. You could check with FireBug which headers are sent went you perform the request in your browser and add those headers.