Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URLConnection throws incorrect exception if address cannot be resolved #30

Open
ingebot opened this issue Apr 14, 2016 · 2 comments
Open
Labels
bug Something isn't working

Comments

@ingebot
Copy link
Collaborator

ingebot commented Apr 14, 2016

Note: this issue was migrated automatically using bugzilla2github

Original bug ID: BZ#70661
From: @guillerodriguez
Reported version: 0.99

@ingebot
Copy link
Collaborator Author

ingebot commented Apr 14, 2016

Comment author: @guillerodriguez

According to the API specification, the URLConnection.connect() method should throw an IOException if an error occurs. However in GNU Classpath if the address can be resolved, an java.nio.channels.UnresolvedAddressException is thrown instead. UnresolvedAddressException is a subclass of IllegalArgumentException, not IOException, so this breaks the API contract.

Here is a sample test case:

import java.net.URL;
import java.net.URLConnection;

public class UnresolvedAddress
{
  public static void main(String args[]) throws Exception
  {
    URL url = new URL("http", "someinvalidsitename.com", 80, "index.html");
    URLConnection connection = url.openConnection();
    connection.setUseCaches(false);
    connection.connect();
  }
}

This results in the following:

Exception in thread "main" java.nio.channels.UnresolvedAddressException
   at gnu.java.nio.SocketChannelImpl.connect(SocketChannelImpl.java:160)
   at gnu.java.net.PlainSocketImpl.connect(PlainSocketImpl.java:281)
   at java.net.Socket.connect(Socket.java:463)
   at java.net.Socket.connect(Socket.java:414)
   at gnu.java.net.protocol.http.HTTPConnection.getSocket(HTTPConnection.java:721)
   at gnu.java.net.protocol.http.HTTPConnection.getOutputStream(HTTPConnection.java:802)
   at gnu.java.net.protocol.http.Request.dispatch(Request.java:292)
   at gnu.java.net.protocol.http.HTTPURLConnection.connect(HTTPURLConnection.java:219)
   at UnresolvedAddress.main(UnresolvedAddress.java:11)

Note that the JDK does the right thing here:

Exception in thread "main" java.net.UnknownHostException: someinvalidsitename.com
  at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:175)
  at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:385)
  at java.net.Socket.connect(Socket.java:546)
  at java.net.Socket.connect(Socket.java:495)
  at sun.net.NetworkClient.doConnect(NetworkClient.java:178)
  at sun.net.www.http.HttpClient.openServer(HttpClient.java:427)
  at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
  at sun.net.www.http.HttpClient.<init>(HttpClient.java:213)
  at sun.net.www.http.HttpClient.New(HttpClient.java:306)
  at sun.net.www.http.HttpClient.New(HttpClient.java:325)
  at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:955)
  at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:891)
  at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:809)
  at UnresolvedAddress.main(UnresolvedAddress.java:11)

@ingebot
Copy link
Collaborator Author

ingebot commented Apr 14, 2016

Comment author: @guillerodriguez

"if the address can be resolved.." should have been "if the address can't be resolved".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant