Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
Subir un archivo al servidor WebDAV usando WebClient.UploadFile devuelve 401

Intento cargar archivos desde mi aplicación C# en un cliente de Windows a un servidor web Linux a través de WebDAV. Para lograr esto, estoy usando WebClient.UploadFile() . La carga siempre arroja una excepción:

El servidor remoto devolvió un error: (401) No autorizado

Probé dos métodos diferentes de autorización, ambos con el mismo resultado.

Autorización:

 // Method A myWebClient.Credentials = new NetworkCredential("user123", "pass123"); // Method B var bytes = Encoding.UTF8.GetBytes(String.Format("{0}:{1}", "user123", "pass123")); string auth = Convert.ToBase64String(bytes); // "Basic aW1hZ2VfdXBsb2FkOkozMkUzYnVGWlB1S0tkQ2tQRkpV" string authString = String.Format("Basic {0}", auth); myWebClient.Headers.Add("Authorization", authString);

Para asegurarme de que WebDAV funciona según lo previsto, conecté y cargué archivos usando WinSCP (GUI de Windows) y cadaver (terminal de Linux), ambos con éxito .


Código de carga del archivo:

 try { string address = "http://123.124.125.126/webdav/1"; string fileName = "F:\\articleimages\\isotope\\1\\1dmgo.jpg"; byte[] response = myWebClient.UploadFile(address, fileName); } catch (WebException wexc) { // wexc.Message == The remote server returned an error: (401) Unauthorized }

Preguntas

  • ¿Mi código es correcto hasta ahora?
  • ¿Qué autorización se espera que use? NetworkCredentials o Header ?

Dado que el concepto solo falla en mi aplicación C#, el problema debe estar ahí.

  • ¿Cómo solucionaría más problemas?

La respuesta real (detalles de la excepción)

 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>401 Unauthorized</title> </head><body> <h1>Unauthorized</h1> <p>This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (eg, bad password), or your browser doesn't understand how to supply the credentials required.</p> </body></html>
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

WebClient es un cliente HTTP, no un cliente WebDAV.

Si hace WebClient.UploadFile , de forma predeterminada utiliza la solicitud HTTP POST , no la solicitud WebDAV PUT .

La falla de autenticación puede ser solo un efecto secundario de eso. E incluso si no, y resuelves el problema de autenticación, probablemente no te ayudaría.

No sé si puede ayudar, pero intente usar una sobrecarga que tome el argumento del method y use "PUT" .


Otra opción, ya que WinSCP funciona para usted, es usar el ensamblado WinSCP .NET .

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!