我正在尝试连接我在我的Android应用程序中运行的Web服务器但是由于某种原因它失败了。
我可以正常连接到网页并使用我的手机浏览器,我也可以使用以下代码连接到谷歌但由于某种原因我的代码将无法连接到我的网络服务器。如果我在端口80上托管服务器,我每次都会得到错误代码503,如果我将它托管在8080上(这是更好的),我会得到一个超时异常,低于。
03-05 20:12:22.432: WARN/System.err(29254): java.net.SocketException: The operation timed out
03-05 20:12:22.602: WARN/System.err(29254): at org.apache.harmony.luni.platform.OSNetworkSystem.connectSocketImpl(Native Method)
03-05 20:12:22.602: WARN/System.err(29254): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:125)
03-05 20:12:22.602: WARN/System.err(29254): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:227)
03-05 20:12:22.612: WARN/System.err(29254): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:521)
03-05 20:12:22.612: WARN/System.err(29254): at java.net.Socket.connect(Socket.java:1019)
03-05 20:12:22.612: WARN/System.err(29254): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:67)
03-05 20:12:22.612: WARN/System.err(29254): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager$ConnectionPool.getHttpConnection(HttpConnectionManager.java:151)
03-05 20:12:22.612: WARN/System.err(29254): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager.getConnection(HttpConnectionManager.java:73)
03-05 20:12:22.612: WARN/System.err(29254): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getHTTPConnection(HttpURLConnection.java:826)
03-05 20:12:22.612: WARN/System.err(29254): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:812)
03-05 20:12:22.612: WARN/System.err(29254): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getResponseCode(HttpURLConnection.java:1275)
03-05 20:12:22.612: WARN/System.err(29254): at com.project.library.remailer.ServerSelect.downloadDirectory(ServerSelect.java:84)
03-05 20:12:22.622: WARN/System.err(29254): at com.project.main.NewMessage$3$1$1.run(NewMessage.java:156)
代码是:
try {
URL url = new URL(directoryLocation);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// The line below is where the exception is called
int response = httpURLConnection.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
// Response successful
InputStream inputStream = httpURLConnection.getInputStream();
// Parse it line by line
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String temp;
while ((temp = bufferedReader.readLine()) != null) {
// Parsing of data here
}
} else {
Log.e("SAMES", "INCORRECT RETURN CODE: " + response);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
当我尝试CommonsWare为端口8080建议的链接中的代码时,我得到了与之前相同的错误,即超时异常。对于端口80,我得到以下异常,这两个错误都发生在url.openStream()上。
端口80例外:
03-06 11:53:53.296: WARN/System.err(639): java.io.FileNotFoundException: http://64.197.194.165:80/path
03-06 11:53:53.376: WARN/System.err(639): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1064)
03-06 11:53:53.376: WARN/System.err(639): at java.net.URL.openStream(URL.java:674)
03-06 11:53:53.376: WARN/System.err(639): at com.project.library.remailer.ServerSelect.downloadDirectory(ServerSelect.java:118)
03-06 11:53:53.376: WARN/System.err(639): at com.project.main.NewMessage$3$1$1.run(NewMessage.java:156)
我也尝试使用以下代码:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(directoryLocation);
try
{
HttpResponse response = client.execute(request);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line;
while((line = reader.readLine()) != null) {
Log.e("SAMES", line);
}
}
catch(Exception e)
{
e.printStackTrace();
}
但是,这导致端口8080出现相同的超时错误,这在调用client.execute(响应)时发生。
对于端口80,我得到一个网页,说它无法连接,这很多HTML编辑,但它在页面上的主要内容是:
03-06 13:10:23.576:ERROR / SAMES(1267):Web服务器可能已关闭,太忙或遇到阻止其响应请求的其他问题。您可能希望稍后再试。
答案 0 :(得分:1)
各种说明:
答案 1 :(得分:1)
你的问题过于复杂(在我看来)是为了完全解决这个问题,所以我想就我最近遇到的问题分享我的调查结果,这正是你在评论中所描述的:
// The line below is where the exception is called
int response = httpURLConnection.getResponseCode();
同一段代码在我的一个类中导致了IOException
- 每次都在每个有效的URL
上,所以很明显问题出现在代码中 - 看起来和你的一样 - 简单,没有标题集,没有任何类似教程的例子。然后我注意到我看到的每个例子都没有调用URLConnection.connect()
方法 - 所以我想:实际连接何时发生?标准HttpURLConnection
使用模式的样子是什么样的?
不幸的是,该主题的文档很少。所以我做了一些谷歌搜索,并发现了这篇博文: HttpURLConnection's Dark Secrets - by Tim Bray ,这对编写更复杂的HttpURLConnection
使用时非常宝贵。它也为我的问题提供了答案 - 让我引用相关的话:
// this opens a connection, then sends GET & headers
in = conn.getInputStream();
// can't get status before getInputStream. If you try, you'll
// get a nasty exception.
http_status = conn.getResponseCode();
// better check it first
if (http_status / 100 != 2) {
// redirects, server errors, lions and tigers and bears! Oh my!
}
请参阅第1和第2条评论?嗯,如果您知道实际的网络连接是由HttpURLConnection.getInputStream()
(HttpURLConnection.getOutputStream()
发起的,如果您要向服务器发送任何内容),那么这很有意义。在 getResponseCode()
之后放置getInputStream()
的调用解决了我的问题。
答案 2 :(得分:0)
如果您在致电httpURLConnection.connect()
后尝试致电url.openConnection()
会怎样?
答案 3 :(得分:-2)
只有具有root权限的应用才能打开1024以下的端口 - 这就是端口80的问题。