zoukankan      html  css  js  c++  java
  • 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;
        }
        
    }
  • 相关阅读:
    [CF1398A-E] Codeforces Round 93
    bzoj3758 数数和bzoj3798 特殊的质数
    P4234 最小差值生成树
    [UOJ274] P6664 温暖会指引我们前行
    P4172 [WC2006]水管局长
    bzoj2959 长跑
    bzoj4998 星球联盟(lct+并查集维护动态双连通性)
    P1501 [国家集训队]Tree II
    link-cut-tree
    fhq-treap,splay 模板
  • 原文地址:https://www.cnblogs.com/yshyee/p/4166828.html
Copyright © 2011-2022 走看看