Java webservice 带 get 参数地址调用

该代码示例展示了如何在Java中使用JaxWsDynamicClientFactory创建并配置Web服务客户端,进行带参数的调用。设置了连接超时时间(CONN_TIMEOUT)和接收超时时间(RECV_TIMEOUT),并使用HTTPConduit配置了保持长连接的策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

private Integer CONN_TIMEOUT = 10 * 1000; // 连接超时时间ms
private Integer RECV_TIMEOUT = 20 * 1000; // 接收超时时间ms
// 命名空间及调用方法名称
QName qName = new QName("http://WebXml.com.cn/", "getCountryCityByIp");
// 参数列表,多个参数逗号隔开
Object[] parameters = { theIpAddress };
// webservice 带参地址
Stirng url = "http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?param1=xxx&param2=xxx";


public static String wsInvoke(String url, QName qName, Object[] params) {
    String res = "";
    JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
    try {
        String wsdl = url.substring(0,url.indexOf("?"));
        // 使用 wsdl 地址生成客户端
        Client client = factory.createClient(wsdl + "?wsdl");
        // 修改客户端调用地址使调用带参数
        client.getEndpoint().getEndpointInfo().setAddress(url);
        HTTPConduit conduit = (HTTPConduit) client.getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setConnectionTimeout(CONN_TIMEOUT);
        policy.setReceiveTimeout(RECV_TIMEOUT);
        // ConnectionType.KEEP_ALIVE 保持长连接
        // ConnectionType.CLOSE 请求处理完成关闭连接
        policy.setConnection(ConnectionType.KEEP_ALIVE);
        conduit.setClient(policy);
        Object[] objects = client.invoke(qName, params);
        res = objects[0].toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return res;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值