HttpClient访问WebService

用不同的技术发布的Web服务,在访问的时候拼接soap消息的方式也不同,我使用SCA发布web服务。

/**
	 * 访问服务
	 * @param wsdl wsdl地址
	 * @param ns 命名空间
	 * @param method 方法名
	 * @param params 参数
	 * @return
	 * @throws Exception
	 */
	public synchronized static String accessService(String wsdl,String ns,String method,Map<String,String> params,String result)throws Exception{
		//拼接参数
		String param = getParam(params);
		String soapResponseData = "";
		//拼接SOAP
		StringBuffer soapRequestData = new StringBuffer("");
		soapRequestData.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
		soapRequestData.append("<soap:Body>");
		soapRequestData.append("<ns1:"+method+" xmlns:ns1=\""+ns+"\">");
		soapRequestData.append(param);
		soapRequestData.append("</ns1:"+method+">");
		soapRequestData.append("</soap:Body>" + "</soap:Envelope>");
		PostMethod postMethod = new PostMethod(wsdl);
		// 然后把Soap请求数据添加到PostMethod中
		byte[] b=null;
		InputStream is=null;
		try {
			b = soapRequestData.toString().getBytes("utf-8"); 
			is = new ByteArrayInputStream(b, 0, b.length);
			RequestEntity re = new InputStreamRequestEntity(is, b.length,"text/xml; charset=UTF-8");
			postMethod.setRequestEntity(re);
			HttpClient httpClient = new HttpClient();
			int status = httpClient.executeMethod(postMethod);
			System.out.println("status:"+status);
			if(status==200){
				soapResponseData = getMesage(postMethod.getResponseBodyAsString(),result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(is!=null){
				is.close();
			}
		}
		return soapResponseData;
	}
	
	public static String getParam(Map<String,String> params){
		String param = "";
		if(params!=null){
			Iterator<String> it  = params.keySet().iterator();
			while(it.hasNext()){
				String str = it.next();
				param+="<"+str+">";
				param+=params.get(str);
				param+="</"+str+">";
			}
		}
		return param;
	}
	
	public static String getMesage(String soapAttachment,String result){
		System.out.println("response:"+soapAttachment);
		if(result==null){
			return null;
		}
		if(soapAttachment!=null && soapAttachment.length()>0){
			int begin = soapAttachment.indexOf(result);
			begin = soapAttachment.indexOf(">", begin);
			int end = soapAttachment.indexOf("</"+result+">");
			String str = soapAttachment.substring(begin+1, end);
			str = str.replaceAll("<", "<");
			str = str.replaceAll(">", ">");
			return str;
		}else{
			return "";
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) { 
		try {
			Map<String,String> param = new HashMap<String,String>();
			param.put("path", "aaa");
			param.put("admission_pwd", "bbb");
			String wsdl="http://127.0.0.1:8080/passport/uniVerifyService";
			String ns = "http://service.verify.passport.com/";
			String method="uniVerify";
			String response =accessService(wsdl,ns,method,param,"result");
			System.out.println("response:"+response);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值