• 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;
    
        }
  • 相关阅读:
    Redis优化经验
    servlet/filter/listener/interceptor区别与联系
    无状态服务(stateless service)
    http请求中java中的302和sendRedirect的区别
    深入ThreadLocal之三(ThreadLocal可能引起的内存泄露)
    Connection reset原因分析和解决方案
    深入ThreadLocal之一
    ThreadLocal的坑--ThreadLocal跨线程传递问题
    MySQL通配符过滤
    六、Linux/UNIX操作命令积累【kill、netstat、df、du】
  • 原文地址:https://www.cnblogs.com/rinack/p/7645731.html
Copyright © 2020-2023  润新知