WebRequest获得一个异常,“远程服务器返回了一个错误:(501)未实现。”

StackOverflow https://stackoverflow.com/questions/4751567

我正在尝试使用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();
          }

我得到的响应还可以 - 所以看来我成功登录了。但是,我需要转到另一个URL才能获取文件 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)
    {    
       ...
    }
 }

我总是会得到一个异常系统。Exception:system.net.webexception:远程服务器返回错误:(501)未实现。在system.net.httpwebrequest.getResponse()

我已经阅读了有关此错误的所有信息 - 似乎问题应该以我的方式来解决 - 请求中有一些东西在服务器上没有正确处理 - 但我看不出是什么是错的。

有帮助吗?

解决方案 2

问题是在Postdata中。显然,远程服务器不喜欢“用户=” +用户名 +“&password =” + Pass; Sting-它正好期待这种刺痛“用户= id&password = Pass”。这没有道理,但这就是我被告知的,它起作用。非常感谢您的建议。珍妮

其他提示

“未实现”是因为您将ContentType指定为GET请求。它对服务器的意思并不意味着任何内容(主要是在发布请求期间使用的,您想提交有效载荷(例如XML)。您需要检查正确的内容类型的响应,以确保您获得了zip文件,但是要提出请求,则必须删除该ContentType规范。

我认为这也是Misterzimbu在评论中指出的。 :)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top