网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

发送带参数的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;

}

}


网站名称:发送带参数的get请求,并解决httpclient编码问题
本文URL:http://bjjierui.cn/article/ppesgi.html

其他资讯