首页 > 代码库 > httpcomponents-client-4.3.6 HttpPost的简单使用
httpcomponents-client-4.3.6 HttpPost的简单使用
/** * httpcomponents-client-4.3.6 * @author y */public class HttpUtil { public static String httpPost( List<NameValuePair> formparams,final String url){ UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8); //设置网络超时 RequestConfig config = RequestConfig.custom() .setConnectionRequestTimeout(3*1000) .setConnectTimeout(3*1000) .setSocketTimeout(3*1000) .build(); HttpPost httppost = new HttpPost(url); httppost.setConfig(config); httppost.setEntity(entity); CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = null; String reuslt = ""; try { response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entityContent = response.getEntity(); if (entityContent != null) { reuslt = EntityUtils.toString(entityContent, Consts.UTF_8); //指定编码格式防止中文乱码 } }else{ Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, "网络链接超时"); } } catch (IOException ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); }finally { try{ if(response!=null){ response.close(); } }catch (IOException ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); } } return reuslt; } }
httpcomponents-client-4.3.6 HttpPost的简单使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。