HttpClient3.1 Get与Post提交

Get提交:

public static String invokeUrl(String url, String param,String charset) {
		if(StringUtils.isBlank(charset)){
			charset="UTF-8";
		}
		HttpClient httpClient = new HttpClient();
		PostMethod post = null;
		String resp = null;
		try {

			String invokeUrl = url+(StringUtils.isBlank(param)?"":("?"
					+ URLEncoder.encode(URLEncoder.encode(param, "UTF-8"),
					"UTF-8")));

			// 设置超时时间
			int connectionTime = 20*1000;
			httpClient.getHttpConnectionManager().getParams()
					.setConnectionTimeout(connectionTime);
			httpClient.getHttpConnectionManager().getParams()
					.setSoTimeout(connectionTime);

			post = new PostMethod(invokeUrl);
			post.setRequestHeader("Content-type", "text/html; charset="+charset);
			// 使用系统提供的默认的恢复策略
			post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
					new DefaultHttpMethodRetryHandler());

			// 执行getMethod
			int statusCode = httpClient.executeMethod(post);
			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed:" + post.getStatusLine());
			}
			// 读取内容
			InputStream responseBody = post.getResponseBodyAsStream();
			resp = IOUtils.toString(responseBody, "GBK");
			LOG.warn("resp:" + resp);
		} catch (Exception e) {
			LOG.error("调用URL异常",e);
		} finally {
			// 释放连接
			if (post != null)
				post.releaseConnection();
		}
		return resp;
	}

Post提交:

public static String getPostResponse(String url,NameValuePair[] data) throws Exception {
		HttpClient httpClient = new HttpClient();
		PostMethod post = null;
		String resp = null;
		try {
			// 设置超时时间
			int connectionTime = 120*1000;
			httpClient.getHttpConnectionManager().getParams()
					.setConnectionTimeout(connectionTime);
			httpClient.getHttpConnectionManager().getParams()
					.setSoTimeout(connectionTime);

			post = new PostMethod(url);
			 post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
			// 使用系统提供的默认的恢复策略
			post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());

	       // 设置提交参数
			if(data!=null && data.length>0){
				post.addParameters(data);
			}

			// 执行getMethod
			int statusCode = httpClient.executeMethod(post);
			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed:" + post.getStatusLine());
			}
			// 读取内容
			InputStream responseBody = post.getResponseBodyAsStream();
			resp = IOUtils.toString(responseBody, "GBK");
			System.out.println("resp:" + resp);
		} catch (Exception e) {
		e.printStackTrace();
		} finally {
			// 释放连接
			if (post != null)
				post.releaseConnection();
		}
		return resp;
	}

提交JSON:

JSONObject jsonParams = new JSONObject();
			jsonParams.put("keyIndex", "");
			jsonParams.put("encryKey", "");
			jsonParams.put("encryStr", "");
			jsonParams.put("interCode", "INTER.SYSTEM.001");
			String requestBody = jsonParams.toJSONString();

	  		HttpClient httpClient = new HttpClient();
	  		PostMethod post = null;
	  		String resp = "";
	  		try {
	  			post = new PostMethod("https://capi.bestpay.com.cn/common/interface");
	  			//post.setParameter("requestdata",requestBody); 
	  			//post.setRequestHeader("Content-type", "application/json; charset=UTF-8");
	  			// 使用系统提供的默认的恢复策略
	  			post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
	  			
	  			RequestEntity se = new StringRequestEntity(requestBody, "application/json", "UTF-8");
	  			post.setRequestEntity(se);

	  			int statusCode = httpClient.executeMethod(post);
	  			//System.out.println(statusCode);
	  			if (statusCode != HttpStatus.SC_OK) {
	  				System.err.println("Method failed:" + post.getStatusLine());
	  			}
	  			// 读取内容
	  			InputStream responseBody = post.getResponseBodyAsStream();
	  			ret = IOUtils.toString(responseBody, "utf8");
	  			System.out.println(resp);
	  		} catch (Exception e) {
	  			e.printStackTrace();
	  		} finally {
	  			// 释放连接
	  			if (post != null)
	  				post.releaseConnection();
	  		}

 

转载于:https://my.oschina.net/dong706/blog/896549

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值