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

  • Question

  • User-770252936 posted

    Hello

    I'm posting a json file to a service.

    It asks me to send it via POST method, and I send it via post.

    But: The remote server returned an error: (405) I get a Method Not Allowed error.

    My codes are as follows;

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    
    NameValueCollection nvc = new NameValueCollection();
    string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
    byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
    
    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(pUrl);
    wr.Headers.Add("Authorization", "Bearer " + pToken);
    wr.Accept = "application/json";
    wr.ContentType = "multipart/form-data; boundary=" + boundary;
    wr.Method = "POST";
    
    Stream rs = wr.GetRequestStream();
    
    string formdataTemplate = "Content-Dis-data; name=\"{0}\"\r\n\r\n{1}";
    foreach (string key in nvc.Keys)
    {
        rs.Write(boundarybytes, 0, boundarybytes.Length);
        string formitem = string.Format(formdataTemplate, key, nvc[key]);
        byte[] formitembytes = Encoding.UTF8.GetBytes(formitem);
        rs.Write(formitembytes, 0, formitembytes.Length);
    }
    rs.Write(boundarybytes, 0, boundarybytes.Length);
    
    string headerTemplate = "Content-Dis-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
    string header = string.Format(headerTemplate, "file", pPath, "application/json");
    byte[] headerbytes = Encoding.UTF8.GetBytes(header);
    rs.Write(headerbytes, 0, headerbytes.Length);
    
    FileStream fileStream = new FileStream(pPath, FileMode.Open, FileAccess.Read);
    byte[] buffer = new byte[4096];
    int bytesRead = 0;
    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
    {
        rs.Write(buffer, 0, bytesRead);
    }
    fileStream.Close();
    
    byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
    rs.Write(trailer, 0, trailer.Length);
    rs.Close();

    Wednesday, April 7, 2021 6:24 PM

All replies

  • User-1330468790 posted

    Hi kortmazsefa,

     

    We need to firstly understand what 405 means. 

    405 Method Not Allowed response status code indicates that the request method is known by the server but is not supported by the target resource. In another words, it confirms that the requested resource is valid and existsbut the client has used an unacceptable HTTP method during the request

     

    I guess you are trying to access a 3rd party web api for some services. You can follow below steps to solve the 405 error.

    1. Check the api documentation and compare the example with yours to see if there is anything missing.
    2. Check the response from the http request and check the detailed error message.

    For example, you could set breakpoint and check the content of the response.

    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(pUrl);
    .........omit the rest of codes .............
    HttpWebResponse response = (HttpWebResponse)wr.GetResponse();
    // Get the stream associated with the response.
    Stream receiveStream = response.GetResponseStream ();
    // Pipes the stream to a higher level stream reader with the required encoding format.
    StreamReader readStream =
    new StreamReader (receiveStream, Encoding.UTF8);
    // readStream.ReadToEnd() will show the response content

     

    If you have 100% followed the documentation correctly, I think the problem might be related to the token. However, this is just a problem troubleshooting direction.

    You could share this web api documentation link for us so that I am able to try from my side.

     

    Best regards,

    Sean

    Thursday, April 8, 2021 6:41 AM