httpclient问题求大神,executeMethod如何么到response? httpClient.executeMethod(get);
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpclient问题求大神,executeMethod如何么到response? httpClient.executeMethod(get);相关的知识,希望对你有一定的参考价值。
private static HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
private static HttpClient httpClient = new HttpClient(httpConnectManager);
int status = httpClient.executeMethod(get);
executeMethod方法如何得到返回的response结果。
* 专门处理返回值是json串的httppost请求
*
* @param map 请求参数的键值对
* @param url 请求地址
* @param clazz 返回类型
* @return
*/
public <T> T doHttpPost(Map<String, String> map, String url, Class<T> clazz)
// System.out.println(map);
CloseableHttpClient hp = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
List<NameValuePair> param = new ArrayList<NameValuePair>();
for (Entry<String, String> en : map.entrySet())
param.add(new BasicNameValuePair(en.getKey(), en.getValue()));
post.setEntity(new UrlEncodedFormEntity(param, Charset.forName("utf-8")));
String result = "";
try
CloseableHttpResponse response = hp.execute(post);
result = EntityUtils.toString(response.getEntity());
// System.out.println(result);
T re = gson.fromJson(result, clazz);
return re;
catch (Exception e)
System.err.println(result);
e.printStackTrace();
finally
try
hp.close();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
return null;
自己封装的一个方法,通过Apache httpclient 官网教程封装的一个处理rest请求的方法
(ps:这里需要Gson对json串转换)
参考技术A String getTokenUrl = "";HttpClient client = new HttpClient();
GetMethod get = new GetMethod(getTokenUrl);
client.executeMethod(get);
if (get.getStatusCode() != HttpStatus.SC_OK)
System.out.println("获取失败,无返回");
String repMsg = get.getResponseBodyAsString();
System.out.println("获取repMsg = " + repMsg); 参考技术B 通过get.getResponseBody()获取响应内容
以上是关于httpclient问题求大神,executeMethod如何么到response? httpClient.executeMethod(get);的主要内容,如果未能解决你的问题,请参考以下文章
httpclient get请求返回的数据乱码?跪求大神帮帮我..下面是代码,就是一个调用接口返回天气预报json数据
用java做一个httpClient 发送https 的get请求,需要证书验证的那种,求大神指点一下!
我用java怎么去实现支付宝接口,没有思路,求大神帮忙,谢谢,