gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:55:35 30 4
gpt4 key购买 nike

我输入: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)

最佳答案

what should I do?

我的第一 react 是您可能需要让旅游应用程序登录到该网站。但我尝试使用网络浏览器访问该页面,结果成功了。

因此,“访问被拒绝”可能是基于 Android 的应用程序本地的某种安全问题。您能否向我们展示此错误的堆栈跟踪?

关于java - 尝试从网站获取网页时访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5131358/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com