zoukankan      html  css  js  c++  java
  • Post&&Get实现

    import java.nio.charset.Charset;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.NameValuePair;
    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.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;

    Get实现(Map)

     1     public static String doGet(String url, Map<String, Object> params) {
     2         String httpStr = "";
     3         StringBuffer param = new StringBuffer();
     4         int i = 0;
     5         for (String key : params.keySet()) {
     6             if (i == 0) {
     7                 param.append("?");
     8             } else {
     9                 param.append("&");
    10             }
    11             param.append(key).append("=").append(params.get(key));
    12             i++;
    13         }
    14         url += param;
    15         CloseableHttpClient httpClient = HttpClients.createDefault();
    16         CloseableHttpResponse response = null;
    17         try {
    18             // 创建httpGet
    19             HttpGet httpGet = new HttpGet(url);
    20             // 执行get请求.
    21             response = httpClient.execute(httpGet);
    22             // 获取响应实体
    23             HttpEntity entity = response.getEntity();
    24             if (entity != null) {
    25                 httpStr = EntityUtils.toString(entity, "UTF-8");
    26             }
    27         } catch (Exception e) {
    28             e.printStackTrace();
    29         } finally {
    30             // 关闭连接,释放资源
    31             try {
    32                 response.close();
    33                 httpClient.close();
    34             } catch (Exception e) {
    35                 e.printStackTrace();
    36             }
    37         }
    38         return httpStr;
    39     }

    Post实现(JSON)

        public static String doPost(String url, String json) {
            String httpStr = "";
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            CloseableHttpResponse response = null;
            try {
                StringEntity stringEntity = new StringEntity(json,"UTF-8");// 解决中文乱码问题
                stringEntity.setContentEncoding("UTF-8");
                stringEntity.setContentType("application/json");
                httpPost.setEntity(stringEntity);
                response = httpClient.execute(httpPost);
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    httpStr = EntityUtils.toString(entity, "UTF-8");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭连接,释放资源
                try {
                    response.close();
                    httpClient.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return httpStr;
        }

    Post实现(Map)

        public static String doPost(String url, Map<String, Object> params) {
            String httpStr = "";
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            CloseableHttpResponse response = null;
            try {
                List<NameValuePair> pairList = new ArrayList<NameValuePair>();
                for (Map.Entry<String, Object> entry : params.entrySet()) {
                    NameValuePair pair = new BasicNameValuePair(entry.getKey(),
                            entry.getValue().toString());
                    pairList.add(pair);
                }
                httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset
                        .forName("UTF-8")));
                response = httpClient.execute(httpPost);
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    httpStr = EntityUtils.toString(entity, "UTF-8");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭连接,释放资源
                try {
                    response.close();
                    httpClient.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return httpStr;
        }
  • 相关阅读:
    ADT上跑java application
    GIT 用RSA加密方式来记住密码
    android中listView下有Button按钮始终在最底部
    Android学习:导入工程时报错The import android cannot be resolved
    Preference的使用
    MySQL查询时,查询结果如何按照where in数组排序
    MySQL创建民族表的SQL语句
    PHP 使用GD库合成带二维码和圆形头像的海报步骤以及源码实现
    使用file_get_contents()和curl()抓取网络资源的效率对比
    常用正则表达式整理(持续更新中)
  • 原文地址:https://www.cnblogs.com/lansetuerqi/p/8336852.html
Copyright © 2011-2022 走看看