webrequest get 抛出异常“远程服务器返回错误:(501) 未实现。”

发布于 2024-10-12 18:02:16 字数 2910 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一念一轮回 2024-10-19 18:02:16

“未实现”是因为您正在为 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. :)

芸娘子的小脾气 2024-10-19 18:02:16

问题出在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文