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;
    
        }
  • 相关阅读:
    springmvc
    POJ 3683 Priest John's Busiest Day
    POJ 3678 Katu Puzzle
    HDU 1815 Building roads
    CDOJ UESTC 1220 The Battle of Guandu
    HDU 3715 Go Deeper
    HDU 3622 Bomb Game
    POJ 3207 Ikki's Story IV
    POJ 3648 Wedding
    HDU 1814 Peaceful Commission
  • 原文地址:https://www.cnblogs.com/rinack/p/7645731.html
Copyright © 2011-2022 走看看