zoukankan      html  css  js  c++  java
  • 向服务器发送post请求

    /**
         * 通过HttpClient发送Post请求
         * @param path 请求路径
         * @param params 请求参数
         * @param encoding 编码
         * @return 请求是否成功
         */
        private static boolean sendHttpClientPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
            List<NameValuePair> pairs = new ArrayList<NameValuePair>();//存放请求参数
            if(params!=null && !params.isEmpty()){
                for(Map.Entry<String, String> entry : params.entrySet()){
                    pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
            }
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, encoding);
            HttpPost httpPost = new HttpPost(path);
            httpPost.setEntity(entity);
            DefaultHttpClient client = new DefaultHttpClient();
            HttpResponse response = client.execute(httpPost);
            if(response.getStatusLine().getStatusCode() == 200){
                return true;
            }
            return false;
        }
        /**
         * 发送Post请求
         * @param path 请求路径
         * @param params 请求参数
         * @param encoding 编码
         * @return 请求是否成功
         */
        private static boolean sendPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
            //  title=liming&timelength=90
            StringBuilder data = new StringBuilder();
            if(params!=null && !params.isEmpty()){
                for(Map.Entry<String, String> entry : params.entrySet()){
                    data.append(entry.getKey()).append("=");
                    data.append(URLEncoder.encode(entry.getValue(), encoding));
                    data.append("&");
                }
                data.deleteCharAt(data.length() - 1);
            }
            byte[] entity = data.toString().getBytes();//生成实体数据
            HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);//允许对外输出数据
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
            OutputStream outStream = conn.getOutputStream();
            outStream.write(entity);
            if(conn.getResponseCode() == 200){
                return true;
            }
            return false;
        }
        /**
         * 发送GET请求
         * @param path 请求路径
         * @param params 请求参数
         * @param encoding 编码
         * @return 请求是否成功
         */
        private static boolean sendGETRequest(String path, Map<String, String> params, String ecoding) throws Exception{
            // http://192.168.1.100:8080/web/ManageServlet?title=xxx&timelength=90
            StringBuilder url = new StringBuilder(path);
            url.append("?");
            for(Map.Entry<String, String> entry : params.entrySet()){
                url.append(entry.getKey()).append("=");
                url.append(URLEncoder.encode(entry.getValue(), ecoding));
                url.append("&");
            }
            url.deleteCharAt(url.length() - 1);
            HttpURLConnection conn = (HttpURLConnection)new URL(url.toString()).openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            if(conn.getResponseCode() == 200){
                return true;
            }
            return false;
        }

  • 相关阅读:
    android之下载416错误
    eclipse之常用工具总结
    php之Callback 回调类型
    smarty的自定义函数
    Unable to open sync connection异常
    android之android Studio 安装后打不开的解决方法
    android之ExpandableListView 的滑动到底部的精确监听事件
    wampserver2.0下配置虚拟主机
    wc之初认识
    php开发中常见函数记录
  • 原文地址:https://www.cnblogs.com/heml/p/3512416.html
Copyright © 2011-2022 走看看