Java调用webService服务


利用SoapClient调用webService

引入:

	<dependency>
		<groupId>cn.hutool</groupId>
		<artifactId>hutool-all</artifactId>
		<version>4.6.4</version>
	</dependency>

代码,利用了builder设计模式

 SoapClient client = 
                 SoapClient.create("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx")
                // 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间
                .setMethod("web:getCountryCityByIp", "http://WebXml.com.cn/")
                // 设置参数,此处自动添加方法的前缀:web
                .setParam("theIpAddress", "218.21.240.106");
                //多个参数进行多次setParam
        System.out.println(client.send(true));

运行结果
在这里插入图片描述

利用post请求调用

pom文件引入

		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.6</version>
		</dependency>
	private final static String url="http://192.168.100.97/zb_yy/WebServiceAppointmentInspection.asmx/CommonInterfaceMessageRegister";
    private final static String Host="http://192.169.100.97";
    private final static String namespace="http://www.tphy.com.cn/CommonInterfaceMessageRegister";
public static String getWebServiceData(String url,String params) {
        String res = "";
        logger.info("host:"+Host+"---"+namespace+"---"+url+"----"+params);
        try {// 发送正常的请求(获取资源)
            URL u = new URL(url);
            URLConnection conn = (URLConnection) u.openConnection();
            conn.setDoOutput(true); // 必须设置这两个请求属性为true,就表示默认使用POST发送
            conn.setDoInput(true);
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Charset", "utf-8");
            // 设置文件类型:   表单提交的 key-value形式
            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
            // 设置接收类型否则返回415错误
            //conn.setRequestProperty("accept","*/*")此处为暴力方法设置接受所有类型,以此来防范返回415;


            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //conn.setRequestProperty("charsert", "utf-8");
            // 请求参数必须使用conn获取的OutputStream输出到请求体参数
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
            out.write(params);
            out.flush(); // 立即充刷至请求体)PrintWriter默认先写在内存缓存中
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                res += line + "\n";
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
        }
        return res;
    }

调用的时候

try {
            str=getWebServiceData(url,"参数名="+s);
            System.out.println(str);
        } catch (IOException e) {
            e.printStackTrace();
        }

例如在这里,参数名就是qqCode,值就是input里的值
s为值

后续

最近和对方联调webservixe接口,一直卡在了Connect out,连接超时,或者return URL 500
总结下原因:
在这里插入图片描述
这几个标红的必须注意,不能是错误的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值