我正试着发邮件给某个服务机构。请求正文中有一个xml。
我试着通过soap ui来实现它-它可以工作-只需将xml文本放在体数据中。通过jmeter-它起作用了。通过我自己的Java代码,它返回405-method not allowed。
我在jmeter或soap ui中得到405,只要我将方法更改为get。
这是我的代码,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();
    }
   }

最佳答案

解决了。问题就在这一行的末尾:
字符串soapendpointurl=“https://some.com/test”;
URL中应该有斜线

关于java - 405方法不允许POST,但是允许,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50763731/

10-13 05:36