Reputation: 479
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);
}
}
Upvotes: 1
Views: 2184
Reputation: 737
parm 1 is wrong in this line:
webClient.UploadFile(webAddress + txtFile, "PUT", txtFile);
and probably should be
webClient.UploadFile(webAddress + @"/" + txtFile, "PUT", txtFile);
Upvotes: 0
Reputation: 41589
I'm not sure you've got the approach right here - since you've already read the file as a string, you should just send the content of the string to a webmethod that accepts the contents of the text file directly.
So in your service you should have (something like):
[WebMethod()]
public void AcceptFile(string content)
{
...
}
You then call that method and pass the txtFile variable as the parameter.
Upvotes: 0
Reputation: 17680
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.
Upvotes: 1