WebRequest get genera un'eccezione “Il server remoto ha restituito un errore: (501) non implementato”.

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

Domanda

Sto cercando di caricare un file utilizzando il metodo WebRequest. Prima ho bisogno di login quindi ottenere un file o un elenco di directory. https: ///xxx.yyy.zzz/login_template

Quando guardo la fonte sito web in Firefox, vedo

  <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">

Così, ho scritto questo codice:

  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();
          }

La risposta che ottengo è OK - così sembra che mi sono collegato con successo in. Ma poi, ho bisogno di andare in un altro URL per ottenere un 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)
    {    
       ...
    }
 }

Ho sempre ottenere uno System.Exception eccezione: System.Net.WebException: il server remoto ha restituito un errore: (501) non implementato. a System.Net.HttpWebRequest.GetResponse ()

Ho letto tutto quello che potevo trovare su questo errore - e sembra che il problema dovrebbe essere nel mio modo di fare arrivare - c'è qualcosa nella richiesta che non è gestita correttamente sul server - ma io don' t vedere ciò che è sbagliato.

È stato utile?

Soluzione 2

Il problema era in postData. A quanto pare, il server remoto non ha come "user =" + username + "& password =" + passare; pungono - si aspettava esattamente questo pungiglione "user = ID e Password = Pass". Il che non ha senso, ma è quello che mi è stato detto e funziona. Grazie corpo per i vostri suggerimenti. Jenny

Altri suggerimenti

Il "non implementato" è perché si sta specificando un ContentType a una richiesta GET. Non significa niente per il server (Viene usato principalmente nel corso di una richiesta POST e si desidera inviare un carico utile ad esempio XML). È necessario controllare la risposta per il tipo di contenuto giusto per assicurarsi che si stanno ottenendo un file zip, ma per effettuare la richiesta, è necessario rimuovere tale specifica ContentType.

Credo che questo è ciò che sta puntando MisterZimbu nel commento pure. :)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top