0

I have a small problem here. I'm running a Discord-bot with multiple commands on my PI which worked out completly fine so far.
Today I was planning on implementing a new command for checking a game server's status which requires URLConnection. As usual I created a fat jar with all the libraries etc. and tested it on my PC (Windows 10) which worked out fine.
When I ran the program on my PI it didn't show me any errors at the start of the program but as soon as I ran the new command it always threw me an exception (marked it in the code).
I have no clue how to fix this issue, please help me.

Here's my code:

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Timer;
import java.util.TimerTask;
import main.permsMain;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;

public class cmdisonline implements Commands {

public void action(final String[] args, MessageReceivedEvent event) {
    if((args.length == 3)) { 
        switch(args[2]) {
        case "-p":
            if(permsMain.check(event)) {
            try {
                String input = new String(args[0]);
                String webPage = new String("http://arkservers.net/server/"+input);
                URL url = new URL(webPage);
                URLConnection urlConnection = url.openConnection();
                InputStream is = urlConnection.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);

                System.out.println("test"); //it doesn't write this into console

                int numCharsRead;
                char[] charArray = new char[1024];
                StringBuffer sb = new StringBuffer();
                while ((numCharsRead = isr.read(charArray)) > 0) {
                    sb.append(charArray, 0, numCharsRead);
                }
                String result = sb.toString();
                boolean foundPumbaa = false;
                System.out.println("[Info] URL: "+ url);

                if(!result.contains("Server Not Found")) {
                    if (result.contains("Last Offline")) {
                        foundPumbaa = true;
                        System.out.println("[Info] "+ input +" is online."); 
                        event.getChannel().sendMessage("**Server __"+ args[1] +"__ is online!.** :white_check_mark:").queue();;                                 
                }
                if (!foundPumbaa) {
                    System.out.println("[Info] "+ input +" is offline.");
                    event.getChannel().sendMessage("**__"+args[1]+"__ is either ghosting or down.**:x:\nIf you are sure that the server is ghosting then please use the **!ping** command for any further actions.").queue();
                }
                } else {
                    System.out.println("[Info] "+ input +" not found.");
                    event.getChannel().sendMessage("**Couldn't resolve hostname '"+ args[0] +"'.**\nPlease make sure you include the port and only use IP's from http://arkservers.net").queue();
                }

            } catch(Exception ServerNotFound) { //Throws me this Exception
                event.getChannel().sendMessage("**Error: Couldn't resolve hostname '"+ args[0] +"'.**\nPlease make sure you include the port and only use IP's from http://arkservers.net").queue();
                ServerNotFound.printStackTrace();
            }
            }
            break;
    default:
        event.getChannel().sendMessage("Usage: **!isonline <IP> <Name> <Parameter>** \nPlease make sure to include the port.").queue();
        break;
    }
    } else {
        event.getChannel().sendMessage("Usage: **!isonline <IP> <Name> <Parameter>** \nPlease make sure to include the port.").queue();
    }
}

EDIT: Exception Log

java.io.IOException: Server returned HTTP response code: 403 for URL: http://arkservers.net/server/85.190.158.37:27017
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1838)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at commands.cmdisonline.action(cmdisonline.java:86)
at main.commandHandler.handleCommand(commandHandler.java:17)
at listeners.commandListener.onMessageReceived(commandListener.java:13)
at net.dv8tion.jda.core.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:403)
at net.dv8tion.jda.core.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:84)
at net.dv8tion.jda.core.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:122)
at net.dv8tion.jda.core.handle.SocketHandler.handle(SocketHandler.java:37)
at net.dv8tion.jda.core.requests.WebSocketClient.handleEvent(WebSocketClient.java:1051)
at net.dv8tion.jda.core.requests.WebSocketClient.onTextMessage(WebSocketClient.java:691)
at net.dv8tion.jda.core.requests.WebSocketClient.onBinaryMessage(WebSocketClient.java:1099)
at com.neovisionaries.ws.client.ListenerManager.callOnBinaryMessage(ListenerManager.java:368)
at com.neovisionaries.ws.client.ReadingThread.callOnBinaryMessage(ReadingThread.java:270)
at com.neovisionaries.ws.client.ReadingThread.handleBinaryFrame(ReadingThread.java:990)
at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:749)
at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:108)
at com.neovisionaries.ws.client.ReadingThread.runMain(ReadingThread.java:64)
at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThread.java:45)
xFish
  • 23
  • 6

1 Answers1

0

The Server sends a 403 Forbidden Error, if there is no User-Agent header. So we have to add one. To do this, we add it to the connection after creating it, like this.

con.setRequestProperty("User-Agent", "");

In this case the content does not matter, because the server does not check it.

Alexander Daum
  • 741
  • 4
  • 14