gpt4 book ai didi

java - 使用java使用ssl SOAP

转载 作者:行者123 更新时间:2023-12-01 18:42:48 25 4
gpt4 key购买 nike

我是Web服务新手,我有2个简单的java项目(maven),一个是服务,另一个是客户端,我只需要使用soap ssl来使用它。这是我的Web服务(接口(interface)+实现) :

@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;
}
}

现在,经过几个小时的谷歌搜索后,我找不到一些容易理解的东西,所以我发现下面的代码看起来并不复杂:

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);
}
}
}

拜托,我不知道该在这 2 法币中放入什么:1-字符串 wsEndPoint = "http://localhost:8883/myservice ";

2- 字符串 xmlInput = "

-我想通过 ssl 使用的每个 SOAP 都需要使用 XML 吗?请大家原谅我对网络服务的不懂。或者如果这个代码不好,你们能给我最简单的代码来通过 ssl 使用 SOAP 网络服务吗?对于完全初学者。谢谢

最佳答案

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

关于java - 使用java使用ssl SOAP ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59888673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com