本文以 http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
为例,这是一个公共的查询手机号码归属地的webservice接口 有兴趣也可以自己发布一个webservice接口玩玩
- 正常soap(完整xml报文)方式调用 顾名思义就是通过正常的xml报文调用接口
- 必须设置connection.setRequestProperty("content-type", "text/xml;charset=utf-8") 字符集看具体
- connection.setDoInput(true), connection.setDoOutput(true)必须设置
- connection.setRequestMethod似乎可以不设置
- 构建xml报文的外部格式基本都一样,xmlns:web即为wsdl的头部的namespace
public static void main(String[] args) throws IOException {
//第一步:创建服务地址
URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);
//第四步:组织SOAP数据,发送请求
String soapXML = getXML("18373133976");
//将信息以流的方式发送出去
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
if (200 == responseCode) {//表示服务端响应成功
//获取当前连接请求返回的数据流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String temp = null;
while (null != (temp = br.readLine())) {
sb.append(temp);
}
/**
* 打印结果
*/
System.out.println(sb.toString());
is.close();
isr.close();
br.close();
}
//记得关闭连接
os.close();
}
public static String getXML(String phone) {
String soapXML = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://WebXml.com.cn/\">\n" +
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <web:getMobileCodeInfo>\n" +
" <!--Optional:-->\n" +
" <web:mobileCode>" + phone + "</web:mobileCode>\n" +
" <!--Optional:-->\n" +
" <web:userID></web:userID>\n" +
" </web:getMobileCodeInfo>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>";
return soapXML;
}
- 返回
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
<getMobileCodeInfoResult>18373133976:湖南 长沙 湖南移动全球通卡</getMobileCodeInfoResult>
</getMobileCodeInfoResponse>
</soap:Body>
</soap:Envelope>
- 正常http-GET方式调用接口
- connection.setDoInput(true), connection.setDoOutput(true)有时候需要设置
- 如果参数有json数据,例如包含双引号这些,需要先把参数encode,然后拼在url后面 例如
String xx = URLEncoder.encode("{\"data\": {\"appointmentBeginDate\": \"2019-01-01\",\"appointmentEndDate\": \"2019-02-01 23:59:59\",\"cutoverType\": [1, 3],\"affectedNetwork\": [1, 2]}}", "UTF-8");
URL url = new URL("http://127.0.0.1:3389/ws/cutover.asmx/Query?requestJson="+xx);
public static void main(String[] args) throws IOException {
//第一步:创建服务地址
URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=13643086903&userID=");
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
if (200 == responseCode) {//表示服务端响应成功
//获取当前连接请求返回的数据流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String temp = null;
while (null != (temp = br.readLine())) {
sb.append(temp);
}
/**
* 打印结果
*/
System.out.println(sb.toString());
is.close();
isr.close();
br.close();
}
}
- 返回
<?xml version="1.0" encoding="utf-8"?><string xmlns="http://WebXml.com.cn/">13643086903:广东 梅州 广东移动神州行卡</string>
- 正常http-POST方式调用接口
- 只需要设置输入输出
- 如果参数有json数据,好像也要转义,可以自己去试试,这个接口没有json等特殊数据
public static void main(String[] args) throws IOException {
//第一步:创建服务地址
URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);
//第四步:组织SOAP数据,发送请求
String soapXML = getXML("18373133976");
//将信息以流的方式发送出去
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
if (200 == responseCode) {//表示服务端响应成功
//获取当前连接请求返回的数据流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String temp = null;
while (null != (temp = br.readLine())) {
sb.append(temp);
}
/**
* 打印结果
*/
System.out.println(sb.toString());
is.close();
isr.close();
br.close();
}
os.close();
}
public static String getXML(String phone) {
return "mobileCode=18373133976&userID=";
}
- 返回
<?xml version="1.0" encoding="utf-8"?><string xmlns="http://WebXml.com.cn/">18373133976:湖南 长沙 湖南移动全球通卡</string>