httpclient发送soap请求

本文介绍如何使用Java发起POST请求调用Web服务,并分别展示了不带参数及带参数的请求方式。对于带参数的情况,还涉及了JAXB对象到XML字符串的转换。

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

不带请求参数 的请求方式:

 

	
	public String getDealChannels(){
		try {
			//the webservice url
			String url = HOST+"service/getDealChannels";
			PostMethod pm = new PostMethod(url);
			HttpClient httpclient = new HttpClient();

			HttpClientParams params = new HttpClientParams();

			//set request time out(millisecond)
			params.setSoTimeout(25000);

			httpclient.setParams(params);
			
			//request to webservice, get response code
			int result = httpclient.executeMethod(pm);
			if (result != 200) {
				System.out.println("Error. code: " + result);
				return null;
			}

			//get response
			InputStream xmlStream = pm.getResponseBodyAsStream();
			return getString(xmlStream);
		} catch (Exception e) {
			System.out.println("Exception: " + e.getLocalizedMessage());
			return null;
		}
	
	}

 

请求参数 的请求方式:

 

	
	public String getDealListByVertical(String verticalName){
		try {
			//the webservice url
			String url = HOST+"service/getDealListByVertical";
			PostMethod pm = new PostMethod(url);
			HttpClient httpclient = new HttpClient();

			HttpClientParams params = new HttpClientParams();
			
			//JAXB class
			IpadVerticalRequestJAXB re = new IpadVerticalRequestJAXB();
			re.setVerticalName(verticalName);
			re.setItemsPerPage(10);
			re.setPageNumber(1);
			re.setOrderBy("title");
			re.setIsDesc(true);
			
			//marshall to xml string type
			String request = marshallRequest(re, re.getClass());

			//set request time out(millisecond)
			params.setSoTimeout(25000);

			httpclient.setParams(params);
			
			//set xml String
			RequestEntity reqE = new StringRequestEntity(request);
			pm.setRequestEntity(reqE);
			
			//request to webservice, get response code
			int result = httpclient.executeMethod(pm);
			if (result != 200) {
				System.out.println("Error. code: " + result);
				return null;
			}

			//get response
			InputStream xmlStream = pm.getResponseBodyAsStream();
			return getString(xmlStream);
		} catch (Exception e) {
			System.out.println("Exception: " + e.getLocalizedMessage());
			return null;
		}
	
	}
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值