Java調(diào)用Web service接口SOAP協(xié)議HTTP請(qǐng)求,解析返回的XML字符串:
1. 使用Java的HTTP庫(kù)發(fā)送SOAP請(qǐng)求,并接收返回的響應(yīng)。
可以使用Java的HttpURLConnection、Apache HttpClient等庫(kù)。
2. 將返回的響應(yīng)轉(zhuǎn)換為字符串。
3. 解析XML字符串,可以使用Java的DOM解析器或者其他第三方庫(kù),如JDOM、DOM4J等。
4. 解析XML數(shù)據(jù),提取需要的信息。
參考代碼如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class SOAPClient {
? ? public static void main(String[] args) {
? ? ? ? try {
? ? ? ? ? ? // 創(chuàng)建SOAP請(qǐng)求的XML數(shù)據(jù)
? ? ? ? ? ? String soapRequest = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://www.demo.com\">\n"
? ? ? ? ? ? ? ? ? ? + " ? <soapenv:Header/>\n"
? ? ? ? ? ? ? ? ? ? + " ? <soapenv:Body>\n"
? ? ? ? ? ? ? ? ? ? + " ? ? ?<web:YourMethodName>\n"
? ? ? ? ? ? ? ? ? ? + " ? ? ? ? <web:Parameter1Name>parameter1Value</web:Parameter1Name>\n"
? ? ? ? ? ? ? ? ? ? + " ? ? ?</web:YourMethodName>\n"
? ? ? ? ? ? ? ? ? ? + " ? </soapenv:Body>\n"
? ? ? ? ? ? ? ? ? ? + "</soapenv:Envelope>";
? ? ? ? ? ? // 發(fā)送SOAP請(qǐng)求,并接收返回的響應(yīng)
? ? ? ? ? ? String endpoint = "http://localhost:8080/demo_webservice"; // Webservice的URL
? ? ? ? ? ? HttpURLConnection connection = (HttpURLConnection) new URL(endpoint).openConnection();
? ? ? ? ? ? connection.setRequestMethod("POST");
? ? ? ? ? ? connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
? ? ? ? ? ? connection.setRequestProperty("SOAPAction", "http://www.demo.com/demoMethodName"); // SOAPAction必須指定
? ? ? ? ? ? connection.setDoOutput(true);
? ? ? ? ? ??
? ? ? ? ? ? OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
? ? ? ? ? ? osw.write(soapRequest);
? ? ? ? ? ? osw.flush();
? ? ? ? ? ??
? ? ? ? ? ? BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
? ? ? ? ? ? StringBuilder sb = new StringBuilder();
? ? ? ? ? ? String line;
? ? ? ? ? ? while ((line = br.readLine()) != null) {
? ? ? ? ? ? ? ? sb.append(line);
? ? ? ? ? ? }
? ? ? ? ? ? br.close();
? ? ? ? ? ? connection.disconnect();
? ? ? ? ? ??
? ? ? ? ? ? String soapResponse = sb.toString();
? ? ? ? ? ? // 解析XML字符串
? ? ? ? ? ? DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
? ? ? ? ? ? Document document = documentBuilder.parse(soapResponse);
? ? ? ? ? ??
? ? ? ? ? ? // 提取需要的信息
? ? ? ? ? ? // . . .
? ? ? ? } catch (IOException | ParserConfigurationException | SAXException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}
通過(guò)上述示例代碼,使用Java的HTTP庫(kù)發(fā)送SOAP請(qǐng)求,并接收返回的響應(yīng),然后將返回的響應(yīng)
轉(zhuǎn)換為字符串。
接下來(lái),可以使用Java的DOM解析器或其他第三方庫(kù)解析XML字符串,提取需要的信息。
需要將示例代碼中的http://localhost:8080/demo_webservice替換為實(shí)際的Web service的文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-805781.html
URL,并將SOAP請(qǐng)求的XML數(shù)據(jù)、SOAPAction、以及需要提取的信息進(jìn)行相應(yīng)的替換。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-805781.html
到了這里,關(guān)于Java調(diào)用WebService接口,SOAP協(xié)議HTTP請(qǐng)求返回XML對(duì)象的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!