zoukankan      html  css  js  c++  java
  • java 发送 HTTP 请求

    一.发送 HTTP GET 请求

        import org.apache.commons.lang.StringUtils;
        import org.apache.http.HttpEntity;
        import org.apache.http.HttpStatus;
        import org.apache.http.client.methods.HttpGet;
        import org.apache.http.client.methods.CloseableHttpResponse;
        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.HttpClientBuilder;
        import org.apache.http.util.EntityUtils;
        import org.apache.http.HttpResponse;
    
        /**
         * 发送 HTTP GET 请求
         * 
         * @param url
         * @return
         */
        public String httpGet(String url) {
    
            String result = "";
    
            // 创建HttpClientBuilder
            HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    
            // HttpClient
            CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
    
            HttpGet httpGet = null;
            HttpResponse httpResponse = null;
            HttpEntity httpEntity = null;
            httpGet = new HttpGet(url);
    
            try {
    
                httpResponse = closeableHttpClient.execute(httpGet);
    
                if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    httpEntity = httpResponse.getEntity();
                    if (httpEntity != null) {
                        result = EntityUtils.toString(httpEntity);
                    }
                }
    
                // 关闭连接
                closeableHttpClient.close();
    
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
    
            return result;
    
        }
    
        /**
         * 执行一个HTTP GET请求,返回请求响应的HTML
         * 
         * @param url
         *            请求的URL地址
         * @param queryString
         *            请求的查询参数,可以为null
         * @return 返回请求响应的HTML
         */
        public String doGet(String url, String queryString) {
            String result = "";
            HttpClient client = new HttpClient();
            HttpMethod method = new GetMethod(url);
            try {
    
                if (StringUtils.isNotBlank(queryString))
                    method.setQueryString(URIUtil.encodeQuery(queryString));
                client.executeMethod(method);
                if (method.getStatusCode() == HttpStatus.SC_OK) {
                    result = method.getResponseBodyAsString();
                }
    
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            } finally {
                method.releaseConnection();
            }
    
            return result;
        }

    二.发送HTTP POST 请求

        import org.apache.commons.lang.StringUtils;
        import org.apache.http.HttpEntity;
        import org.apache.http.HttpStatus;
        import org.apache.http.client.methods.HttpGet;
        import org.apache.http.client.methods.CloseableHttpResponse;
        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.HttpClientBuilder;
        import org.apache.http.util.EntityUtils;
        import org.apache.http.HttpResponse;
        
         /**
         * 发送POST请求
         * @return
         * @throws IOException
         */
        private static String httpPost(String url,String condition) throws IOException {
    
            String result = "";
    
            // 创建HttpClientBuilder
            HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
            CloseableHttpClient closeableHttpClient = null;
            CloseableHttpResponse closeableHttpResponse = null;
    
            HttpEntity httpEntity = null;
            HttpPost httpPost = new HttpPost(builderUrl.toString());
            StringEntity stringEntity = null;
    
            try {
    
                // 添加http头信息
                stringEntity = new StringEntity(condition, "utf-8");// 解决中文乱码问题
                stringEntity.setContentEncoding("UTF-8");
                stringEntity.setContentType("application/json");
                httpPost.setEntity(stringEntity);
    
                closeableHttpClient = httpClientBuilder.build();
                closeableHttpResponse = closeableHttpClient.execute(httpPost);
    
                if (closeableHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    httpEntity = closeableHttpResponse.getEntity();
                    if (httpEntity != null) {
                        result = EntityUtils.toString(httpEntity);
                    }
                }
    
                closeableHttpResponse.close();
                // 关闭连接
                closeableHttpClient.close();
    
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
                throw e;
            }
    
            return result;
    
        }
  • 相关阅读:
    JS对象、包装类
    js刷题网站
    typeof 返回的数据类型
    一文讲懂什么是函数柯里化,柯里化的目的及其代码实现
    JS 中深拷贝的几种实现方法
    JavaScript 开发的45个经典技巧
    JavaScript中的delete操作符
    IE下iframe不能正常加载,显示空白
    使用Costura.Fody插件将自己写的程序打包成一个可以独立运行的EXE文件
    list获取所有上级
  • 原文地址:https://www.cnblogs.com/rinack/p/7645731.html
Copyright © 2011-2022 走看看