python get请求带参数_发送带参数的get请求,并解决httpclient编码问题

package cn.wemart.httppost;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import java.net.URI;

import java.net.URISyntaxException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;

import org.apache.http.NameValuePair;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.protocol.HTTP;

import org.apache.http.util.EntityUtils;

import org.dom4j.DocumentException;

public class ExecuteGet {

/**

* 组装请求参数

* @param keyVlaueList

*/

public static Map setPostPara(String[][] keyVlaueList) throws DocumentException{

Map map = new HashMap();

for(int i = 0;i

{

map.put(keyVlaueList[i][0],keyVlaueList[i][1]);

}

return map;

}

/**

* 获取接口返回字符串

* @param url

* @throws DocumentException

* @throws URISyntaxException

*/

public static String getGetMethodResponse(String url,String[][] keyVlaueList) {

String xmlStr =null;

String Str;

try {

//把键值对转换成JSON格式串

Map postMap;

postMap = setPostPara(keyVlaueList);

JSONObject object = JSONObject.fromObject(postMap);

List postPara = new ArrayList();

postPara.add(new BasicNameValuePair("para",object.toString()));

System.out.println("+++++para="+object.toString()+"+++++");

//发送转码过的字符串,发送

CloseableHttpClient httpclient = HttpClients.createDefault();

HttpGet httpget = new HttpGet(url);

String sendstr = EntityUtils.toString(new UrlEncodedFormEntity(postPara,HTTP.UTF_8));

httpget.setURI(new URI(httpget.getURI().toString() + "?" + sendstr));

CloseableHttpResponse response = httpclient.execute(httpget);

//处理response的中文编码

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"));

StringBuffer sb = new StringBuffer();

while ((Str = reader.readLine()) != null) {

sb.append(Str).append("\n");

}

xmlStr = sb.toString();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (URISyntaxException e) {

e.printStackTrace();

} catch (DocumentException e) {

e.printStackTrace();

}

return xmlStr;

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值