httpClient 之httpMethod


先贴一段代码

/**

* <p>
* Description:富有充值回调中介
* </p>
* <p>
* Company:xbd
* </p>

* @author:lilei
* @date:2016年11月25日下午2:51:39
*/
@RequestMapping(value = "/angency/recharge")
public String rechargeAngency() {
String resp_code = request.getParameter("resp_code");
String resp_desc = request.getParameter("resp_desc");
// String mchnt_cd = request.getParameter("mchnt_cd");
// String mchnt_txn_ssn = request.getParameter("mchnt_txn_ssn");
// String login_id = request.getParameter("login_id");
// String amt = request.getParameter("amt");
// String signature = request.getParameter("signature");
String charset = "utf-8";
StringBuilder url = new StringBuilder(Config.server_url+"/wx_xbd/my-account.html");
HttpClientHelper httpclient = new HttpClientHelper();
try {
url.append("?1=1");
if (null != resp_code && StringUtils.isNotBlank(resp_code)) {
url.append("&code=" + resp_code);
}
if (null != resp_code && StringUtils.isNotBlank(resp_code)) {

                               // url.append("&msg=" + resp_desc);
url.append("&msg=" + URLEncoder.encode(resp_desc, "utf-8"));
}
httpclient.doGet(url.toString(), charset);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}


/**

* <p>Description:get请求方式 </p>
* <p>Company:xbd </p>
* @author:lilei
* @date:2016年11月7日下午12:31:48
*/
public void doGet(String url, String charset) {
HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
// 创建get实例
GetMethod getMethod = new GetMethod(url);
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
String response = "";
try {
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("请求出错: " + getMethod.getStatusLine());
}
Header[] headers = getMethod.getResponseHeaders();
for (Header h : headers)
System.out.println(h.getName() + "------------ " + h.getValue());
byte[] responseBody = getMethod.getResponseBody();// 读取为字节数组
response = new String(responseBody, charset);
System.out.println("----------response:" + response);
} catch (HttpException e) {
System.out.println(e.getMessage());
} catch (IOException e1) {
System.out.println(e1.getMessage());
} finally {
getMethod.releaseConnection();
}
}


  resp_desc 是一串中文的提示信息,,拼接URL后 发现getMethod竟然解析不了 提示invalid url  非法的url喽,,这是怎么回事啊,,原来url 里不支持有中文字符,之后通过 URLEncoder.encode() 方法转码一下,OK 了 可以正常的解析了!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
http工具类:package com.tpl.util; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; /** * */ public class HttpClientUtil { public static void main(String arg[]) throws Exception { String url = "http://xxx/project/getxxx.action"; JSONObject params= new JSONObject(); List res=new ArrayList(); JSONObject params1 = new JSONObject(); // params1.put("code", "200"); // params1.put("phone", "13240186028"); res.add(params1); params.put("result", res); String ret = doPost(url, params).toString(); System.out.println(ret); } /** httpClient的get请求方式2 * @return * @throws Exception */ public static String doGet(String url, String charset) throws Exception { /* * 使用 GetMethod 来访问一个 URL 对应的网页,实现步骤: 1:生成一个 HttpClinet 对象并设置相应的参数。 * 2:生成一个 GetMethod 对象并设置响应的参数。 3:用 HttpClinet 生成的对象来执行 GetMethod 生成的Get * 方法。 4:处理响应状态码。 5:若响应正常,处理 HTTP 响应内容。 6:释放连接。 */ /* 1 生成 HttpClinet 对象并设置参数 */ HttpClient httpClient = new HttpClient(); // 设置 Http 连接超时为5秒 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); /* 2 生成 GetMethod 对象并设置参数 */ GetMethod getMethod = new GetMethod(url); // 设置 get 请求超时为 5 秒
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值