locked
HTTPWebRequest PUT/POST Error : Method not allowed RRS feed

  • Question

  • Hi ,

    I'm trying to upload a file to a remote server using HTTPWebRequest (both PUT and POST) (We are using asp.net 1.1, there's no FTPWebrequest in 1.1 and Webclient doesn't work either). But when I run the code, it gives me this error:

    The remote server returned an error: (405) Method Not Allowed

    Below is the code I'm using. Please let me know what am I doing wrong here.

     try
                {
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"http://FolderPathASURL/");
                    req.Credentials = new NetworkCredential(@"username", "password");
                    req.Method = "PUT"; //Tried POST as well
                    req.AllowWriteStreamBuffering = true;
                    // Retrieve request stream 
                    Stream reqStream = req.GetRequestStream();
                    // Open the local file
                    FileStream rdr = new FileStream(@"C:\TestFile.txt", FileMode.Open);
                    // Allocate byte buffer to hold file contents
                    byte[] inData = new byte[4096];
    
                    // loop through the local file reading each data block
                    //  and writing to the request stream buffer
                    int bytesRead = rdr.Read(inData, 0, inData.Length);
                    while (bytesRead > 0)
                    {
                        reqStream.Write(inData, 0, bytesRead);
                        bytesRead = rdr.Read(inData, 0, inData.Length);
                    }
    
                    rdr.Close();
                    reqStream.Close();
                    req.GetResponse();
                }
                catch (System.Net.WebException we)
                {
                    Console.WriteLine(we.Message);
    
                    if (we.Status == WebExceptionStatus.ProtocolError)
                        Console.WriteLine(we.Response.ResponseUri);
                    Console.ReadLine();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                }




    Abhi

    Thursday, June 25, 2015 1:39 PM

Answers

  • You really need to check the server side.  It's the one returning the error code.  Ensure that it has code that can accept the PUT/POST request.

    Darin R.

    • Marked as answer by Fred Bao Tuesday, July 7, 2015 2:01 AM
    Thursday, June 25, 2015 4:47 PM
  • Hello Abhi,

    >>I'm doing this in my Winforms app (VS 2003).

    Then please check if the server side allows a post request as for example, in a WCF, we need to specify the Method to Post in the webinvoke attribute:

    [WebInvoke(Method = "POST", ..

    Or it will show the exception you encounter.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Tuesday, July 7, 2015 2:01 AM
    Tuesday, June 30, 2015 10:15 AM

All replies

  • You really need to check the server side.  It's the one returning the error code.  Ensure that it has code that can accept the PUT/POST request.

    Darin R.

    • Marked as answer by Fred Bao Tuesday, July 7, 2015 2:01 AM
    Thursday, June 25, 2015 4:47 PM
  • Hello Abhi,

    For issues related with asp.net project, please post it to http://forums.asp.net/. which discusses about web development issue specifically.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, June 26, 2015 2:59 AM
  • Hi Fred,

    I'm doing this in my Winforms app (VS 2003).


    Abhi

    Friday, June 26, 2015 12:01 PM
  • Hello Abhi,

    >>I'm doing this in my Winforms app (VS 2003).

    Then please check if the server side allows a post request as for example, in a WCF, we need to specify the Method to Post in the webinvoke attribute:

    [WebInvoke(Method = "POST", ..

    Or it will show the exception you encounter.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Tuesday, July 7, 2015 2:01 AM
    Tuesday, June 30, 2015 10:15 AM