有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何使用Java使用RPCEN编码的SOAP Web服务

有没有一种方法可以使用java使用SOAP web服务,只需使用:

  • 所需的SOAPaction(例如名为“find”的methodname)
  • web服务的URL
  • 标头身份验证(用户名和密码)
  • 最后输出结果

我有一个用php成功使用的示例请求xml文件,但我找不到在java上使用它的正确方法

[更新:web服务的WSDL样式为RPC/编码]

[更新#2:您可以找到我是如何解决以下问题的(通过使用IDE生成的java存根)


共 (2) 个答案

  1. # 1 楼答案

    经过长时间的搜索,我终于找到了一种使用rpc/编码的SOAPweb服务的方法。 我决定从wsdl url生成客户端存根

    一个成功的方法是通过这个链接(来源:What is the easiest way to generate a Java client from an RPC encoded WSDL

    在eclipse/netbeans调整生成的代码(java存根)之后,您只需构建客户端。 通过使用您生成的类,您可以使用首选的soap api

    例如

    Auth auth = new Auth("username", "password");
    SearchQuery fsq = new SearchQuery ("param1","param2","param3");
    Model_SearchService service = new Model_SearchServiceLoc();
    SearchRequest freq = new SearchRequest(auth, fsq);
    Result r[] = service.getSearchPort().method(freq);
    for(int i=0; i<r.length; i++){
        System.out.println(i+" "+r[i].getInfo()[0].toString());
    }
    
  2. # 2 楼答案

    可以使用^{}发送SOAP消息。e、 g:

    public static void main(String[] args) throws Exception {
    
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n" +
                "  <soap:Body>\r\n" +
                "    <ConversionRate xmlns=\"http://www.webserviceX.NET/\">\r\n" +
                "      <FromCurrency>USD</FromCurrency>\r\n" +
                "      <ToCurrency>CNY</ToCurrency>\r\n" +
                "    </ConversionRate>\r\n" +
                "  </soap:Body>\r\n" +
                "</soap:Envelope>";
    
        Authenticator.setDefault(new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("username", "password".toCharArray());
            }
        });
    
        URL url = new URL("http://www.webservicex.net/CurrencyConvertor.asmx");
        URLConnection  conn =  url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        conn.setRequestProperty("SOAPAction", "http://www.webserviceX.NET/ConversionRate");
    
        // Send the request XML
        OutputStream outputStream = conn.getOutputStream();
        outputStream.write(xml.getBytes());
        outputStream.close();
    
        // Read the response XML
        InputStream inputStream = conn.getInputStream();
        Scanner sc = new Scanner(inputStream, "UTF-8");
        sc.useDelimiter("\\A");
        if (sc.hasNext()) {
            System.out.print(sc.next());
        }
        sc.close();
        inputStream.close();
    
    }