java.net.httpurlconnection就不会工作



我想在我的android应用程序中做一个简单的http请求。

((HttpURLConnection) new URL("https://omg-a-site.org").openConnection()).getResponseCode();

我尝试使用java.net.URL.getInputStream和java.net.URLConnection.getInputStream,但它只是不起作用。我做错了什么吗?

你的代码正在做一些事情。下面是一个try-catch,代码在"try"块。如果发生任何ioexception, catch块将输出异常文本中的内容。

try {
((HttpURLConnection) new URL("https://omg-a-site.org").openConnection()).getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}

该代码将产生如下输出:这看起来很合理——它找不到名为" omg.a.site.org ":

的主机
java.net.UnknownHostException: omg-a-site.org
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:567)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
at java.base/java.net.Socket.connect(Socket.java:633)
at java.base/sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:299)
at java.base/sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:174)
at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:183)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:498)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:603)
at java.base/sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:266)
at java.base/sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:380)
at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:189)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1242)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1128)
at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:175)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1665)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1589)
at java.base/java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:529)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:308)
at a68.main(a68.java:8)

相关内容

  • 没有找到相关文章

最新更新