尝试从网站获取网页时拒绝访问

时间:2011-02-27 03:56:05

标签: java android html get

我输入:Chesslounge.net作为网址,当我尝试获取内容时,我会继续拒绝访问权限。

以下是一些代码:

switch(v.getId()) {

            case R.id.button1:

                EditText text = (EditText)findViewById(R.id.editText1);
                TextView tv =(TextView)findViewById(R.id.textView2);
                String data;
                WebFile htmlfile;
            try {
                htmlfile = new WebFile(text.getText().toString());
                data = (String)htmlfile.getContent();

                tv.setText(data);
            } catch (MalformedURLException e) {
                tv.setText("Error: Enter Valid URL");
            } catch (IOException e) {
                tv.setText(e.getMessage());

            }










        }







    }


public final class WebFile {
    // Saved response.
    private java.util.Map<String,java.util.List<String>> responseHeader = null;
    private java.net.URL responseURL = null;
    private int responseCode = -1;
    private String MIMEtype  = null;
    private String charset   = null;
    private Object content   = null;

    /** Open a web file. */
    public WebFile( String urlString )
        throws java.net.MalformedURLException, java.io.IOException {
        // Open a URL connection.
        final java.net.URL url = new java.net.URL( urlString );
        final java.net.URLConnection uconn = url.openConnection( );
        if ( !(uconn instanceof java.net.HttpURLConnection) )
            throw new java.lang.IllegalArgumentException(
                "URL protocol must be HTTP." );
        final java.net.HttpURLConnection conn =
            (java.net.HttpURLConnection)uconn;

        // Set up a request.
        conn.setConnectTimeout( 10000 );    // 10 sec
        conn.setReadTimeout( 10000 );       // 10 sec
        conn.setInstanceFollowRedirects( true );
        conn.setRequestProperty( "User-agent", "spider" );

        // Send the request.
        conn.connect( );

        // Get the response.
        responseHeader    = conn.getHeaderFields( );
        responseCode      = conn.getResponseCode( );
        responseURL       = conn.getURL( );
        final int length  = conn.getContentLength( );
        final String type = conn.getContentType( );
        if ( type != null ) {
            final String[] parts = type.split( ";" );
            MIMEtype = parts[0].trim( );
            for ( int i = 1; i < parts.length && charset == null; i++ ) {
                final String t  = parts[i].trim( );
                final int index = t.toLowerCase( ).indexOf( "charset=" );
                if ( index != -1 )
                    charset = t.substring( index+8 );
            }
        }

        // Get the content.
        final java.io.InputStream stream = conn.getErrorStream( );
        if ( stream != null )
            content = readStream( length, stream );
        else if ( (content = conn.getContent( )) != null &&
            content instanceof java.io.InputStream )
            content = readStream( length, (java.io.InputStream)content );
        conn.disconnect( );
    }

    /** Read stream bytes and transcode. */
    private Object readStream( int length, java.io.InputStream stream )
        throws java.io.IOException {
        final int buflen = Math.max( 1024, Math.max( length, stream.available() ) );
        byte[] buf   = new byte[buflen];;
        byte[] bytes = null;

        for ( int nRead = stream.read(buf); nRead != -1; nRead = stream.read(buf) ) {
            if ( bytes == null ) {
                bytes = buf;
                buf   = new byte[buflen];
                continue;
            }
            final byte[] newBytes = new byte[ bytes.length + nRead ];
            System.arraycopy( bytes, 0, newBytes, 0, bytes.length );
            System.arraycopy( buf, 0, newBytes, bytes.length, nRead );
            bytes = newBytes;
        }

        if ( charset == null )
            return bytes;
        try {
            return new String( bytes, charset );
        }
        catch ( java.io.UnsupportedEncodingException e ) { }
        return bytes;
    }

    /** Get the content. */
    public Object getContent( ) {
        return content;
    }

    /** Get the response code. */
    public int getResponseCode( ) {
        return responseCode;
    }

    /** Get the response header. */
    public java.util.Map<String,java.util.List<String>> getHeaderFields( ) {
        return responseHeader;
    }

    /** Get the URL of the received page. */
    public java.net.URL getURL( ) {
        return responseURL;
    }

    /** Get the MIME type. */
    public String getMIMEType( ) {
        return MIMEtype;
    }
}

}

这是我的logcat,我得到了例外:

02-27 08:06:23.622: WARN/System.err(279): java.net.SocketException: Permission denied
02-27 08:06:23.652: WARN/System.err(279):     at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
02-27 08:06:23.652: WARN/System.err(279):     at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:186)
02-27 08:06:23.662: WARN/System.err(279):     at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:265)
02-27 08:06:23.662: WARN/System.err(279):     at java.net.Socket.checkClosedAndCreate(Socket.java:873)
02-27 08:06:23.662: WARN/System.err(279):     at java.net.Socket.connect(Socket.java:1020)
02-27 08:06:23.672: WARN/System.err(279):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
02-27 08:06:23.672: WARN/System.err(279):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
02-27 08:06:23.672: WARN/System.err(279):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
02-27 08:06:23.682: WARN/System.err(279):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
02-27 08:06:23.682: WARN/System.err(279):     at com.apps.blogspot.blogspot$WebFile.<init>(blogspot.java:110)
02-27 08:06:23.682: WARN/System.err(279):     at com.apps.blogspot.blogspot.onClick(blogspot.java:49)
02-27 08:06:23.692: WARN/System.err(279):     at android.view.View.performClick(View.java:2408)
02-27 08:06:23.692: WARN/System.err(279):     at android.view.View$PerformClick.run(View.java:8816)
02-27 08:06:23.692: WARN/System.err(279):     at android.os.Handler.handleCallback(Handler.java:587)
02-27 08:06:23.702: WARN/System.err(279):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-27 08:06:23.702: WARN/System.err(279):     at android.os.Looper.loop(Looper.java:123)
02-27 08:06:23.702: WARN/System.err(279):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-27 08:06:23.712: WARN/System.err(279):     at java.lang.reflect.Method.invokeNative(Native Method)
02-27 08:06:23.712: WARN/System.err(279):     at java.lang.reflect.Method.invoke(Method.java:521)
02-27 08:06:23.712: WARN/System.err(279):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-27 08:06:23.722: WARN/System.err(279):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-27 08:06:23.722: WARN/System.err(279):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

  

我该怎么办?

我的第一反应是您可能需要登录该网站的旅游应用程序。但我尝试使用网络浏览器访问该页面,并且它有效。

所以也许“拒绝访问”是某种基于android的应用程序本地安全的东西。你能告诉我们这个错误的堆栈跟踪吗?