The main difference between using urlconnection to call WebService and Ajax to call WebService is that the former uses Java to construct HTTP requests,
The latter uses the JS language to construct HTTP requests. The functions of these two methods are actually the same, but the implementation forms are different ····················
1. Server SideCodeYou can refer to using JDK to call WebService
2. The client code is as follows:
App
Package COM. njupt. webService. urlconnection; import Java. io. inputstream; import Java. io. outputstream; import java.net. httpurlconnection; import java.net. URL; public class app {public static void main (string [] ARGs) throws exception {URL wsurl = new URL ("http: // 127.0.0.1: 6790/Hello "); httpurlconnection conn = (httpurlconnection) wsurl. openconnection (); Conn. setdoinput (true); // Input Conn. setdooutput (true); // output Conn. setrequestmethod ("Post"); Conn. setrequestproperty ("Content-Type", "text/XML; charset = UTF-8"); // to use double quotation marks in double quotation marks, you can use the Escape Character \ to escape double quotes string soap = "<soapenv: envelope xmlns: soapenv = \" character "xmlns: q0 = \" http://webservice.njupt.com/\ "xmlns: XSD = \ "http://www.w3.org/2001/xmlschema\" xmlns: xsi = \ "external"> "+" <soapenv: Body> <q0: sayhello> <ARG 0> Zhang Zetian is my goddess </arg0> </q0: sayhello> </soapenv: Body> </soapenv: envelope> "; outputstream OS = Conn. getoutputstream (); OS. write (soap. getbytes (); inputstream is = Conn. getinputstream (); byte [] B = new byte [1024]; int Len = 0; string S = ""; while (LEN = is. read (B ))! =-1) {S + = new string (B, 0, Len, "UTF-8");} system. out. println (s); OS. close (); is. close (); Conn. disconnect ();}}