405方法不允许POST,但是允许

时间:2018-06-08 15:17:22

标签: java http soap soapui soap-client

我正在尝试将POST发送到某个服务。在请求体内部有一个XML。 我试图通过SOAP UI来实现它 - 它的工作原理 - 只需将正文数据放入XML文本中。通过Jmeter - 它的工作原理。通过我自己的java代码,它返回405 - 方法不允许。 如果只是将方法更改为GET,我在Jmeter或SOAP UI中获得405。

所以这是我的代码,POST方法肯定是在哪里设置的。我不知道出了什么问题。

public static void main(String args[]) {
    String soapEndpointUrl = "https://some.com/test";
    callSoapBankService(soapEndpointUrl);
}

private static void callSoapBankService(String soapEndpointUrl) {
    try {

        HttpsURLConnection httpsConnection = null;
        // Open HTTPS connection
        URL url = new URL(soapEndpointUrl);
        httpsConnection = (HttpsURLConnection) url.openConnection();
        // Connect
        httpsConnection.setRequestMethod("POST");
        httpsConnection.setDoInput(true);
        httpsConnection.setDoOutput(true);
        httpsConnection.setRequestProperty("SOAPAction", "");
        httpsConnection.setRequestProperty("Connection", "keep-alive");
        httpsConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpsConnection.setRequestProperty("Content-Length", "397");
        httpsConnection.setRequestProperty("Host", "some.com");
        httpsConnection.setRequestProperty("User-Agent", "Apache-HttpClient/4.1.1 (java 1.5)");
        DataOutputStream output = new DataOutputStream(httpsConnection.getOutputStream());
            output.writeBytes("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://some.com/\">\n" +
                    "                <SOAP-ENV:Header/>\n" +
                    "                <SOAP-ENV:Body>\n" +
                    "                    <ws:getDocument>\n" +
                    "                        <beginDate>2018-02-09</beginDate>\n" +
                    "                        <endDate>2018-02-10</endDate>\n" +
                    "                    </ws:getDocument>\n" +
                    "                </SOAP-ENV:Body>\n" +
                    "             </SOAP-ENV:Envelope>");
         output.flush();
         output.close();
         DataInputStream input = new DataInputStream(httpsConnection.getInputStream());
         for( int c = input.read(); c != -1; c = input.read() )
         System.out.print( (char)c );
         input.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
   }

1 个答案:

答案 0 :(得分:0)

解决了。问题出现在这一行的最后:

String soapEndpointUrl =&#34; https://some.com/test&#34;;

网址中应该有斜杠