標籤:
代碼準備:
1.網路上有提供一些免費的伺服器測試地址,可以上這裡找一找:https://my.oschina.net/CraneHe/blog/183471
2.我選擇了一個翻譯地址:http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx
2.1開啟之後看到該地址下有一個方法:
2.2點擊進入,網站會提供該方法的用戶端請求xml格式:
2.3,這個紅框部分就是我們要的,將它寫入代碼,就可以完成請求了.
注意:以上還是擷取soap請求xml的方法,也是比較入門的方式,有經驗的筒子直接上wsdl利用解釋文檔也可以自己寫xml...
然後是代碼,我直接附上代碼,大家直接複製即可運行,附註釋.
Translate.class
import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;/** * Created by garfield on 2016/10/16. */public class Translate { public static void translate(String word ) throws Exception { //地址 String urlString = "http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx "; //方法 String soapActionString = "http://WebXml.com.cn/getEnCnTwoWayTranslator"; URL url = new URL(urlString); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); //拼接請求體,此處也可以在外面寫xml檔案然後讀取,但是為了方便一個檔案搞定,而且參數也比較好傳遞我們直接String拼接(直接將網頁上的複製進來即可) String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" + " <soap:Body>\n" + " <getEnCnTwoWayTranslator xmlns=\"http://WebXml.com.cn/\">\n" + " <Word>" + word + "</Word>\n" + " </getEnCnTwoWayTranslator>\n" + " </soap:Body>\n" + "</soap:Envelope>"; byte[] buf = soap.getBytes(); //設定一些頭參數 httpConn.setRequestProperty("Content-Length", String.valueOf(buf.length)); httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); httpConn.setRequestProperty("soapActionString", soapActionString); httpConn.setRequestMethod("POST"); //輸入參數和輸出結果 httpConn.setDoOutput(true); httpConn.setDoInput(true); OutputStream out = httpConn.getOutputStream(); out.write(buf); out.close(); //最後合格解析結果大家就各顯神通了,此處列印出解析的過程,最終得到翻譯答案 byte[] datas = readInputStream(httpConn.getInputStream()); String result = new String(datas); System.out.println("result:" + result); System.out.println(result.substring(result.indexOf("string") - 1,result.lastIndexOf("string") + 7)); System.out.println(result.substring(result.indexOf("string") - 1,result.lastIndexOf("string") + 7).replaceAll("</{0,1}(string)?>","")); } /** * 從輸入資料流中讀取資料 * * @param inStream * @return * @throws Exception */ public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.close(); inStream.close(); return data; } public static void main(String[] args) throws Exception { translate("sea"); }
運行結果:
result:<?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><getEnCnTwoWayTranslatorResponse xmlns="http://WebXml.com.cn/"><getEnCnTwoWayTranslatorResult><string>sea: [ si: ]</string><string>n. 海,海洋 |</string></getEnCnTwoWayTranslatorResult></getEnCnTwoWayTranslatorResponse></soap:Body></soap:Envelope><string>sea: [ si: ]</string><string>n. 海,海洋 |</string>sea: [ si: ]n. 海,海洋 |
第一行是直接返回的結果,下面兩行協助理解解析,最後得到sea單詞的解釋,是不是簡單清楚...
最簡單易懂的webService用戶端之soap請求