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 :)
Related
I'm trying to connect to GDAX using their REST API.
I first want to do something very simple, i.e. getting historic rates.
I tried this:
private static final String GDAX_URL = "https://api.gdax.com";
public String getCandles(final String productId, final int granularity) {
HttpsURLConnection connection = null;
String path = "/products/" + productId + "/candles";
try {
//Create connection
URL url = new URL(GDAX_URL);
connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("granularity", String.valueOf(granularity));
connection.setUseCaches(false);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(path);
wr.close();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuffer response = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (connection != null) {
connection.disconnect();
}
}
return null;
}
But I get a 400 code in return "Bad Request – Invalid request format".
My problem is with the passing of the path "/products//candles" and the parameters (e.g. granularity).
I don't understand what should go in the request properties and in the message itself, and in what form.
I managed to make it work like this:
URL url = new URL(GDAX_URL + path + "?granularity="+granularity);
connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json");
Not sure how to use the DataOutputStream, so I just removed it. At least it works.
So I'm trying to connect to our database via Xserve, AT the moment I'm trying to access the token for the user. I'm using the correct username and password along with the context type and grant type; I know this because I've tried the same POST method via googles postmaster extension. For whatever reason when I try the same thing on Android, at least what I think is the same, it gives me a 400 response code and doesn't return anything.
Here's the code used to connect:
private HttpURLConnection urlConnection;
#Override
protected Boolean doInBackground(Void... params) {
Boolean blnResult = false;
StringBuilder result = new StringBuilder();
JSONObject passing = new JSONObject();
try {
URL url = new URL("http://xserve.uopnet.plymouth.ac.uk/modules/INTPROJ/PRCS251M/token");
// set up connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8" );
urlConnection.setRequestMethod("POST");
urlConnection.connect();
// set up parameters to pass
passing.put("username", mEmail);
passing.put("password", mPassword);
passing.put("grant_type", "password");
// add parameters to connection
OutputStreamWriter wr= new OutputStreamWriter(urlConnection.getOutputStream());
wr.write(passing.toString());
// If request was good
if (urlConnection.getResponseCode() == 200) {
blnResult = true;
BufferedReader reader = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
reader.close();
}
//JSONObject json = new JSONObject(builder.toString());
Log.v("Response Code", String.format("%d", urlConnection.getResponseCode()));
Log.v("Returned String", result.toString());
}catch( Exception e) {
e.printStackTrace();
}
finally {
urlConnection.disconnect();
}
return blnResult;
}
I haven't stored the result into the JSONObject yet as I'll use that later, but I expected some kind of output via the "Log.v".
Is there anything that stands out?
try {
URL url = new URL("http://xserve.uopnet.plymouth.ac.uk/modules/INTPROJ/PRCS251M/token");
parameters = new HashMap<>();
parameters.put("username", mEmail);
parameters.put("password", mPassword);
parameters.put("grant_type", "password");
set = parameters.entrySet();
i = set.iterator();
postData = new StringBuilder();
for (Map.Entry<String, String> param : parameters.entrySet()) {
if (postData.length() != 0) {
postData.append('&');
}
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
postDataBytes = postData.toString().getBytes("UTF-8");
// set up connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
urlConnection.setRequestMethod("POST");
urlConnection.getOutputStream().write(postDataBytes);
// If request was good
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
reader = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
reader.close();
}
Log.v("Login Response Code", String.valueOf(urlConnection.getResponseCode()));
Log.v("Login Response Message", String.valueOf(urlConnection.getResponseMessage()));
Log.v("Login Returned String", result.toString());
jsonObject = new JSONObject(result.toString());
token = jsonObject.getString("access_token");
} catch (Exception e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
if (token != null) {
jsonObject = driverInfo(token);
}
}
this works, although I've moved it to it's own function now.
changed the input type to a HashMap
I am facing a problem while using POST Method in Java nowadays. I am receiving
Exception in thread "main" java.lang.RuntimeException: Server returned HTTP response code: 411 for URL.
I couldn't find any available document anywhere. None of them were useful. How do I fix it?
My code
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class req {
public static void main(String[] args) {
sendPostRequest(requestURL);
}
private static String sendPostRequest(String requestUrl) {
StringBuilder jsonString = new StringBuilder();
try {
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
byte[] data = requestUrl.getBytes("UTF-8");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setRequestProperty("Content-Length", String.valueOf(data.length));
connection.setRequestProperty("Authorization", "Basic " + "usename:password");
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
br.close();
connection.disconnect();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return jsonString.toString();
}
}
Perfectly working method:
public String sendPostRequest(String requestURL, HashMap<String, String> postDataParams) {
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
response = br.readLine();
}
else {
response="Error Registering";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
If you are returning a JSON in your response:
public JSONObject getPostResult(String json){
if(!json.isEmpty()) {
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON_ERROR", "Error parsing data " + e.toString());
}
}
return jObj;
}
If you are still having trouble, maybe this will help. I did not test, machine does not have java installed.
You should also set all other headers that you need.
public static String PostRequest(String requestUrl, String username, String password) {
StringBuilder jsonString = new StringBuilder();
HttpURLConnection connection = null;
try {
URL url = new URL(requestUrl);
connection = (HttpURLConnection)url.openConnection();
byte[] authData = Base64.encode((username + password).getBytes());
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + new String(authData));
connection.setRequestProperty("Content-Length", String.valueOf(authData.length));
try (DataOutputStream writer = new DataOutputStream(connection.getOutputStream())) {
writer.writeBytes("REPLACE ME WITH DATA TO BE WRITTEN");
writer.flush();
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String data = null;
while ((data = reader.readLine()) != null) {
jsonString.append(data);
}
}
} catch (IOException ex) {
//Handle exception.
} finally {
if (connection != null)
connection.disconnect();
}
return jsonString.toString();
}
You should send empty data in body if you are using post method.
For example if you are using json data you need to send "{}"
public void Post() throws Exception {
StringBuffer d = new StringBuffer();
String da = "ClearanceDate=2020-08-31&DepositeDate=2020-08-31&BankTransactionNo=UATRYU56789";
URL url = new URL("https://abcd/AddReceipt?" + da);
byte[] postDataBytes = ("https://abcd/AddReceipt?" + da).toString()
.getBytes("UTF-8");
System.out.println("Data--" + postDataBytes);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
// con.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");
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
con.setRequestProperty("Content-Length",
String.valueOf(postDataBytes.length));
con.setRequestProperty(
"Authorization",
"Bearer "
+ "o731WGgp1d913ZOYivnc55yOg0y1Wk7GsT_mnCUKOJf1VChYOdfRjovAxOhyyPKU93ERue6-l9DyG3IP29ObsCNTFr4lGZOcYAaR96ZudKgWif1UuSfVx4AlATiOs9shQsGgb1oXN_w0NRJKvYqD0LLsZLstBAzP1s5PZoaS9c6MmO32AV47FUvxRT6Tflus5DBDHji3N4f1AM0dShbzmjkBCzXmGzEDnU6Jg1Mo5kb884kParngKADG5umtuGbNzChQpMw_A0SyEYaUNh18pXVmnNhqM3Qx5ZINwDEXlYY");
con.setRequestProperty("Accept", "application/json");
con.setDoInput(true);
con.setDoOutput(true);
con.getOutputStream().write(postDataBytes);
int status = con.getResponseCode();
System.out.println("Response status: " + status + "|"
+ con.getResponseMessage());
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
con.disconnect();
System.out.println("Response status: " + status);
System.out.println(content.toString());
System.out.print("Raw Response->>" + d);
}
I am sending Https request with username and password through 2 ways but in both the cases I am getting Error 401- Unauthorized error
direct connection through URLConnection
try {
URL url = new URL("https://xxx.xxxxx.com/v1/limit/5");
URLConnection urlConnection = url.openConnection();
HttpsURLConnection connection = null;
if(urlConnection instanceof HttpsURLConnection) {
String authStr = "username"+":"+"password";
byte[] bytesEncoded = Base64.encodeBase64(authStr.getBytes());
String authEncoded = new String(bytesEncoded);
connection = (HttpsURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "Basic "+authEncoded);
} else {
System.out.println("Please enter an HTTPs URL.");
return;
}
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String urlString = "";
String current;
while((current = in.readLine()) != null) {
urlString += current;
}
System.out.println(urlString);
} catch(IOException e) {
e.printStackTrace();
}
Connection through Jersey Client but in the same way I am getting 401-unauth error
Following piece of code was working for the last three years, but all of a sudden it throws connection timed out only in server, but works as intended in localhost.
Any comments ?
public String getWikiContent(String query) {
StringBuilder builder = new StringBuilder();
String path = "https://en.wikipedia.org/w/api.php?action=query&prop=extracts&exintro=1&explaintext=1&titles=" + query + "&format=json&redirects";
try {
URL url = new URL(path);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestProperty("Content-Type",
"application/json");
if (urlConn.getResponseCode() != 200) {
throw new IOException(urlConn.getResponseMessage());
}
InputStream is = urlConn.getInputStream();
BufferedReader buff = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = buff.readLine()) != null) {
builder.append(line);
}
}catch (IOException e){
e.printStackTrace();
}
return builder.toString();
}
For some reasons its a network issue, just adding a proxy server fixed it.
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(url,port));
URLConnection urlConn = url.openConnection(proxy);