简体   繁体   中英

Access denied when trying to get webpage from a website

Well I enter: Chesslounge.net as the url and I keep getting access denied when I try to get the content what should I do?

Here is some code:

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

}

Here is my logcat where I get the exception:

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)

what should I do?

My first reaction was that you probably need to make tour application login to the site. But I tried visiting the page using a web browser, and it worked.

So maybe the "access denied" is some kind of security thing that is local to your android-based application. Can you please show us the stack trace for this error?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM