upload files
-
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
-
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
Yes - by using the
Credentials
property and assigning anICredentials
implementation. If you're usingCredentialCache.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 theDefaultCredentials
s). Instead, create a new instance of theCredentialCache
to hold multiple credentials, or just aNetworkCredential
to hold a single credential. If you look at the class documentation for theCredentialCache
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 newNetworkCredential
and add it to aCredentialCache
associated with a givenUri
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 theIAuthenticationModule
, also - of course - documented in the .NET Framework SDK.Microsoft MVP, Visual C# My Articles
-
Yes - by using the
Credentials
property and assigning anICredentials
implementation. If you're usingCredentialCache.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 theDefaultCredentials
s). Instead, create a new instance of theCredentialCache
to hold multiple credentials, or just aNetworkCredential
to hold a single credential. If you look at the class documentation for theCredentialCache
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 newNetworkCredential
and add it to aCredentialCache
associated with a givenUri
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 theIAuthenticationModule
, also - of course - documented in the .NET Framework SDK.Microsoft MVP, Visual C# My Articles
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
-
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
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