0

I would like to know if I code it correctly. To upload file manually to my workplace server, I have to use Login ID and Password. With the clode below, should I include my loginID and Password as well?

    public void SaveLogsToWeb(string logFileName)
    {
        WebClient webClient = new WebClient();
        string webAddress = null;
        try
        {
            webAddress = @"http://myCompany/ShareDoc/";

            webClient.Credentials = CredentialCache.DefaultCredentials;

            WebRequest serverRequest = WebRequest.Create(webAddress);
            WebResponse serverResponse;
            serverResponse = serverRequest.GetResponse();
            serverResponse.Close();

            webClient.UploadFile(webAddress + logFileName, "PUT", logFileName);
            webClient.Dispose();
            webClient = null;
        }
        catch (Exception error)
        {
            MessageBox.Show(error.Message);
        }
    }

When I run it, exception throws "(401) Unauthorized"

thanks.

1
  • 1
    Is this a web app or a console app? Commented Jan 19, 2010 at 3:59

2 Answers 2

1

You should never include user/password information in a code file. The reason this is throwing up a 401 is because the internet user and the application pool it's running under don't have write permissions to the directory you're attempting to write to.

Right-click on the directory and add /ASPNET and /Network Service as users with write permission. This should clear up the problem. Make sure you isolate the directory.

Here's a good msdn article on it: http://support.microsoft.com/kb/815153

Sign up to request clarification or add additional context in comments.

8 Comments

thanks for the info. I will read when I get home. Again thanks.
After following the provided link, it only mentions about ASPNET, but I'm using C# to upload file to web. Here is the part which I don't get. Before it worked with the code snipped above, but when I execute the same program again, it complains about unauthorized.
Yes, the code snippet above works, but the permission isn't based on the code you have above. It's at the IIS/File system level. 401 is always when the web server does not have permission to perform the action on the local file system. Typically it involves missing "read" access, but in this case you're attempting an actual write so the web process on the server would need new write permissions.
do u know where I can get the new write permissions? Talking to the person who sets up the server or?
Yes, the server administrator would be the person who would need to do this for you. I was under the impression that you controlled both the code and the server.
|
0

Very late maybe but, you can add these 2 lines before webClient.Upload.. to get it fixed:

webClient.Headers["Accept"] = "/";
webClient.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.