webrequest get 抛出异常“远程服务器返回错误:(501) 未实现。”
我正在尝试使用 webrequest 方法加载文件。首先我需要登录然后获取文件或目录列表。 https:///xxx.yyy.zzz/login_template
当我在firefox中查看网站源代码时,我看到
<META http-equiv="Content-Type" content="text/html">
....
<form method="post" action="/template/login" enctype="application/x-www-form- urlencoded">
....
<input name="user" type="text">
<input name="password" type="password">
<input type=hidden name="switch" value="Log In">
<input type="submit" value="Accept">
所以,我写了这段代码:
public static string DownloadFile()
{
CookieContainer cookieJar = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https:///xxx.yyy.zzz/login_template");
request.CookieContainer = cookieJar;
// Set the credentials.
request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
request.Credentials = new NetworkCredential(userName, pass);
request.KeepAlive = true;
request.UserAgent = "SecureTransport";
request.ContentType = @"application/x-www-form-urlencoded";
request.Method = WebRequestMethods.Http.Post;
bool loggedin = false;
try
{
// first need to log in
string postData = "user=" + userName + "&Password=" + pass;
byte[] postBuffer = System.Text.Encoding.GetEncoding(1252).GetBytes(postData);
request.ContentLength = postBuffer.Length;
Stream newStream = request.GetRequestStream();
// Send the data.
newStream.Write(postBuffer, 0, postBuffer.Length);
newStream.Close();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
// Get the stream containing content returned by the server.
if (response.StatusCode == HttpStatusCode.OK)
{
loggedin = true;
}
response.Close();
}
我得到的响应是好的 - 所以看来我成功登录了。 但是,我需要转到另一个网址来获取文件 https:///xxx.yyy.zzz /myfile.zip
HttpWebRequest requestToGetFile = (HttpWebRequest)WebRequest.Create("https:///xxx.yyy.zzz/myfile.zip");
requestToGetFile.Method = WebRequestMethods.Http.Get;
requestToGetFile.CookieContainer = cookieJar;
requestToGetFile.UserAgent = "SecureTransport";
requestToGetFile.ContentType = @"application/octet-stream";
using (HttpWebResponse responseToGetFile = (HttpWebResponse)requestToGetFile.GetResponse())
{
if (responseToGetDir.StatusCode != HttpStatusCode.OK)
{
...
}
}
我总是收到异常 System.Exception: System.Net.WebException: 远程服务器返回错误: (501) 未实现。在 System.Net.HttpWebRequest.GetResponse()
我已经阅读了所有我能找到的有关此错误的内容 - 看来问题应该出在我所得到的方式上 - 请求中有些内容没有得到正确处理服务器 - 但我不知道出了什么问题。
I'm trying to load a file using webrequest method. First I need to log in then get a file or a directory listing. https:///xxx.yyy.zzz/login_template
When I look at the website source in firefox, I see
<META http-equiv="Content-Type" content="text/html">
....
<form method="post" action="/template/login" enctype="application/x-www-form- urlencoded">
....
<input name="user" type="text">
<input name="password" type="password">
<input type=hidden name="switch" value="Log In">
<input type="submit" value="Accept">
So, I wrote this code:
public static string DownloadFile()
{
CookieContainer cookieJar = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https:///xxx.yyy.zzz/login_template");
request.CookieContainer = cookieJar;
// Set the credentials.
request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
request.Credentials = new NetworkCredential(userName, pass);
request.KeepAlive = true;
request.UserAgent = "SecureTransport";
request.ContentType = @"application/x-www-form-urlencoded";
request.Method = WebRequestMethods.Http.Post;
bool loggedin = false;
try
{
// first need to log in
string postData = "user=" + userName + "&Password=" + pass;
byte[] postBuffer = System.Text.Encoding.GetEncoding(1252).GetBytes(postData);
request.ContentLength = postBuffer.Length;
Stream newStream = request.GetRequestStream();
// Send the data.
newStream.Write(postBuffer, 0, postBuffer.Length);
newStream.Close();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
// Get the stream containing content returned by the server.
if (response.StatusCode == HttpStatusCode.OK)
{
loggedin = true;
}
response.Close();
}
The response that I get is OK - so it seems that I successfully logged in.
But then, I need to go to another url to get a file https:///xxx.yyy.zzz/myfile.zip
HttpWebRequest requestToGetFile = (HttpWebRequest)WebRequest.Create("https:///xxx.yyy.zzz/myfile.zip");
requestToGetFile.Method = WebRequestMethods.Http.Get;
requestToGetFile.CookieContainer = cookieJar;
requestToGetFile.UserAgent = "SecureTransport";
requestToGetFile.ContentType = @"application/octet-stream";
using (HttpWebResponse responseToGetFile = (HttpWebResponse)requestToGetFile.GetResponse())
{
if (responseToGetDir.StatusCode != HttpStatusCode.OK)
{
...
}
}
I always get an exception System.Exception: System.Net.WebException: The remote server returned an error: (501) Not Implemented. at System.Net.HttpWebRequest.GetResponse()
I've read all I could find about this error - and it seems that the problem should be in the way I do get - there is something in the request that it's not being handled properly on the server - but I don't see what is wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“未实现”是因为您正在为 GET 请求指定 ContentType。它对服务器没有任何意义(它主要在 POST 请求期间使用,并且您想要提交有效负载,例如 XML)。您需要检查响应的内容类型是否正确,以确保您获得的是 zip 文件,但要发出请求,您必须删除该 ContentType 规范。
我认为这也是 Zimbu 先生在评论中指出的。 :)
The "not implemented" is because you are specifying a ContentType to a GET request. It does not mean anything to the server (It is mostly used during a POST request and you want to submit a payload such as XML). You need to check the response for the right content type to make sure you are getting a zip file but to make the request, you have to remove that ContentType specification.
I think this is what MisterZimbu is pointing out in the comment as well. :)
问题出在 postData 中。显然,远程服务器不喜欢“user =”+ userName +“&Password =”+ pass;刺痛 - 它正是期待这个刺痛“user=ID&Password=Pass”。这没有道理,但这就是我被告知的并且它有效。
非常感谢您的建议。
珍妮
The problem was in postData. Apparently, the remote server didn't like "user=" + userName + "&Password=" + pass; sting - it was expecting exactly this sting "user=ID&Password=Pass". Which doesn't make sense, but that's what I was told and it works.
Thank you very body for your suggestions.
Jenny