java后台调用webservice接口示例

在网上查阅了很多关于java后台调用webservice接口的例子,但在我项目中一直无法通过,后来还是通过拼接请求报文才解决,如下是具体代码:

public static void main(String[] args) throws IOException {
		String url ="http://localhost:8080/HE_USD_Server_App/services/PhoneInterfaceService";
		URL wsUrl = new URL(url);
		HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setRequestMethod("POST");
		conn.setConnectTimeout(5*1000);
		conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
		conn.setRequestProperty("SOAPAction",  
                url);
		String req =" <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns0=\"http://commonInterface.bpm.ultrapower.com\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
		req+="<soapenv:Body>";
		req+="<ns0:USDLoginService>";
		req+="<arg0>";
		req+="<![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?><opDetail>";
		req+="<userLoginName>5002</userLoginName>";
		req+="<passWord>68CF63C62BC68D71FC41C028375E2F6E</passWord>";
		req+="<appType>android</appType>";
		req+="</opDetail>]]>";
		req+="</arg0>";
		req+="</ns0:USDLoginService>";
		req+="</soapenv:Body> </soapenv:Envelope>";
        OutputStream os = conn.getOutputStream();
		os.write(req.getBytes());
		InputStream is = conn.getInputStream();
		byte[] b = new byte[1024];
		int len = 0;
		String s = "";
		while((len = is.read(b)) != -1){
			String ss = new String(b,0,len,"UTF-8");
			s += ss;
		}
		is.close();
		os.close();
		conn.disconnect();
		System.out.println("服务器返回数据:"+s);
	}

ps:
USDLoginService 为接口方法名称


  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值