java调用xml接口
时间: 2023-12-09 13:05:49 浏览: 36
Java可以通过HTTP或SOAP协议调用XML接口。
1. HTTP调用XML接口
首先,需要准备好XML请求数据,然后使用Java的HttpURLConnection类发送HTTP POST请求,将XML数据作为请求体发送给XML接口:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class HttpXmlClient {
public static void main(String[] args) {
try {
// 准备请求数据
String xml = "<request><param1>value1</param1><param2>value2</param2></request>";
// 发送HTTP POST请求
URL url = new URL("http://example.com/xmlapi");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/xml");
conn.setDoOutput(true);
conn.getOutputStream().write(xml.getBytes());
// 获取响应数据
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String response = "";
String line;
while ((line = in.readLine()) != null) {
response += line;
}
in.close();
// 处理响应数据
System.out.println(response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
2. SOAP调用XML接口
如果XML接口是基于SOAP协议实现的,可以使用Java的JAX-WS API调用:
```java
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import java.net.URL;
public class SoapXmlClient {
public static void main(String[] args) {
try {
// 创建SOAP消息
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
// 添加请求数据
QName qName = new QName("http://example.com/soapapi", "request");
SOAPElement request = body.addChildElement(qName);
QName param1 = new QName("param1");
request.addChildElement(param1).addTextNode("value1");
QName param2 = new QName("param2");
request.addChildElement(param2).addTextNode("value2");
// 创建SOAP连接
URL url = new URL("http://example.com/soapapi?wsdl");
QName serviceName = new QName("http://example.com/soapapi", "Service");
QName portName = new QName("http://example.com/soapapi", "Port");
Service service = Service.create(url, serviceName);
Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
// 发送SOAP消息并接收响应
SOAPMessage response = dispatch.invoke(message);
// 处理响应数据
SOAPPart responsePart = response.getSOAPPart();
SOAPEnvelope responseEnvelope = responsePart.getEnvelope();
SOAPBody responseBody = responseEnvelope.getBody();
String responseXml = responseBody.getFirstChild().toString();
System.out.println(responseXml);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
以上两种方法可以根据具体的XML接口实现进行调整。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)