WebService之访问Soap服务

一、Soap服务获取WebService接口数据

方法1:使用wsimport进行soap服务进行调用
使用JDK自带的wsimport工具
1,使用cmd控制台将位置切换到项目目录的src下

wsimport -keep http://localhost:8080/hello?wsdl

执行完毕后刷新项目可以看见自动生成的项目,删除.class执行文件,在项目中刷新便可以看见对应的Java文件
2,新建一个方法进行调用

package hello;

import com.theservice.service.Hello;
import com.theservice.service.HelloService;

public class Test {
//Hello与HelloService为wsimport自动生成的
	public static void main(String[] args) {
		Hello hello=new HelloService().getHelloPort();
		System.out.println(hello.hello());
	}
}

方法2.使用URLConnection访问Scop服务
调用此方法可以直接返回
urlName为发布的地址
responseinfo使用SOAPUI进行获取,新建项目名称,填写wsdl地址
在这里插入图片描述
箭头所指为responseinfo
在这里插入图片描述

package com.TheClient.sys.util;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class SoapUtil {

	public static String getinfoByScop(String urlPath,String requestBody) throws Exception {
        URL url=new URL(urlPath);
        HttpURLConnection connection=(HttpURLConnection) url.openConnection();
        String address=requestBody;
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
        
        OutputStream outputStream=connection.getOutputStream();
        outputStream.write(address.getBytes());
        int code=connection.getResponseCode();
        StringBuffer buffer=new StringBuffer();
        if (code==200) {
			InputStream inputStream=connection.getInputStream();
			byte[] bs=new byte[1024];
			Integer len=0;
			while ((len=inputStream.read(bs))!=-1) {
				buffer.append(new String(bs,0,len,"utf-8"));
			}
        	inputStream.close();
		}
        outputStream.close();
        return buffer.toString();
    }
	
}

方法3,待续

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值