WebException Method not allowed

G

Guest

Hello,
I am trying to use WebClient class's UploadData method for uploading
data to a particular uri. I ahve created a text file say new.txt in the
IISROOT folder. But when I try to upload data to this file using uri
http://localhost/new.txt it gives following exception

Uploading to http://localhost/new.txt ...
The remote server returned an error: (405) Method Not Allowed. at
System.Net.
HttpWebRequest.CheckFinalStatus()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.HttpWebRequest.GetResponse()
at System.Net.WebClient.UploadData(String address, String method, Byte[]
data
)
at HTTPPostConsole.Class1.Main(String[] args) in
f:\httppostconsole\class1.cs
:line 33

My code is

string uriString;
Console.Write("\nPlease enter the URI to post data to {for example,
http://www.contoso.com} : ");
uriString = Console.ReadLine();
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
Console.WriteLine("\nPlease enter the data to be posted to the URI
{0}:",uriString);
string postData = Console.ReadLine();
myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");

// Apply ASCII Encoding to obtain the string as a byte array.
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
Console.WriteLine("Uploading to {0} ...", uriString);

// Upload the input string using the HTTP 1.0 POST method.
byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);

Ths same code works fine if I give uri as www.contoso.com.

How can I solve this problem.
 
M

mick

Are you sure that your webserver security settings are configured
correctly?

"405 errors often arise with the POST method. You may be trying to
introduce some kind of input form on your Web site, but not all ISPs
allow the POST method necessary to process the form.

All 405 errors can be traced to configuration of the Web server and
security governing access to the content of the Web site, so should
easily be explained by your ISP."

From: http://www.checkupdown.com/status/E405.html
 
J

Joerg Jooss

sushi said:
Hello,
I am trying to use WebClient class's UploadData method for
uploading data to a particular uri. I ahve created a text file say
new.txt in the IISROOT folder. But when I try to upload data to this
file using uri http://localhost/new.txt it gives following exception

Uploading to http://localhost/new.txt ...
The remote server returned an error: (405) Method Not Allowed. at
System.Net.
HttpWebRequest.CheckFinalStatus()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult
asyncResult) at System.Net.HttpWebRequest.GetResponse()
at System.Net.WebClient.UploadData(String address, String method,
Byte[] data
)
at HTTPPostConsole.Class1.Main(String[] args) in
f:\httppostconsole\class1.cs
:line 33

My code is

string uriString;
Console.Write("\nPlease enter the URI to post data to {for example,
http://www.contoso.com} : ");
uriString = Console.ReadLine();
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
Console.WriteLine("\nPlease enter the data to be posted to the URI
{0}:",uriString);
string postData = Console.ReadLine();
myWebClient.Headers.Add("Content-Type","application/x-www-form-urlenco
ded");

// Apply ASCII Encoding to obtain the string as a byte array.
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
Console.WriteLine("Uploading to {0} ...", uriString);

// Upload the input string using the HTTP 1.0 POST method.
byte[] responseArray =
myWebClient.UploadData(uriString,"POST",byteArray);

Ths same code works fine if I give uri as www.contoso.com.

How can I solve this problem.

First, use UploadValues() to post form data. UploadData() is meant to
be sued for "raw" posts like posting binaries (i.e. uploads).

Second, what are you trying to achieve? Are you trying to upload a file
instead? You cannot post values to some arbitrary file -- there must be
some application at the backend processing a POST.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top