2

I'm trying to establish a http connection and read the contents of a HTML page. This is the class I'm using to connect to a page,

public class Connection { private URL url; private HttpURLConnection httpURLConnection; public Connection(String url) throws MalformedURLException { this.url = new URL(url); this.httpURLConnection = new HttpURLConnection(this.url) { @Override public void connect() throws IOException { httpURLConnection.connect(); httpURLConnection.setReadTimeout(15000); httpURLConnection.setConnectTimeout(15000); httpURLConnection.setUseCaches(false); HttpURLConnection.setFollowRedirects(true); } @Override public void disconnect() { httpURLConnection.disconnect(); } @Override public boolean usingProxy() { return false; } }; } public String parse() throws IOException { InputStream is = httpURLConnection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; StringBuffer response = new StringBuffer(); while ((line = rd.readLine()) != null) { response.append(line); response.append('\n'); } rd.close(); return response.toString(); } } 

This is the calling code,

public static void main(String[] args) { // write your code here String url = "https://www.buffalo.edu"; Connection c; String str = null; try { c = new Connection(url); str = c.parse(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(str); } 

I get the following Exception

 null java.net.UnknownServiceException: protocol doesn't support input at java.net.URLConnection.getInputStream(URLConnection.java:830) at io.soumasish.connections.Connection.parse(Connection.java:47) at io.soumasish.App.main(App.java:17) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 

What am I doing wrong here. Any help appreciated.

0

2 Answers 2

1

You have a wrong usage of HttpURLConnection. It's a loop of connect() which does nothing

this.httpURLConnection = new HttpURLConnection(this.url) { @Override public void connect() throws IOException { httpURLConnection.connect(); ... 

The correct pattern of HttpURLConnection Class is mentioned here: https://developer.xamarin.com/api/type/Java.Net.HttpURLConnection/

Modify your Connection() like below will make it work.

public Connection(String url) throws IOException { this.url = new URL(url); this.httpURLConnection = (HttpURLConnection) this.url.openConnection(); } 
Sign up to request clarification or add additional context in comments.

Comments

0

There is a good explanation of the same issue here on this thread as to why this is failing (Creating HttpURLConnection for URLStreamHandlerFactory, getting Error: protocol doesn't support input).

The book is referenced here as well (https://books.google.de/books?id=LXsgAQAAQBAJ&lpg=PT285&ots=H-snX4lzGe&dq=Java+should+I+subclass+URLStreamHandler&pg=PT286&redir_esc=y#v=snippet&q=connect()&f=false)

Comments