Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. upload files

upload files

Scheduled Pinned Locked Moved C#
sysadminhelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steven M Hunt
    wrote on last edited by
    #1

    Hi. I am trying to upload and download files to and from my web server. I Keep getting a 405 with WebClient. I understand the problem is that i don't have access. I tried to send a username password with WebClient.Credentials, but it still doesn't work. Is there any way to get permission from a web server to upload a file and then upload it? -- Steve

    H 1 Reply Last reply
    0
    • S Steven M Hunt

      Hi. I am trying to upload and download files to and from my web server. I Keep getting a 405 with WebClient. I understand the problem is that i don't have access. I tried to send a username password with WebClient.Credentials, but it still doesn't work. Is there any way to get permission from a web server to upload a file and then upload it? -- Steve

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Yes - by using the Credentials property and assigning an ICredentials implementation. If you're using CredentialCache.DefaultCredentials, then it probably won't work unless you're connecting to a site that requires NTLM authentication with your domain account (which is about all that would be in the DefaultCredentialss). Instead, create a new instance of the CredentialCache to hold multiple credentials, or just a NetworkCredential to hold a single credential. If you look at the class documentation for the CredentialCache class in the .NET Framework SDK (installed by default with VS.NET), it even gives an example of using both classes mentioned above. For the former, you instantiate a new NetworkCredential and add it to a CredentialCache associated with a given Uri and specifying the authentication type ("Digest", "NTLM", "Kerberos", etc.). If you wanted to download a file from www.domain.com with username:password, you'd do something like this:

      WebClient client = new WebClient();
      CredentialCache cache = new CredentialCache();
      cache.Add(new Uri("http://www.domain.com/"), "Basic",
      new NetworkCredential("username", "password", "DOMAIN")));
      client.Credentials = cache;
      client.DownloadFile("http://www.domain.com/file.txt", "file.txt");

      Read the documentation for NetworkCredential to see what password schemes are supported. You can add additional schemes, if necessary, by implementing the IAuthenticationModule, also - of course - documented in the .NET Framework SDK.

      Microsoft MVP, Visual C# My Articles

      S 1 Reply Last reply
      0
      • H Heath Stewart

        Yes - by using the Credentials property and assigning an ICredentials implementation. If you're using CredentialCache.DefaultCredentials, then it probably won't work unless you're connecting to a site that requires NTLM authentication with your domain account (which is about all that would be in the DefaultCredentialss). Instead, create a new instance of the CredentialCache to hold multiple credentials, or just a NetworkCredential to hold a single credential. If you look at the class documentation for the CredentialCache class in the .NET Framework SDK (installed by default with VS.NET), it even gives an example of using both classes mentioned above. For the former, you instantiate a new NetworkCredential and add it to a CredentialCache associated with a given Uri and specifying the authentication type ("Digest", "NTLM", "Kerberos", etc.). If you wanted to download a file from www.domain.com with username:password, you'd do something like this:

        WebClient client = new WebClient();
        CredentialCache cache = new CredentialCache();
        cache.Add(new Uri("http://www.domain.com/"), "Basic",
        new NetworkCredential("username", "password", "DOMAIN")));
        client.Credentials = cache;
        client.DownloadFile("http://www.domain.com/file.txt", "file.txt");

        Read the documentation for NetworkCredential to see what password schemes are supported. You can add additional schemes, if necessary, by implementing the IAuthenticationModule, also - of course - documented in the .NET Framework SDK.

        Microsoft MVP, Visual C# My Articles

        S Offline
        S Offline
        Steven M Hunt
        wrote on last edited by
        #3

        Hey. I'm still having problems with uploading. I am using a linux web server. do you think that is causing the trouble? If so, what can i do to get it working? -- Steve

        H 1 Reply Last reply
        0
        • S Steven M Hunt

          Hey. I'm still having problems with uploading. I am using a linux web server. do you think that is causing the trouble? If so, what can i do to get it working? -- Steve

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          You're code uses the HTTP protocol, which is a specification that is independent of the host OS; so, no, it doesn't matter that you're running linux on the server. If you're having problems, please be specific. The code I gave you is a sample that you should research further, like reading the method and property documentation in the .NET Framework SDK if you don't understand a particular line of code. You should also read - which I mentioned - which authentication protocols are supported. If your web server (presumably Apache) is uses an unsupported authentication mechanism, then you're have to implement the IAuthenticationModule interface and roll your own authentication module. With specifics regarding your problem, I really can't help you.

          Microsoft MVP, Visual C# My Articles

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups