'The remote server returned an error: (405) Method Not Allowed service request and "Request method 'POST' not supported"

I have service client application written in C# where I create a service request as a soap XML as following:

  var _url = "http://localhost:8082/tartsend";
  var _action = "http://rep.oio.no/getTest";
  StartSend startSend = new tartSend();
        Envelope soapEnvelopeXml = StartSend.StartSendMethod();

        HttpWebRequest webRequest = CreateWebRequest(_url, _action);
        InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

        
        IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

        asyncResult.AsyncWaitHandle.WaitOne();

        string soapResult;
        using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
        {
            using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
            {
                soapResult = rd.ReadToEnd();
            }
            Console.Write(soapResult);
        }
    }
  
    private static XmlDocument CreateSoapEnvelope()
    {

        XmlDocument soapEnvelopeDocument = new XmlDocument();
        return soapEnvelopeDocument;
    }

    private static HttpWebRequest CreateWebRequest(string url, string action)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Headers.Add("SOAPAction", action);
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        return webRequest;
    }

    private static void InsertSoapEnvelopeIntoWebRequest(Envelope soapEnvelopeXml, 
           HttpWebRequest webRequest)
      {
       
        var xmlString = string.Empty;

        XmlSerializer xmlSerializer = new XmlSerializer(typeof(Envelope));
        var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + 
        "//Sendservice.xml";
        System.IO.FileStream file = System.IO.File.Create(path);

        using (StringWriter textWriter = new StringWriter())
        {
            xmlSerializer.Serialize(file, soapEnvelopeXml);
            xmlString = textWriter.ToString();

        }
        Console.WriteLine(xmlString);

    }

Envelop is the class object that contains the Body, Header, and the other objects/ attributes. I serialize it in "InsertSoapEnvelopeIntoWebRequest" method and send it to the service side.

the server side is a simple method that suppose to send OK as response and nothing else. The server side service code is in jave spring boot as following:-

@SpringBootApplication
@EnableWebMvc
@RestController
public class TestController {


@RequestMapping(value = "/getslutsend", method=RequestMethod.GET,
        consumes={MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_XML_VALUE, 
MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_PLAIN_VALUE})
public ResponseEntity<?> readFromtest(@RequestBody String test) {

    return new ResponseEntity<String>(HttpStatus.OK);
}


    public static void main(String[] args) {

    SpringApplication.run(TestController.class, args);

 }
}

I'm not sure what I'm doing wrong to get the above errors mentioned in the title of this post. I get " (405) Method Not Allowed" and Request method not supported.



Solution 1:[1]

You have the RequestMethod.GET in your request mapping and the @RequestBody in parameter annotation. Normally, you should avoid this combination, please use either RequestMethod.POST with @RequestBody, or RequestMethod.GET without the @RequestBody.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user488399