I have a problem. I have the following PHP page:
<?php
header('Content-Type: application/json');
echo $_POST["agentid"];
?>
And my Java code is the following:
public String callWebpage(String strUrl, HashMap<String, String> data) throws InterruptedException, IOException {
var objectMapper = new ObjectMapper();
String requestBody = objectMapper
.writeValueAsString(data);
URL url = new URL(strUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.getOutputStream().write(requestBody.getBytes("UTF-8"));
String result = convertInputStreamToString(con.getInputStream());
return result;
}
To call the function, I use the following lines:
var values = new HashMap<String, String>() {{
put("agentid", String.valueOf(agentId));
}};
String jsonResponse = webAPI.callWebpage("https://www.test.org/test.php", values);
This does print the result of the page, but it gives me:
2021-02-28 00:40:16.735 Custom error: [8] Undefined index: agentid<br>2021-02-28 00:40:16.735 Error on line 5
The page is HTTPS and I do get a response, but why is my agentid variable not getting received by the page and how can I fix this?