zoukankan      html  css  js  c++  java
  • RequestUtil 获取网址页面信息

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStream;

    import java.io.InputStreamReader;

    import java.io.Reader;

    import java.io.StringWriter;

    import java.io.Writer;

    import java.net.HttpURLConnection;

    import java.net.MalformedURLException;

    import java.net.URL;

    import java.util.Map;

    import javax.servlet.http.HttpServletRequest;

    public class RequestUtil {

             public final static String getErrorUrl(HttpServletRequest request) {

                       String errorUrl = (String) request.getAttribute("javax.servlet.error.request_uri");

                       if (errorUrl == null) {

                                errorUrl = (String) request.getAttribute("javax.servlet.forward.request_uri");

                       }

                       if (errorUrl == null) {

                                errorUrl = (String) request.getAttribute("javax.servlet.include.request_uri");

                       }

                       if (errorUrl == null) {

                                errorUrl = request.getRequestURL().toString();

                       }

                       return errorUrl;

             }

        /**

         * 获取 请求参数

         * @param request

         * @return

         */

             public static String queryString(HttpServletRequest request){

                       Map<String, String[]> params = request.getParameterMap(); 

            String queryString = ""; 

            for (String key : params.keySet()) { 

                String[] values = params.get(key); 

                for (int i = 0; i < values.length; i++) { 

                    String value = values[i];

                    if(value!=null){

                             value=StringUtil.stringURLDecoderByUTF8(value);

                    }

                    queryString += key + "=" + value + "&";

                } 

            } 

            // 去掉最后一个空格 

            queryString = queryString.substring(0, queryString.length() - 1); 

            return queryString;

             }

            

             public final static StringBuilder getErrorInfoFromRequest(HttpServletRequest request, boolean isInfoEnabled) {

                       StringBuilder sb = new StringBuilder();

                       String errorUrl = getErrorUrl(request);

                       sb.append(StringUtil.formatMsg("Error processing url: %1, Referrer: %2, Error message: %3. ", errorUrl,

                                         request.getHeader("REFERER"), request.getAttribute("javax.servlet.error.message")));

                       Throwable ex = (Throwable) request.getAttribute("exception");

                       if (ex == null) {

                                ex = (Throwable) request.getAttribute("javax.servlet.error.exception");

                       }

                       if (ex != null) {

                                sb.append(StringUtil.formatMsg("Exception stack trace: ", ex));

                       }

                       return sb;

             }

            

             public final static String getHtml(String fullUrl) throws IOException{

                      

                       URL url = new URL(fullUrl);

                       HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                       InputStream in = conn.getInputStream();

                       Writer writer = new StringWriter();

                       if (in != null) {

                                char[] buffer = new char[1024];

                                try {

                                         Reader reader = new BufferedReader(new InputStreamReader(in,

                                                            "UTF-8"));

                                         int n;

                                         while ((n = reader.read(buffer)) != -1) {

                                                   writer.write(buffer, 0, n);

                                         }

                                } finally {

                                         in.close();

                                }

                       }

                       return writer.toString();

             }

            

             public  static void main(String[]args) throws IOException{

                       String url="http://www.baidu.com";

                       String a=getHtml(url);

                       System.out.println("ss:"+a);

             }

            

    }

  • 相关阅读:
    go-zero尝试运行输出hello-world
    grpc客户端 服务端测试
    protobuf序列化
    protobuff3语法详情
    【转】普通程序员如何转向AI方向
    深度学习微软 azure-云服务器组 centos特殊内核版本 gpu NVIDIA 驱动及CUDA 11.0安装
    分享一个主要用于nas场景的集成了迅雷,百度网盘等软件的docker ubuntu vnc镜像-适用于x86环境
    以spark sql 维护spark streaming offset
    打通es及lucene应用,lucene应用es Query,应用完整的es query
    打通es及lucene应用,lucene应用es Query,结合非queryString查询(二)
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10400889.html
Copyright © 2011-2022 走看看