First, Wximport automatically generate code
Wsimport-keep-p com.test.client http://localhost:8080/test/services/TestService?wsdl
-D: Generate Directory of class files for client execution classes
-S: Generate a directory of source files for the client execution class
-P: Define the package name of the generated class
Second, through Ajax calls (cross-domain calls are not supported)
1 functionCallaxiswspost (method, variable, value, URL, _namespace, callback, loadprocess) {2 functionGetlen (str) {3 varBytescount = 0;4 for(vari = 0; i < str.length; i++) {5 varc =Str.charat (i);6 if(/^[\u0000-\u00ff]$/.test (c))//Match Double byte7 {8Bytescount + = 1;9 }Ten Else { OneBytescount + = 2; A } - } - returnBytescount; the } - - varXMLHTTP =NULL; - if(window. ActiveXObject) { +XMLHTTP =NewActiveXObject ("Microsoft.XMLHTTP"); -}Else if(window. XMLHttpRequest) { +XMLHTTP =NewXMLHttpRequest (); A } at vardata; -data = ' <?xml version= ' 1.0 ' encoding= ' utf-8 '?> '; -data = Data + ' <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/">"; -data = data + ' <soap:Body> '; -data = data + ' < ' + method + ' xmlns= ' + _namespace + ' "> '; - for(vari = 0; i < variable.length; i++) { indata = data + ' < ' + variable[i] + ' > ' + value[i] + ' </' + variable[i] + ' > '; - to } +data = data + ' </' + method + ' > '; -data = data + ' </soap:Body> '; thedata = data + ' </soap:Envelope> '; *Xmlhttp.onreadystatechange =function () { $ if(Xmlhttp.readystate = = 1) {Panax Notoginseng if(loadprocess) - loadprocess (); the } + if(Xmlhttp.readystate = = 4) { A if(Xmlhttp.status = = 200) { the if(callback) + callback (Xmlhttp.responsetext); - } $ } $ } - -xmlHTTP. Open ("POST", URL,true); thexmlHTTP. setRequestHeader ("Content-type", "text/xml; Charset=utf-8 "); -xmlHTTP. setRequestHeader ("Content-length", Getlen (data));WuyixmlHTTP. setRequestHeader ("SOAPAction", _namespace +method); the XMLHTTP. Send (data); -}View Code
Third, call via URL connection
1 private StringBuffer urlconnectionpost (String tourl,stringbuffer data) {2StringBuffer SB =NULL;3BufferedReader reader =NULL;4OutputStreamWriter WR =NULL;5 URL url;6 Try {7URL =NewURL (tourl);8URLConnection conn =url.openconnection ();9Conn.setdooutput (true);TenConn.setconnecttimeout (1000 * 5); One A //Open OutputStreamWriter When there is a value for post - if(data!=NULL&& data.tostring (). Trim (). Length () >0){ -WR =NewOutputStreamWriter (Conn.getoutputstream (), "UTF-8"); the Wr.write (data.tostring ()); - Wr.flush (); - } - + //Get The response -Reader =NewBufferedReader (NewInputStreamReader (Conn +. getInputStream (), "UTF-8")); ASB =NewStringBuffer (); atString line =NULL; - while(line = Reader.readline ())! =NULL) { -Sb.append (line + "/n")); - } -}Catch(IOException e) { - //TODO auto-generated Catch block in e.printstacktrace (); -}finally{ to Try { + if(wr!=NULL){ - wr.close (); the } * if(reader!=NULL){ $ reader.close ();Panax Notoginseng } -}Catch(IOException e) { the //TODO auto-generated Catch block + e.printstacktrace (); A } the } + returnsb; -}View Code
Iv. calling through HttpClient
Reprint: http://blog.csdn.net/bhq2010/article/details/9210007
Browser-based test tools (restclient)
Component add-on address: https://addons.mozilla.org/zh-CN/firefox/addon/restclient/
Java Call WebService