Search code examples
c#.netasmxwebclientwebclient.uploaddata

Upload text file to web service


I have following method which accepts a text file and I am trying to upload this text file on the web service. I use a user name and a password. But I get an exception: "The remote server returned an error: (404) Not Found.". If I supply the user name and password again I get the same exception. What should I do to overcome this issue?

public static void UploadTextFileToWebService(string txtFile)
{
    WebClient webClient = new WebClient();
    string webAddress = null;
    try
    {
        webAddress = @"https://www.myweb.org/mywebwebservices/dataupload.asmx";
        webClient.Credentials = CredentialCache.DefaultCredentials;

        WebRequest serverRequest = WebRequest.Create(webAddress);
        WebResponse serverResponse;
        serverResponse = serverRequest.GetResponse();
        serverResponse.Close();              
        webClient.UploadFile(webAddress + txtFile, "PUT", txtFile);
        webClient.Dispose();
        webClient = null;
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

Solution

  • Your webservice seems to be an asmx webservice so i doubt you can do the upload the way you are trying to.

    You need to use the correct SoapMessage format to send whatever requests you are sending.

    Since you are using C#/.Net your easiest path would be to add a service reference which would create a proxy to enable you send your request via an object model.