views:

1016

answers:

2

Hi,

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.

A: 

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

Joel Etherton
thanks for the info. I will read when I get home. Again thanks.
Bopha
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.
Bopha
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.
Joel Etherton
do u know where I can get the new write permissions? Talking to the person who sets up the server or?
Bopha
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.
Joel Etherton
No I don't control the server. I'm in a testing team trying to save log file to the company test project site programmatically. I can upload file manually after logging into the company site.
Bopha
Thanks Joel Etherton for your time. I will try to talk to the IT people since they are the one who setting the server.
Bopha
Solution:Just found that my URL is not correct because it should not include for example, http://sharedoc/mycompany.com/SystemTest/TestPass/, so instead it should be http://sharedoc/SystemTest/TestPass/ By eliminating mycompany.com, it allows me to upload.
Bopha
A: 

When I include this code line serverRequest.Credentials = CredentialCache.DefaultCredentials; Now it complains error (403) Forbidden.

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); 
        serverRequest.Credentials = CredentialCache.DefaultCredentials;
        WebResponse serverResponse; 
        serverResponse = serverRequest.GetResponse(); 
        serverResponse.Close(); 

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