WebRequestは例外をスローします。「リモートサーバーはエラーを返しました:(501)実装されていません。」

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

質問

WebRequestメソッドを使用してファイルをロードしようとしています。最初にログインしてから、ファイルまたはディレクトリリストを取得する必要があります。 https:///xxx.yyy.zzz/login_template

FirefoxのWebサイトソースを見ると、わかります

  <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)
    {    
       ...
    }
 }

私は常に例外system.exception:system.net.webexception:リモートサーバーがエラーを返しました:(501)は実装されていません。 at System.net.httpwebrequest.getResponse()

私はこのエラーについて見つけたことをすべて読んだことがあります - そして、問題は私が得る方法にあるべきだと思われます - それがサーバーで適切に処理されていないという要求に何かがあります - しかし、私は何がわかりません間違っている。

役に立ちましたか?

解決 2

問題はPostdataでした。どうやら、リモートサーバーは「user =」 + username +」&password = " + passを好まなかったようです。 Sting-まさにこの「ユーザー= id&password = pass」と正確に予定されていました。それは意味がありませんが、それは私が言われたことであり、それはうまくいきます。ご提案ありがとうございます。ジェニー

他のヒント

「実装されていない」は、getリクエストにコンテンツタイプを指定しているためです。それはサーバーにとって何も意味するものではありません(主にPOSTリクエスト中に使用され、XMLなどのペイロードを送信する必要があります)。 ZIPファイルを取得していることを確認するには、適切なコンテンツタイプの応答を確認する必要がありますが、リクエストを作成するには、そのcontentType仕様を削除する必要があります。

これがミスタージンブもコメントで指摘していることだと思います。 :)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top