Solved IOException (Server returned HTTP response code 403)

Discussion in 'Spigot Plugin Development' started by derp1337, May 17, 2018.

  1. I'm trying to figure out what's wrong with my code that queries IPHub's JSON api so that I can get the ISP (I'm going to use it to momentarily block ISPs for about an hour or two if someone attempts to, say, DDOS or crash the server by sending packets)

    The stacktrace I get when running the command is this:


    Code (Text):
    [14:58:12 WARN]: java.io.IOException: Server returned HTTP response code: 403 for URL: http://v2.api.iphub.info/ip/1 //My IP
    [14:58:12 WARN]:        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    [14:58:12 WARN]:        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    [14:58:12 WARN]:        at java.net.URLConnection.getContent(Unknown Source)
    [14:58:12 WARN]:        at com.unityip.util.Tools.ipHubContact(Tools.java:234)
    [14:58:12 WARN]:        at com.unityip.util.Tools$2.run(Tools.java:162)
    [14:58:12 WARN]:        at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftTask.run(CraftTask.java:71)
    [14:58:12 WARN]:        at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:52)
    [14:58:12 WARN]:        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    [14:58:12 WARN]:        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    [14:58:12 WARN]:        at java.lang.Thread.run(Unknown Source)
    My method for attempting is this:


    Code (Text):
        public void ipHubContact(String IP, CommandSender commandSender) {
            try {
            String site = ("http://v2.api.iphub.info/ip/" + IP);

            JsonParser parser = new JsonParser();
            JsonElement object = parser.parse(checker);**/
            URL url = new URL(site);
            HttpURLConnection request= (HttpURLConnection) url.openConnection();
            request.setRequestProperty("X-Key", xkey);
           
            JsonParser jp = new JsonParser();
            JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //Convert the input stream to a json element
            JsonObject obj = root.getAsJsonObject();
            String object = obj.get("ISP").getAsString();
                if(commandSender instanceof ConsoleCommandSender) {
                    System.out.println("ISP is " + object);  
                } else {
                    commandSender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&9[&3&lUIP&9] &bReturned ISP:&3 " + object));
                }
               
               
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           
        }
    The line (230) is this one
    Code (Text):
            HttpURLConnection request= (HttpURLConnection) url.openConnection();
     
  2. Double check your x-key? Like debug print it. 403 is forbidden error code.
     
  3. I was going to ask if you're sure that xkey is set to something... but it seems like your exception is getting thrown before that's even set? I'm not really sure how, I thought the request would not really be sent until you call getContent.
     
  4. I regenerated my API Key and it's working without IOException now - I just have an NPE on this line now

    Code (Text):

    commandSender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&9[&3&lUIP&9] &bReturned ISP:&3 " + object));

     
    Which would probably be how I handled sending the ISP
     
    #4 derp1337, May 17, 2018
    Last edited: May 17, 2018
  5. I was incorrect - tried sending from both player POV & Console, didn't work. Full stacktrace is:


    Code (Text):
    java.lang.NullPointerException
            at com.unityip.util.Tools.ipHubContact(Tools.java:235) ~[?:?]
            at com.unityip.util.Tools$2.run(Tools.java:162) ~[?:?]
            at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot.jar:git-Spigot-6f7aabf-c8ff651]
            at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:52) [spigot.jar:git-Spigot-6f7aabf-c8ff651]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_151]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_151]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_151]
    Which is this line (I did a bit of tinkering to try and fix, so that's why it's different)

    Code (Text):
    commandSender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&9[&3&lUIP&9] &bReturned ISP:&3 " + obj.get("isp").getAsString()));
     
     
  6. I'd try debug printing the root json element- maybe it's an array or some other non-object?
     
  7. Trying to print it didn't work either, just another NPE. I'm assuming you wanted me to do this


    Code (Text):
    commandSender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&9[&3&lUIP&9] &bReturned ISP: &3 " + root));
     
     
  8. Yeah ... that gave an NPE? On that line? That would be really weird unless your commandSender is null :\
     
  9. I mean, I think it's because I try and call the CommandSender back from a HashMap -- I could really just probably add an extra argument to the methods that are inbetween (where I query SQL) to pass on what the CommandSender is

    EDIT: I was trying to be fancy. I shouldn't be trying to be fancy. I made a mistake. Ooops.
     
    • Like Like x 1

Share This Page