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

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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));
}
}

9 Comments

The javadoc (found here: link) is very vague. I've tried different protocols and none worked.
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?
Is there a way to find out what type of protocol it is? And also how would I open InputStream over TCP?
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.
Sorry I used the wrong parameter in the URL the correct link is here maple.fm/api/2/search?server=1.
|
-4

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();

1 Comment

I'm already throwing the exception in the method so that shouldn't be the problem here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.