Java calls webservice to implement telephone referer query.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.