0

When reading the reply after posting information to a webpage I am getting a strange issue. The console prints "success" but the check fails in the code and false is returned at the end instead of true when the check happens.

The php script for the page that this is hitting is as follows:

<?php
    echo "success";
?>

Eventually it will take in posted information, but for right now I just want to use it for my application to see if it can connect to its target.

public static boolean checkConnect(String cURL) {
    try{
        URLConnection connect = new URL(cURL).openConnection();
        connect.setDoOutput(true);
        connect.setRequestProperty("Accept-Charset", charset);
        connect.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);

        OutputStreamWriter wr = new OutputStreamWriter(connect.getOutputStream());
        wr.write("this is a test");
        wr.flush(); 
        // Get the response 
        BufferedReader rd = new BufferedReader(new InputStreamReader(connect.getInputStream())); 
        String line; 
        String results = "";
        while ((line = rd.readLine()) != null) {
            results += line;
        } 

        //prints "success" to console.
        System.out.println(results.trim());

        //fails
        if (results.trim() =="success")
            return true;
        wr.close(); 
        rd.close(); 
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;

}
James
  • 110
  • 9

1 Answers1

0

In Java the String is an object. To compare a String with something you have to use the equals method.

If(results.trim().equals("success")){}
Yassine.b
  • 617
  • 5
  • 11