• (原创)如何获取网页URL的source code (Android)


    1.对于常规的网址如: http开头的:

     URL oracle = new URL( url);
               String test = oracle.toExternalForm();
               String test1 = oracle.toString();
               URI oracle1 = null;
               try {
                 oracle1 = oracle.toURI();
            } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                BufferedReader in = new BufferedReader(
                new InputStreamReader(oracle.openStream()));

                String inputLine;
                String inputLine2 = "";
                while ((inputLine = in.readLine()) != null)
                    inputLine2 +=inputLine;
                in.close(); 

    2.对加密网址:如https: (特别是需要证书的)

    solution:取消所有证书的检测 

    package s.s.d;

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.ProtocolException;
    import java.net.URL;
    import java.security.GeneralSecurityException;
    import java.security.SecureRandom;
    import java.security.cert.X509Certificate;
    import java.util.List;
    import java.util.Map;

    import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;

    import org.apache.http.HttpConnectionMetrics;

    public class AndroidHttpConnection implements ChunkedHttpConnection {
        
        HttpURLConnection  _conn;
        Map<String, List<String>>  _headers;
        
        private static HostnameVerifier _hostnameVerifier;
        private static TrustManager[] _trustManagers;
        static {
            trustAllHostnames();
            trustAllHttpsCertificates();
        }
        
        public static class FakeHostnameVerifier implements HostnameVerifier {
            public boolean verify(String hostname, javax.net.ssl.SSLSession session) {
                return (true);
            }
        }

        private static void trustAllHostnames() {
            if (_hostnameVerifier == null) {
                _hostnameVerifier = new FakeHostnameVerifier();
            }
            HttpsURLConnection.setDefaultHostnameVerifier(_hostnameVerifier);    
        }

        public static class FakeX509TrustManager implements X509TrustManager {
            private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};

            public void checkClientTrusted(X509Certificate[] chain, String authType) {
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType) {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return (_AcceptedIssuers);
            }          
        }

        private static void trustAllHttpsCertificates() {
            SSLContext context;

            // Create a trust manager that does not validate certificate chains
            if (_trustManagers == null) {
                _trustManagers = new TrustManager[] { new FakeX509TrustManager() };
            }
            try {
                context = SSLContext.getInstance("TLS");
                context.init(null, _trustManagers, new SecureRandom());
            } catch (GeneralSecurityException gse) {
                throw new IllegalStateException(gse.getMessage());
            }
            HttpsURLConnection.setDefaultSSLSocketFactory(context
                    .getSocketFactory());
        }


        
        
        public AndroidHttpConnection(URL url) throws IOException
        {
            if (!"http".equalsIgnoreCase(url.getProtocol()) && !"https".equalsIgnoreCase(url.getProtocol()))
                throw new ProtocolException("Protocol " + url.getProtocol() + " doesn't support output");    
            
            _conn = (HttpURLConnection)url.openConnection();
            _conn.setRequestMethod("GET");
            _conn.setRequestProperty("Accept", "application/xml");
            _conn.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
                //c.setRequestProperty("Accept-Encoding", "gzip,deflate");
            _conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.5");
            _conn.setRequestProperty("Keep-Alive", "300");
            _conn.setRequestProperty("Connection", "keep-alive");
            //_conn.connect();
            
    //_conn.setAllowUserInteraction(false);
            _conn.setUseCaches(true);
            _conn.setInstanceFollowRedirects(false);
            _conn.setDoOutput(true);
        }
        
        public AndroidHttpConnection(String url) throws IOException
        {
            this(new URL(url));
        }

        public long getDate() throws IOException {
            
            return _conn.getDate();
            
        }

        public long getExpiration() throws IOException {
            return _conn.getExpiration();
        }

        public String getFile() {
            return _conn.getURL().getFile();
        }

        public Map<String, List<String>> getHeaders()
        {
            if (_headers == null)
                _headers = _conn.getHeaderFields();        
            
            return _headers;
        }
        public String getHeaderField(String name) throws IOException {
            getHeaders();
            List<String> result = (List<String>)_headers.get(name.toLowerCase());
            if (result != null)
                return result.get(result.size()-1);
            return null;
        }

        public String getHeaderField(int n) throws IOException {
            return _conn.getHeaderField(n);
        }

        public long getHeaderFieldDate(String name, long def) throws IOException {
            return _conn.getHeaderFieldDate(name, def);
        }

        public int getHeaderFieldInt(String name, int def) throws IOException {
            return _conn.getHeaderFieldInt(name, def);
        }

        public String getHeaderFieldKey(int n) throws IOException {
            return _conn.getHeaderFieldKey(n);
        }

        public String getHost() {
            return _conn.getURL().getHost();
        }

        public long getLastModified() throws IOException {
            return _conn.getLastModified();
        }

        public int getPort() {
            return _conn.getURL().getPort();
        }

        public String getProtocol() {
            return _conn.getURL().getProtocol();
        }

        public String getQuery() {
            return _conn.getURL().getQuery();
        }

        public String getRef() {
            return _conn.getURL().getRef();
        }

        public String getRequestMethod() {
            return _conn.getRequestMethod();
        }

        public String getRequestProperty(String key) {
            return _conn.getRequestProperty(key);
        }

        public int getResponseCode() throws IOException {
            //getHeaders();
            return _conn.getResponseCode();
        }

        public String getResponseMessage() throws IOException {
            //getHeaders();
            return _conn.getResponseMessage();
        }

        public String getURL() {
            return _conn.getURL().toString();
        }

        public void setRequestMethod(String method) throws IOException {
            _conn.setRequestMethod(method);
        }

        public void setRequestProperty(String key, String value) throws IOException {
            if (key.equalsIgnoreCase("Transfer-Encoding") && value.equalsIgnoreCase("chunked"))
                _conn.setChunkedStreamingMode(0);
            _conn.setRequestProperty(key, value);
        }

        public String getEncoding() {
            return _conn.getContentEncoding();
        }

        public long getLength() {
            return _conn.getContentLength();
        }

        public String getType() {
            return _conn.getContentType();
        }

        protected InputStream      _is;
        protected DataInputStream _dis;
        public DataInputStream openDataInputStream() throws IOException {        
            if (_dis == null)
                _dis = new DataInputStream(openInputStream());
            return _dis;
        }

        public InputStream openInputStream() throws IOException {
            if (_is == null)
            {    
                 //if (_conn.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST)
                
    // {
                
    //    _is = _conn.getErrorStream();
                 
    //}
                
    //else
                {
                    _is = _conn.getInputStream();
                }
            }
            return _is;
        }

        public void close() throws IOException {
            _conn.disconnect();
        }

        protected OutputStream     _os;
        protected DataOutputStream _dos;
        public DataOutputStream openDataOutputStream() throws IOException {
            if (_dos == null)
                _dos = new DataOutputStream(openOutputStream());
            return _dos;
        }

        public OutputStream openOutputStream() throws IOException {
            if (_os == null)
                _os = _conn.getOutputStream();
            return _os;
        }
    /*
        public void setAuthenticator(final Authenticator auth) {
            java.net.Authenticator.setDefault(new java.net.Authenticator(){
                java.net.PasswordAuthentication _pa = null;
                
                protected java.net.PasswordAuthentication getPasswordAuthentication()
                {
                    if (_pa == null)
                    {
                        char[] pass = null;
                        PasswordAuthentication pa = auth.onAuthenticationChallenge(this.getRequestingPrompt(), true, true);
                        if(pa.getPassword() != null)
                        {
                            pass = new char[pa.getPassword().length];
                            for(int i = 0; i < pass.length; i++)
                                pass[i] = (char)pa.getPassword()[i];
                        }
                        _pa = new java.net.PasswordAuthentication(new String(pa.getUserName()), pass);
                    }
                    return _pa;
                }
            });
            
        }
    */




        public InputStream openChunkedInputStream() throws IOException {
            return openInputStream();
        }




        public OutputStream openChunkedOutputStream(int cacheLength)
                throws IOException {
            _conn.setChunkedStreamingMode(cacheLength);        
            return openOutputStream();
        }




        public HttpConnectionMetrics getMetrics() {
            // TODO Auto-generated method stub
            return null;
        }




        public int getSocketTimeout() {
            // TODO Auto-generated method stub
            return 0;
        }




        public boolean isOpen() {
            // TODO Auto-generated method stub
            return false;
        }




        public boolean isStale() {
            // TODO Auto-generated method stub
            return false;
        }




        public void setSocketTimeout(int arg0) {
            // TODO Auto-generated method stub
            
        }




        public void shutdown() throws IOException {
            // TODO Auto-generated method stub
            
        }
    }

    2).

    package s.s.d;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    import org.apache.http.HttpConnection;

    public interface ChunkedHttpConnection extends HttpConnection {
        public InputStream openChunkedInputStream() throws IOException;
        public OutputStream openChunkedOutputStream(int cacheLength) throws IOException;
    }

    3).主题部分:

    AndroidHttpConnection con = new AndroidHttpConnection(url);
             //con.setRequestMethod("GET");
             Map<String, List<String>> headers = con.getHeaders();
             int count = headers.size();    
             
             String value = con.getResponseMessage();
             
             InputStream is = con.openInputStream();
            // OutputStream os = con.openOutputStream();
             
            
    // os.write(buffer, offset, count)
             
             
             BufferedReader in = new BufferedReader(new InputStreamReader(is));          
             
             String inputLine; 
             String htmlCode = ""; 
             
             try { 
                while ((inputLine = in.readLine()) != null) { 
                    htmlCode += inputLine; 
                   // Log.d(LOG_TAG, "html: " + inputLine); 
                } 
             
                in.close(); 
                } catch (Exception e) { 
                    e.printStackTrace(); 
                   // Log.d(LOG_TAG, "Error: " + e.getMessage()); 
                    
    //Log.d(LOG_TAG, "HTML CODE: " + htmlCode); 
                }  
            

     网上搜索了一下:

    1).【android】HttpURLConnection 几种不同方法示例【上】

    http://www.cnblogs.com/harrisonpc/archive/2011/03/31/2001520.html

    2).【android】HttpURLConnection 几种不同方法示例【中】

    http://www.cnblogs.com/harrisonpc/archive/2011/03/31/2001526.html

    3).【android】HttpURLConnection 几种不同方法示例【下】

    http://www.cnblogs.com/harrisonpc/archive/2011/03/31/2001536.html 

  • 相关阅读:
    【Linux开发】linux设备驱动归纳总结(七):1.时间管理与内核延时
    【Linux开发】linux设备驱动归纳总结(七):1.时间管理与内核延时
    【Linux开发】linux设备驱动归纳总结(七):2.内核定时器
    【Linux开发】linux设备驱动归纳总结(七):2.内核定时器
    【Linux开发】linux设备驱动归纳总结(八):1.总线、设备和驱动
    【Linux开发】linux设备驱动归纳总结(八):1.总线、设备和驱动
    【Linux开发】linux设备驱动归纳总结(六):1.中断的实现
    【Linux开发】linux设备驱动归纳总结(六):1.中断的实现
    【Linux开发】linux设备驱动归纳总结(六):2.分享中断号
    【Linux开发】linux设备驱动归纳总结(六):2.分享中断号
  • 原文地址:https://www.cnblogs.com/Jessy/p/2397720.html
Copyright © 2020-2023  润新知