HttpClient 调用方法


调用方:

public String getHttpClientInfo(String url){
		String ret = "";
		try {
			HttpClient httpclient = new HttpClient();
			//httpclient.setConnectionTimeout(10000);
			PostMethod method = new PostMethod(url);
			method.setDoAuthentication(true);   
			method.setRequestHeader("Content-Type", "text/html;charset=UTF-8");  
			httpclient.executeMethod(method);
			
			if (method.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				ret = new String(method.getResponseBodyAsString());
			}
			method.releaseConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}




被调用方——(spring mvc 架构):


@RequestMapping(value="/queryList", produces="application/json;charset=utf-8") 
    public @ResponseBody Object queryList(String code,String type) {
		Map<String,Object> returnMap = new LinkedHashMap<String, Object>();
		//查询
		List<CaseHandle> list=gridService.queryCommunityList(null, type, code, "1011002", true);
		returnMap.put("status", Status.success);
		returnMap.put("relist", list);
		return returnMap;
    }





  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: HttpClient是一个开源的HTTP客户端库,可以用来发送HTTP请求和接收HTTP响应。我们可以使用HttpClient调用Web服务。 调用Web服务的步骤如下: 1. 创建HttpClient对象 2. 创建HttpPost对象,并设置请求的URL和请求参数 3. 创建HttpEntity对象,并设置请求参数 4. 将HttpEntity对象设置到HttpPost对象中 5. 执行HttpPost请求,并获取响应结果 6. 解析响应结果 示例代码如下: ``` import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; public class HttpClientWebServiceDemo { public static void main(String[] args) throws Exception { // 创建HttpClient对象 HttpClient httpClient = HttpClientBuilder.create().build(); // 创建HttpPost对象,并设置请求的URL和请求参数 HttpPost httpPost = new HttpPost("http://localhost:8080/webservice"); // 创建HttpEntity对象,并设置请求参数 StringEntity stringEntity = new StringEntity("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.demo.com/\"><soapenv:Header/><soapenv:Body><ser:sayHello><arg0>world</arg0></ser:sayHello></soapenv:Body></soapenv:Envelope>", "UTF-8"); // 将HttpEntity对象设置到HttpPost对象中 httpPost.setEntity(stringEntity); // 执行HttpPost请求,并获取响应结果 HttpResponse httpResponse = httpClient.execute(httpPost); // 解析响应结果 HttpEntity httpEntity = httpResponse.getEntity(); String responseContent = EntityUtils.toString(httpEntity, "UTF-8"); System.out.println(responseContent); } } ``` 以上示例代码是使用HttpClient调用一个名为sayHello的Web服务,并传递一个参数world。在实际使用中,需要根据具体的Web服务接口和参数进行调整。 ### 回答2: Httpclient是一个用于发送HTTP请求的Java开源库,可以用于调用WebService。调用WebService需要遵循WebService的协议,一般是SOAP(Simple Object Access Protocol)协议。 使用Httpclient调用WebService可以按照以下步骤进行: 1. 创建HttpClient对象,可以使用如下代码: HttpClient httpClient = new DefaultHttpClient(); 2. 创建HttpPost对象,设置WebService的URL地址,可以使用如下代码: HttpPost httpPost = new HttpPost("WebService的URL地址"); 3. 创建SOAP请求的SOAP消息,设置请求的SOAP消息格式,可以使用如下代码: String soapMessage = "SOAP请求消息"; StringEntity entity = new StringEntity(soapMessage, "UTF-8"); httpPost.setEntity(entity); 4. 设置请求的SOAP消息头部,可以使用如下代码: httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8"); httpPost.setHeader("SOAPAction", "WebService的SOAPAction"); 5. 发送HttpPost请求,并获取响应结果,可以使用如下代码: HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); InputStream inputStream = httpEntity.getContent(); 6. 解析响应结果,可以使用如下代码: BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuilder stringBuilder = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line); } String soapResponse = stringBuilder.toString(); 7. 关闭相关资源,可以使用如下代码: inputStream.close(); bufferedReader.close(); httpClient.getConnectionManager().shutdown(); 通过以上步骤,就可以使用Httpclient调用WebService,并获取WebService的响应结果。当然,在实际的调用过程中,还需要根据具体的WebService定义和需求进行相应的处理和解析。 ### 回答3: HttpClient是一个开源的Java HTTP客户端库,可以用于发送HTTP请求和处理HTTP响应。通过HttpClient库,我们可以方便地调用WebService。 调用WebService可以分为以下几个步骤: 1. 创建HttpClient对象:使用HttpClientBuilder类创建一个HttpClient对象,用于发送请求和接收响应。 2. 创建HttpPost对象:使用HttpPost类创建一个HttpPost对象,用于封装HTTP POST请求。 3. 设置请求地址和参数:使用setEntity方法设置请求的地址和参数。请求地址为WebService的URL,参数可以通过NameValuePair等类封装为HttpEntity对象。 4. 发送请求并获取响应:通过HttpClient对象的execute方法发送请求,返回HttpResponse对象。通过HttpResponse对象可以获取响应的状态码、响应头和响应体等信息。 5. 解析响应结果:根据WebService返回的数据格式,使用相应的方法解析响应结果。常用的方法有将响应体转换为字符串、获取响应头信息等。 6. 关闭HttpClient:使用HttpClient对象的close方法关闭连接,释放资源。 在调用WebService时,还需要注意以下事项: - 设置请求头:根据WebService的要求,可能需要设置特定的请求头,如SOAPAction。 - 验证身份:如果WebService需要验证身份,可以通过设置HttpClient的CredentialsProvider对象进行身份认证。 - 处理异常:在调用WebService时,可能会遇到各种异常情况,如网络连接异常、请求超时等,在代码中需要进行相应的异常处理。 总之,通过HttpClient调用WebService可以方便地实现与应用程序间的数据交互,提高开发效率和灵活性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值