调用webservice接口_Java调用webservice实现电话归属地查询为例

更新时间:2019-07-01    来源:WebService    手机版     字体:

【www.bbyears.com--WebService】

很多时候我们需要的数据来着webservice接口,那么如何对接接口呢?

其实很简单的功能就实现了

 代码如下

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 {
    // 提供IP地址搜索WEB服务的URL
    private String url = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; // 在浏览器中打开url,可以找到
    // private String soapAction="http://WebXml.com.cn/getCountryCityByIp"
    private String namespace = "http://WebXml.com.cn/";// targetNamespace
    private String actionURI = "getMobileCodeInfo"; // Action路径
    private String op = "getMobileCodeInfo"; // 要调用的方法名
 
    
    @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);
            // 设置要调用哪个方法
            call.setOperationName(new QName(namespace, op));
            // 设置参数名称,具体参照从浏览器中看到的
            call.addParameter(new QName(namespace, "mobileCode"),
                    XMLType.XSD_STRING, ParameterMode.IN); // 要返回的数据类型
            call.setReturnType(new QName(namespace, op), String.class); // 入参:对应theIpAddress
            Object[] params = new Object[]{"18035147802"}; // 调用方法并传递参数
            String v = (String) call.invoke(params);
            request.setAttribute("aaa", v);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        return "test";
    }
}

这个是相对比较简单的了就是把手机号传过去,然后由对方网站返回相关的json 或者xml数据,然后我们就知道相关信息了。

本文来源:http://www.bbyears.com/asp/56093.html

热门标签

更多>>

本类排行