I'm trying to post two strings on a PHP web server. First, I use this relevant part of my PHP script, which does work in other projects with other languages.
function open($db){
$name = $_POST['name'];
$pwd = $_POST['pwd'];
// Prepare SQL statement
$statement = $db->prepare("SELECT * FROM `table` WHERE `name`=?");
$statement->bind_param("s", $name);
$statement->execute();
$result = $statement->get_result();
$row = mysqli_fetch_assoc($result);
if($result->num_rows > 0){
$hashed_pwd = $row['pwd'];
}
else{
echo 'No result!';
$statement->close();
return;
}
if(password_verify($pwd, $hashed_pwd)){
echo 'Success!';
}
else {
echo 'Wrong password!';
}
// Close SQL Statement
$statement->close();
}
In Java, I face multiple problems. The function gets called, when pressing the “Open” button:
public void onOpenClick(View v){
final String name = "someString";
final String pwd = "someString";
new Thread(new Runnable() {
@Override
public void run() {
try {
//Add the action to the original URL
urlString += "?action=open";
URL url = new URL(urlString);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
String info = "name=" + name + "pwd=" + pwd;
con.setRequestMethod("POST");
con.setDoOutput(true);
con.getOutputStream().write(info.getBytes("UTF-8"));
con.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
answer = in.readLine();
}catch (Exception e){
System.out.println(e.toString());
answer = e.toString();
}
}
}).start();
}
Is using a Thread the correct way to make a PHP requests? Furthermore, I always get as answer “No result,” and if I change the echo statement in the PHP file to $name. nothing returns at all, In conclusion, the connection to my server is successful. the POST statement is not, I tried different approaches. none of them worked, If I press the button again. I do not get any response at all and answer is null or empty.
Edit
I tried adding following statements as well:
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("charset", "utf-8");
And:
StringBuilder postData = new StringBuilder();
postData.append(URLEncoder.encode("name", "UTF-8"));
postData.append("=");
postData.append(URLEncoder.encode(name, "UTF-8"));
postData.append(URLEncoder.encode("pwd", "UTF-8"));
postData.append("=");
postData.append(URLEncoder.encode(pwd, "UTF-8"));
byte[] postDataBytes = postData.toString().getBytes(StandardCharsets.UTF_8);
con.setRequestMethod("POST");
con.setInstanceFollowRedirects(false);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("charset", "utf-8");
con.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
con.setDoOutput(true);
con.setUseCaches(false);
I had the same issue when trying to call a php server from javascript. I figured out I needed to add the Content-Type
header correctly.
'Content-Type': 'application/x-www-form-urlencoded'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.