zoukankan      html  css  js  c++  java
  • HttpClient的get和post方式提交数据的使用

    /**
     * Http工具类
     */
    public class HttpUtil {
        // 创建HttpClient对象
        public static HttpClient httpClient = new DefaultHttpClient();
        public static final String BASE_URL = "";
    
        /**
         * get请求
         *
         * @param url
         *            发送请求的URL
         * @return 服务器响应字符串
         * @throws Exception
         */
        public static String doGet(String url) throws Exception {
            // 创建HttpGet对象。
            HttpGet get = new HttpGet(url);
            // 发送GET请求
            HttpResponse httpResponse = httpClient.execute(get);
            // 如果服务器成功地返回响应
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                // 获取服务器响应字符串
                HttpEntity entity = httpResponse.getEntity();
                InputStream content = entity.getContent();
                return convertStreamToString(content);
            }
            return null;
        }
    
        /**
         * post请求
         *
         * @param url
         *            发送请求的URL
         * @param params
         *            请求参数
         * @return 服务器响应字符串
         * @throws Exception
         */
        public static String doPost(String url, Map<String, String> rawParams)
                throws Exception {
            // 创建HttpPost对象。
            HttpPost post = new HttpPost(url);
            // 如果传递参数个数比较多的话可以对传递的参数进行封装
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            for (String key : rawParams.keySet()) {
                // 封装请求参数
                params.add(new BasicNameValuePair(key, rawParams.get(key)));
            }
            // 设置请求参数
            post.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
            // 发送POST请求
            HttpResponse httpResponse = httpClient.execute(post);
            // 如果服务器成功地返回响应
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                // 获取服务器响应字符串
                HttpEntity entity = httpResponse.getEntity();
                InputStream content = entity.getContent();
                return convertStreamToString(content);
            }
            return null;
        }
    
        /**
         * 获取服务器的响应,转换为字符串
         */
        private static String convertStreamToString(InputStream is) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return sb.toString();
        }
    }
  • 相关阅读:
    【转】2008年中国.NET技术应用趋势分析
    VB计算器(给上学需要应付作业的应个急)
    创业失败的18个原因
    使用ChilkatDotNet组件构建网络爬虫程序
    使用文本编辑器开发和部署一个ASP.NET Web应用程序
    优化 SQL Server 查询性能
    【转】sql性能优化方法
    使用 SOS 对 Linux 中运行的 .NET Core 进行问题诊断
    MultiThread Of Member Functions
    汇编 中断调用表 (中断向量表)
  • 原文地址:https://www.cnblogs.com/androidsuperman/p/3468477.html
Copyright © 2011-2022 走看看