调用 HttpWebRequest.GetResponse() 时出现协议错误
我有一个页面包含一些文件的链接。
我基本上需要访问页面的源代码来解析它并获取文件的所有超链接。
我的代码是这样的(我在网上的很多地方找到了一些代码..):
"private static byte[] ReadImageFromUrl(string url)
{
var myReq = (HttpWebRequest)WebRequest.Create(url);
myReq.Timeout = 10000;
WebResponse myResp = myReq.GetResponse();
Stream stream = myResp.GetResponseStream();
List<byte> bytesList = new List<byte>();
using (var br = new BinaryReader(stream))
{
try
{
while (true)
{
var b = br.ReadByte();
bytesList.Add(b);
}
}
catch (Exception)
{}
br.Close();
}
myResp.Close();
return bytesList.ToArray();
}"
现在的问题是我得到“System.Net.WebException:远程服务器返回错误:(500)内部服务器错误。”调用“myReq.GetResponse()”时 - 检查错误,我发现状态为“ProtocolError”。
WebException 对象的响应属性包含一些服务器错误..(尽管从浏览器打开它时它会正确打开)...此外,当我使用其中一个文件的 url 调用此函数时,我会得到相同的 ProtocolError 状态,但是404 错误...
请给出任何提示我如何解决它...或完成此任务的任何其他可能性。
谢谢 !
I have a page containing links to some files.
I basically need to access the source of the page for parsing it then and obtaining all the hyperlinks to the files.
My code is something like this (some piece of code I've found in many places on the net ..):
"private static byte[] ReadImageFromUrl(string url)
{
var myReq = (HttpWebRequest)WebRequest.Create(url);
myReq.Timeout = 10000;
WebResponse myResp = myReq.GetResponse();
Stream stream = myResp.GetResponseStream();
List<byte> bytesList = new List<byte>();
using (var br = new BinaryReader(stream))
{
try
{
while (true)
{
var b = br.ReadByte();
bytesList.Add(b);
}
}
catch (Exception)
{}
br.Close();
}
myResp.Close();
return bytesList.ToArray();
}"
Now the problem is I get "System.Net.WebException: The remote server returned an error: (500) Internal Server Error." when calling "myReq.GetResponse()" - examining the error I see that the status is 'ProtocolError'.
The response property of the WebException object contains some server error ..(although when opening it from the browser it opens correctly) ...also when I call this function with the url of one of my files I get the same ProtocolError status, but the 404 error ...
Please give any hint how could I solve it... or any other possibility of accomplishing this task.
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
Fiddler
后我的新代码是:所有以const_开头的变量都取自
Fiddler
。My new code after using
Fiddler
is:All variables that start with const_ are taken from
Fiddler
.好吧,我使用 Fiddler 解决了这个问题...我将标头传递给了我的请求对象,就像我在 Fiddler 中看到的那样...&它有效,没有错误
Well, I solved that using Fiddler ... I passed to my request object the headers as I have seen them in Fiddler ...& it worked, no error