Why does my httpurlconnection throw a 500 error?

297 Views Asked by At

I'm trying to get my game server to link up with my Xenforo forums but it pulls a 500 error when trying to connect. Its definitely reading the databases as i can type an incorrect username when logging in and it will tell me to register. But if i use a username that exists it throws the error.

This is the error in my Central server console :

java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost//extra/xenforo/index.php
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1900)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
at io.ruin.api.utils.PostWorker.post(PostWorker.java:65)
at io.ruin.api.utils.PostWorker.postArray(PostWorker.java:82)
at io.ruin.api.utils.XenPost.post(XenPost.java:14)
at io.ruin.central.model.world.WorldLogin.lambda$new$0(WorldLogin.java:33)
at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1626)
at java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1618)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

I've tried messing with the post string but can't get it to connect keeps catching IOException E

public class PostWorker {

    public static String post(String url, Map<Object, Object> postMap) {
        HttpURLConnection con = null;
        try {
            con = (HttpURLConnection) new URL(url).openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setConnectTimeout(5000);
            con.setRequestProperty("User-Agent", "Mozilla/5.0");
            con.setRequestProperty("Connection","Keep-Alive");

            try(PrintStream ps = new PrintStream(con.getOutputStream())) {
                boolean first = true;
                for(Map.Entry<Object, Object> post : postMap.entrySet()) {
                    String key = URLEncoder.encode(""+post.getKey(), "UTF-8");
                    String value = URLEncoder.encode(""+post.getValue(), "UTF-8");
                    ps.print((first ? "" : "&") + key + "=" + value);
                    first = false;
                }
            }

            try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
                StringBuilder response = new StringBuilder();
                String line;
                while((line = br.readLine()) != null)
                    response.append(line);
                return response.toString();
            }
        } catch(IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if(con != null)
                con.disconnect();
        }
    }

    public static String post(String url, byte[] data) {
        HttpURLConnection con = null;
        try {
            con = (HttpURLConnection) new URL(url).openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setConnectTimeout(5000);
            con.setRequestMethod("POST");
            con.setRequestProperty("User-Agent", "Mozilla/5.0");
            con.setRequestProperty("Connection","Keep-Alive");
            con.setRequestProperty("Content-Length", Integer.toString(data.length));

            try(DataOutputStream out = new DataOutputStream(con.getOutputStream())) {
                out.write(data);
            }

            try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
                StringBuilder response = new StringBuilder();
                String line;
                while((line = br.readLine()) != null)
                    response.append(line);
                return response.toString();
            }
        } catch(IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if(con != null)
                con.disconnect();
        }
    }

    public static String postArray(String url, Map<Object, Object> map) {
        return post(url, JsonUtils.toJson(map).getBytes());
    }

}

Above is the java code which uses the HTTPurlconnection.

127.0.0.1 - - [13/Aug/2020:19:06:22 -0400] "POST /extra/xenforo/index.php HTTP/1.1" 500 1794 "-" "Mozilla/5.0"

Above is what the apache access.log posts.

My friend hosts the same server same files and has no problem connecting. I'd also like to note i'm using Xampp to host the forum files not sure if that effects anything.

Thanks if anybody can help :)

0

There are 0 best solutions below