用HttpClient发送get,post请求的工具类
2017-07-13 10:13
405 查看
public class HttpRequestUtils { private static Logger log = LoggerFactory.getLogger(HttpRequestUtils.class); //返回对象的json public static JSONObject httpGet(String url){ JSONObject jsonResult = null; try { DefaultHttpClient client =new DefaultHttpClient(); //发送get请求 HttpGet request = new HttpGet(url); HttpResponse response=client.execute(request); //请求发送并得到响应 if (response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){ //读取服务器返回的json字符串数据 HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); BufferedReader in = new BufferedReader(new InputStreamReader(is,"UTF-8")); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null){ buffer.append(line); } jsonResult=JSONObject.fromObject(buffer.toString()); } }catch (Exception e){ log.info("get请求失败:"+ url); } return jsonResult; } //返回数组的json public static JSONArray httpGetArray(String url){ JSONArray jsonResult = null; try { DefaultHttpClient client =new DefaultHttpClient(); //发送get请求 HttpGet request = new HttpGet(url); HttpResponse response=client.execute(request); //请求发送并得到响应 if (response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){ //读取服务器返回的json字符串数据 HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); BufferedReader in = new BufferedReader(new InputStreamReader(is,"UTF-8")); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null){ buffer.append(line); } jsonResult= JSON.parseArray(buffer.toString()); } }catch (Exception e){ log.info("get请求失败:"+ url); } return jsonResult; } //发送post请求 public static JSONObject httpPost(String url,Map<String,String>params) { JSONObject jsonResult = null; try { DefaultHttpClient client = new DefaultHttpClient(); //发送post请求 HttpPost httpPost = new HttpPost(url); List<NameValuePair> nvps = new ArrayList<>(); if (params != null) { Iterator<String> keys = params.keySet().iterator(); while (keys.hasNext()) { String key = keys.next(); nvps.add(new BasicNameValuePair(key, params.get(key))); } //URL格式编码 httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); } HttpResponse response = client.execute(httpPost); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null) { buffer.append(line); } jsonResult = JSONObject.fromObject(buffer.toString()); } } catch (Exception e) { log.info("get请求失败:"+ url); } return jsonResult; } }
相关文章推荐
- java apache commons HttpClient发送get和post请求的学习整理(转)
- android_30_使用HttpClient发送Get和Post请求
- HttpClient发送get请求,post请求,携带cookie访问,json提交
- Android使用Apache HttpClient发送GET、POST请求
- java apache commons HttpClient发送get和post请求的学习整理
- 如何使用火狐下的两款接口测试工具RESTClient和HttpRequester发送post请求
- Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- java apache commons HttpClient发送get和post请求的学习整理
- Java代码中利用httpClient发送post、get请求
- java apache commons HttpClient发送get和post请求的学习整理
- 同步和异步Httpclient通过get和post方式发送请求
- java apache commons HttpClient发送get和post请求的学习整理
- 我的Android进阶之旅------>Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- httpclient发送post/get请求,Java后台怎么接受
- Android使用Apache HttpClient发送GET、POST请求
- java apache commons HttpClient发送get和post请求的学习整理
- Android下通过httpClient发送GET和POST请求的实例代码
- 【实战】(二)android模拟qq登录(apache的httpClient发送get和post请求)
- HttpClient发送Get、Post请求简单实践
- HttpClient发送get post请求和数据解析