Code Preparation:
1. There are some free server test address available on the network, you can find it here: https://my.oschina.net/CraneHe/blog/183471
2. I chose a translation address: Http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx
2.1 After opening, you see a method under this address:
2.2 Click Enter, the Web site will provide the method's client request XML format:
2.3, this red box part is what we want, write it to the code, you can complete the request.
Note: The above is also the method to obtain the SOAP request XML, is also a way to get started, the experience of the package directly on the WSDL using the interpretation of the document can also write their own XML ...
Then the code, I directly attached to the code, you can directly copy to run, with comments.
Translate.class
ImportJava.io.ByteArrayOutputStream;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL;/*** Created by Garfield on 2016/10/16.*/ Public classTranslate { Public Static voidTranslate (String word)throwsException {//AddressString urlstring = "Http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx"; //MethodString soapactionstring = "Http://WebXml.com.cn/getEnCnTwoWayTranslator"; URL URL=NewURL (urlstring); HttpURLConnection Httpconn=(HttpURLConnection) url.openconnection (); //splicing request body, here can also write XML file outside and then read, but in order to facilitate a file, and the parameters are better to pass our direct string splicing (directly on the page to copy in it)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" + "<geten Cntwowaytranslator xmlns=\ "http://webxml.com.cn/\" >\n "+" <Word> "+ Word +" </word& gt;\n "+" </getencntwowaytranslator>\n "+" </soap:body>\n "+ "</soap:Envelope>"; byte[] buf =soap.getbytes (); //set some header parametersHttpconn.setrequestproperty ("Content-length", String.valueof (buf.length)); Httpconn.setrequestproperty ("Content-type", "text/xml; Charset=utf-8 "); Httpconn.setrequestproperty ("Soapactionstring", soapactionstring); Httpconn.setrequestmethod ("POST"); //input parameters and output resultsHttpconn.setdooutput (true); Httpconn.setdoinput (true); OutputStream out=Httpconn.getoutputstream (); Out.write (BUF); Out.close (); //finally qualified analytic result everybody recount, here prints the analytic process, finally obtains the translation answer byte[] Datas =Readinputstream (Httpconn.getinputstream ()); String result=NewString (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)?>", "")); } /*** Read data from the input stream * *@paraminstream *@return * @throwsException*/ Public Static byte[] Readinputstream (InputStream instream)throwsException {bytearrayoutputstream OutStream=NewBytearrayoutputstream (); byte[] buffer =New byte[1024]; intLen = 0; while(len = instream.read (buffer))! =-1) {outstream.write (buffer,0, Len); } byte[] data =Outstream.tobytearray (); Outstream.close (); Instream.close (); returndata; } Public Static voidMain (string[] args)throwsException {Translate ("Sea"); }
Operation Result:
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. Sea, Ocean |</string> </getencntwowaytranslatorresult></getencntwowaytranslatorresponse></soap:body></soap: Envelope><string>sea: [si:]</string><string>n. Sea, Ocean |</string>|
The first line is the result of the direct return, the following two lines to help understand the interpretation, and finally get the sea word explanation, is not simple clear ...
The most straightforward SOAP request for WebService clients