2

I'm trying to open an InputStream to a certain URL as given by the service's API. However, it does not have a set protocol (it's not http or https) and without one, I am getting the following error.

Is there any way to get a request without a protocol?

Exception:

Exception in thread "main" java.net.MalformedURLException: no protocol.

Code:

    String url = "maple.fm/api/2/search?server=1";
    InputStream is = new URL(url).openStream();

UPDATE: I now updated the code to:

Code:

    String url = "http://maple.fm/api/2/search?server=1";
    InputStream is = new URL(url).openStream();

and now I'm getting the following error:

Exception:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://maple.fm/api/2/search?server=1

Robert Lu
  • 441
  • 2
  • 7
  • 18

3 Answers3

3

A URL without a protocol is not a valid URL. It is actually a relative URI, and you can only use a relative URI if you have an absolute URI (or equivalent) to provide the context for resolving it.

Is there any way to [make] a request without a protocol?

Basically .... No. The protocol tells the client-side libraries how to perform the request. If there is no protocol, the libraries would not know what to do.

The reason that "urls without protocols" work in a web browser's URL bar is that the browser is being helpful, and filling in the missing protocol with "http:" ... on the assumption that that is what the user probably means. (Plus a whole bunch of other stuff, like adding "www.", adding ".com", escaping spaces and other illegal characters, ... or trying a search instead of a normal HTTP GET request.)

Now you could try to do the same stuff in your code before passing the URL string to the URL class. But IMO, the correct solution if you are writing code to talk to a service is to just fix the URL. Put the correct protocol on the front ...


The 403 error you are now getting means Forbidden. The server is saying "you are not permitted to do this".

Check the documentation for the service you are trying to use. (Perhaps you need to go through some kind of login procedure. Perhaps what you are trying to do is only permitted for certain users, or something.)

Try the example URL on this page ... which incidentally works for me from my web browser.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

When you say it does not have a set protocol, I am a little bit suspicious of what that means. If it can use multiple protocols, I would hope the API documentation mentions some way of determining what the protocol should be.

I hit the URL http://maple.fm/api/2/search?server=1 and it is simply returning JSON over http. I think your actual problem is that you are trying to open an InputStream to talk to a web server. I believe the solution to your problem, of trying to handle JSON over http, can be found here.

I decided to dig into this because I was curious. Combining this answer and this answer, we have the following code which will print out the JSON output from your URL. Of course, you still need a JSON library to parse it, but that's a separate problem.

import java.net.*;
import java.io.*;

public class Main{ 
 public static String getHTML(String urlToRead) {
      URL url;
      HttpURLConnection conn;
      BufferedReader rd;
      String line;
      String result = "";
      try {
         url = new URL(urlToRead);
         conn = (HttpURLConnection) url.openConnection();
         conn.setRequestMethod("GET");
         conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
         rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
         while ((line = rd.readLine()) != null) {
            result += line;
         }
         rd.close();
      } catch (IOException e) {
         e.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return result;
   }
public static void main(String[] args) {
    String url = "http://maple.fm/api/2/search?server=1";
    System.out.println(getHTML(url));
}
}
Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • The javadoc (found here: [link](http://maple.fm/api)) is very vague. I've tried different protocols and none worked. – Robert Lu Mar 30 '14 at 05:33
  • Is it some kind of raw socket protocol? If you are creating an `InputStream`, then maybe you actually just want to create a socket and talk to it over `TCP` directly? – merlin2011 Mar 30 '14 at 05:34
  • Is there a way to find out what type of protocol it is? And also how would I open InputStream over TCP? – Robert Lu Mar 30 '14 at 05:35
  • When I hit your URL from my browser, I get the output: `{"fm_items":[],"seconds_ago":null}`. Is that the desired output? If so, it looks like `http` to me. – merlin2011 Mar 30 '14 at 05:36
  • Sorry I used the wrong parameter in the URL the correct link is here maple.fm/api/2/search?server=1. – Robert Lu Mar 30 '14 at 05:39
  • That also provides what looks like valid output in my browser. I'm going to update my answer. – merlin2011 Mar 30 '14 at 05:41
  • UPDATE: I used the following URL for the InputStream (http://maple.fm/api/2/search?server=1) and got a new error. I will update my original question. – Robert Lu Mar 30 '14 at 05:42
  • Ahh. I think that may be the problem here too. I will try out your suggestion and see if that solves it. Thanks! – Robert Lu Mar 30 '14 at 05:44
  • I just tried it and there's no error showing up. I'm guessing it worked but I will resume tomorrow. Thanks for the help. – Robert Lu Mar 30 '14 at 06:03
-3

you need to surround it with a try/catch block

try {
    String url = "maple.fm/api/2/search?world=1";
    InputStream is = new URL(url).openStream();
catch(MalformedURLException e) {
    e.printStackTrace();
Snoop Dogg
  • 62
  • 3