简体   繁体   English

使用 java 使用 ssl 肥皂

[英]Consume ssl soap with java

i am new to web services , i have 2 simple java projects(maven) , one is the service and other is the client, i just need to consume this using soap ssl.This is my web service (interface+implementation):我是网络服务的新手,我有 2 个简单的 java 项目(maven),一个是服务,另一个是客户端,我只需要使用soap ssl 来使用它。这是我的网络服务(接口+实现):

@WebService
@SOAPBinding(style = Style.RPC)
public interface CalculService {

@WebMethod
@WebResult
public int test(int val1, int val2);
}
///// impl///
@WebService(endpointInterface = 
 "wstest.CalculService")

public class CalculImpl implements CalculService {

public int test(int val1, int val2) {
    return val1 + val2;
}
}

now after googling for hours i couldnt find something easy to understand, so i found this code below which seemed not complicated :现在在谷歌搜索了几个小时后,我找不到容易理解的东西,所以我发现下面的这段代码看起来并不复杂:

public class ClientSoap {
public static void main(String[] args) throws IOException {
    // Code to make a webservice HTTP request
    String responseString = "";
    String outputString = "";
    String wsEndPoint = "http://localhost:8883/myservice";
    URL url = new URL(wsEndPoint);
    URLConnection connection = url.openConnection();
    HttpURLConnection httpConn = (HttpURLConnection) connection;
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    String xmlInput = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
            + "<soapenv:Header/><soapenv:Body> <generaleRequest xmlns=\"http://wstest/CalculService/\"><arg0>50</arg0>"
            + "</generaleRequest></soapenv:Body></soapenv:Envelope>";
    byte[] buffer = new byte[xmlInput.length()];
    buffer = xmlInput.getBytes();
    bout.write(buffer);
    byte[] b = bout.toByteArray();
    String SOAPAction = "generale";
    httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
    httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
    httpConn.setRequestProperty("SOAPAction", SOAPAction);
    httpConn.setRequestMethod("POST");
    httpConn.setDoOutput(true);
    httpConn.setDoInput(true);
    OutputStream out = httpConn.getOutputStream();
    // Write the content of the request to the outputstream of the HTTP
    // Connection.
    out.write(b);
    out.close();
    // Ready with sending the request.
    // Read the response.
    InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(), Charset.forName("UTF-8"));
    BufferedReader in = new BufferedReader(isr);
    // Write the SOAP message response to a String.
    while ((responseString = in.readLine()) != null) {
        outputString = outputString + responseString;
    }
    // Write the SOAP message formatted to the console.
    String formattedSOAPResponse = formatXML(outputString);
    System.out.println(formattedSOAPResponse);
}

// format the XML in pretty String
private static String formatXML(String unformattedXml) {
    try {
        Document document = parseXmlFile(unformattedXml);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", 3);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(document);
        StreamResult xmlOutput = new StreamResult(new StringWriter());
        transformer.transform(source, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

// parse XML
private static Document parseXmlFile(String in) {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(in));
        return db.parse(is);
    } catch (IOException | ParserConfigurationException | SAXException e) {
        throw new RuntimeException(e);
    }
}
}

Please i didndt know what to put in this 2 lignes: 1-String wsEndPoint = " http://localhost:8883/myservice ";请我不知道在这 2 个 lignes 中放什么: 1-String wsEndPoint = " http://localhost:8883/myservice ";

2- String xmlInput = " 2- 字符串 xmlInput = "

-does every soap i want to consume via ssl i need to use XML? - 我想通过 ssl 消费的每个肥皂都需要使用 XML 吗? please guys excuse my noobism in web services.请大家原谅我在网络服务方面的菜鸟。 or if this code not good , can u guys give me THE SIMPLEST CODE to consume a soap web service via ssl?或者如果这个代码不好,你们能给我最简单的代码来通过 ssl 使用肥皂网络服务吗? for total beginner.适合初学者。 thank you谢谢你

请有人告诉我在 (1-String wsEndPoint=???) 和 (String xmlInput =") 上放什么。或者只是给我最简单的代码来通过 ssl 和 https 使用肥皂

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM