本文以 http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
為例,這是一個公共的查詢手機(jī)號碼歸屬地的webservice接口 有興趣也可以自己發(fā)布一個webservice接口玩玩
- 正常soap(完整xml報(bào)文)方式調(diào)用 顧名思義就是通過正常的xml報(bào)文調(diào)用接口
- 必須設(shè)置connection.setRequestProperty("content-type", "text/xml;charset=utf-8") 字符集看具體
- connection.setDoInput(true)蓖乘, connection.setDoOutput(true)必須設(shè)置
- connection.setRequestMethod似乎可以不設(shè)置
- 構(gòu)建xml報(bào)文的外部格式基本都一樣,xmlns:web即為wsdl的頭部的namespace
public static void main(String[] args) throws IOException {
//第一步:創(chuàng)建服務(wù)地址
URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
//第二步:打開一個通向服務(wù)地址的連接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:設(shè)置參數(shù)
//3.1發(fā)送方式設(shè)置:POST必須大寫
connection.setRequestMethod("POST");
//3.2設(shè)置數(shù)據(jù)格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3設(shè)置輸入輸出桂肌,因?yàn)槟J(rèn)新創(chuàng)建的connection沒有讀寫權(quán)限剪决,
connection.setDoInput(true);
connection.setDoOutput(true);
//第四步:組織SOAP數(shù)據(jù),發(fā)送請求
String soapXML = getXML("18373133976");
//將信息以流的方式發(fā)送出去
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服務(wù)端響應(yīng)胯舷,打印
int responseCode = connection.getResponseCode();
if (200 == responseCode) {//表示服務(wù)端響應(yīng)成功
//獲取當(dāng)前連接請求返回的數(shù)據(jù)流
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);
}
/**
* 打印結(jié)果
*/
System.out.println(sb.toString());
is.close();
isr.close();
br.close();
}
//記得關(guān)閉連接
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方式調(diào)用接口
- connection.setDoInput(true)完慧, connection.setDoOutput(true)有時候需要設(shè)置
- 如果參數(shù)有json數(shù)據(jù),例如包含雙引號這些剩失,需要先把參數(shù)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 {
//第一步:創(chuàng)建服務(wù)地址
URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=13643086903&userID=");
//第二步:打開一個通向服務(wù)地址的連接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:接收服務(wù)端響應(yīng),打印
int responseCode = connection.getResponseCode();
if (200 == responseCode) {//表示服務(wù)端響應(yīng)成功
//獲取當(dāng)前連接請求返回的數(shù)據(jù)流
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);
}
/**
* 打印結(jié)果
*/
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方式調(diào)用接口
- 只需要設(shè)置輸入輸出
- 如果參數(shù)有json數(shù)據(jù)拴孤,好像也要轉(zhuǎn)義脾歧,可以自己去試試,這個接口沒有json等特殊數(shù)據(jù)
public static void main(String[] args) throws IOException {
//第一步:創(chuàng)建服務(wù)地址
URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
//第二步:打開一個通向服務(wù)地址的連接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//3.3設(shè)置輸入輸出演熟,因?yàn)槟J(rèn)新創(chuàng)建的connection沒有讀寫權(quán)限鞭执,
connection.setDoInput(true);
connection.setDoOutput(true);
//第四步:組織SOAP數(shù)據(jù),發(fā)送請求
String soapXML = getXML("18373133976");
//將信息以流的方式發(fā)送出去
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服務(wù)端響應(yīng)芒粹,打印
int responseCode = connection.getResponseCode();
if (200 == responseCode) {//表示服務(wù)端響應(yīng)成功
//獲取當(dāng)前連接請求返回的數(shù)據(jù)流
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);
}
/**
* 打印結(jié)果
*/
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>