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);

             }

            

    }

  • 相关阅读:
    [转]android Intent机制详解
    [转]Android进程与线程基本知识
    HTML背景图片自适应
    边框边界填充理解
    [转]Android 代码自动提示功能
    [转]Windows7:Visual Studio 2008试用版的评估期已经结束解决方法
    eclipse安装、汉化、搭建安卓开发环境
    asp.net控件拖不动。控件错误
    opengl 入门浅学(一)
    opengl 无法定位程序输入点_glutInitWithExit于动态链接库glut32.dll上
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10400889.html
Copyright © 2011-2022 走看看