Most of the time we need the webservice interface, how do we connect to the interface?
In fact, the simple function is implemented.
The code is as follows: |
Copy code |
Package com. air. controller; Import java. util. Vector; Import javax. servlet. http. HttpServletRequest; Import javax. xml. namespace. QName; Import javax. xml. rpc. ParameterMode; Import javax. xml. rpc. encoding. XMLType; Import org. apache. axis. client. Call; Import org. apache. axis. client. Service; Import org. springframework. stereotype. Controller; Import org. springframework. web. bind. annotation. RequestMapping; @ Controller @ RequestMapping () Public class GetCountryCityByIp { // Provide an IP address to search for the URL of the WEB service Private String url = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; // open the url in the browser, you can find // Private String soapAction = "http://WebXml.com.cn/getCountryCityByIp" Private String namespace = "http://WebXml.com.cn/"; // targetNamespace Private String actionURI = "getMobileCodeInfo"; // Action path Private String op = "getMobileCodeInfo"; // method name to be called @ RequestMapping ("/cityip.html ") Public String GetCountryCityByIp (HttpServletRequest request ){ Service service = new Service (); Try { Call call = (Call) service. createCall (); Call. setTargetEndpointAddress (new java.net. URL (url )); Call. setUseSOAPAction (true ); // Action uri Call. setSOAPActionURI (namespace + actionURI ); // Set the method to call Call. setOperationName (new QName (namespace, op )); // Set the parameter name. For details, refer to Call. addParameter (new QName (namespace, "mobileCode "), XMLType. XSD_STRING, ParameterMode. IN); // data type to be returned Call. setReturnType (new QName (namespace, op), String. class); // input parameter: corresponding to theIpAddress Object [] params = new Object [] {"18035147802"}; // call a method and pass parameters String v = (String) call. invoke (params ); Request. setAttribute ("aaa", v ); } Catch (Exception ex ){ Ex. printStackTrace (); } Return "test "; } } |
This is relatively simple, that is, to pass the mobile phone number, and then the other website will return the relevant json or xml data, and then we will know the relevant information.