WebException Method not allowed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sushi

    WebException Method not allowed

    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. CheckFinalStatu s()
    at System.Net.Http WebRequest.EndG etResponse(IAsy ncResult asyncResult)
    at System.Net.Http WebRequest.GetR esponse()
    at System.Net.WebC lient.UploadDat a(String address, String method, Byte[]
    data
    )
    at HTTPPostConsole .Class1.Main(St ring[] args) in
    f:\httppostcons ole\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.ReadLin e();
    // Create a new WebClient instance.
    WebClient myWebClient = new WebClient();
    Console.WriteLi ne("\nPlease enter the data to be posted to the URI
    {0}:",uriString );
    string postData = Console.ReadLin e();
    myWebClient.Hea ders.Add("Conte nt-Type","applicat ion/x-www-form-urlencoded");

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

    // Upload the input string using the HTTP 1.0 POST method.
    byte[] responseArray = myWebClient.Upl oadData(uriStri ng,"POST",byteA rray);

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

    How can I solve this problem.
Working...