Variablen(inhalt) Abgleich Java und PHP schlägt fehl

Status
Nicht offen für weitere Antworten.

pixel-shock

Aktives Mitglied
Hi ,

ich habe schon wieder eine Frage. ;)

Ich habe eine Klasse Cookie, mit der man Cookies setzen und lesen kann - mittels PHP

Das geht auch soweit.

- Wenn kein Cookie gesetzt ist, dann gibt mir php "notSet" zurück.
- Java bekommt das auch, aber bei der Abfrage, ob mein Java String "data" "notSet" ist, überspringt er einfach. :bahnhof:

Code meines Applets:

Code:
Cookie c = new Cookie("http://www.meineURL/cookie.php");
		try {
			String data = c.getCookie().trim();
			while (c.hasData() == false) {
				System.out.println("warte auf cookie...");
			};
			if (data == "notSet") {
				loginName.setText("Username");
				loginPass.setText("Passwort");	
				System.out.println("Kein Cookie vorhanden");
			}
		} catch (IOException e1) {}

Code der Cookie-Klasse:

Code:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Cookie {
	
	private URL url;
	private HttpURLConnection urlc;
	private InputStream is;
	private String data = null;

	public Cookie(String link) {
		try {
			this.url = new URL(link);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
	
	public String getCookie() throws IOException {
		try {
			urlc = (HttpURLConnection)url.openConnection();
			urlc.setRequestMethod("POST");
			urlc.setUseCaches(false);
			is = urlc.getInputStream();
		} catch (IOException e) {e.printStackTrace();}		
		
		int c = 0;		
		StringBuffer incoming = new StringBuffer();
		
		 while (c >= 0) {
			 c = is.read();
			 incoming.append((char) c);
		 }
		 data = incoming.toString().substring(0,incoming.toString().length()-1);
		 return (data);
	}
	
    public void send(String data) throws IOException {
        if (urlc == null) {
        	urlc = (HttpURLConnection)url.openConnection();
        }
        if (urlc.getDoOutput() == false) {
        	urlc.setDoOutput(true);
        }

        OutputStream out = urlc.getOutputStream();
        out.write(data.getBytes());
        out.flush();

    }
    
    public boolean hasData() {
    	if (data != null) {
    		return true;
    	} else {
    		return false;
    	}
    }
}

Ich verstehe das nicht ganz - er gibt mir ja auch "notSet" in der Konsole aus :(

Lg
Tino
 

pixel-shock

Aktives Mitglied
Kann es sein, dass bei data noch nen Zeilenende oder ein Null-Byte dranhängt, wenn ich diesen hier teste:

Code:
if (data.matches("notSet")) {
				loginName.setText("Username2");
				loginPass.setText("Passwort");	
				System.out.println("Kein Cookie vorhanden");
			}

dann geht es, aber ist eher eine unschöne Lösung denke ich.
Hab jetzt schon ne Weile gesucht, wie ich das Null-Byte oder Zeilenende weg bekomme, aber nichts gefunden.

LG
Tino
 

SebiB90

Top Contributor
Code:
if (data == "notSet") {
das ist nicht dein ernst oder?
du willst schon so "kompliziertes" machen und du weißt nicht wie man Strings vergleicht?
bei Strings immer equals benutzen, also
Code:
if(data.equals("notSet")){
 

pixel-shock

Aktives Mitglied
Hi

zu jetzt ist's mir peinlich ;)
Auf equals bin ich echt nicht gekommen.

DANKE DIR!

Sodale ich geh mal meinen Kopf vor die Wand schlagen ;)

LG & schönen WE wünsche ich dir :)

LG
Tino
 

pixel-shock

Aktives Mitglied
Ich habe da nochmal eine kleine Frage bzw. ein Problem, was ich nicht ganz nachvollziehen kann:

Also das Lesen der von PHP ausgegebenen Daten funktioniert prinzipiell, aber wenn ich das hier ausgeben lasse:

Code:
echo trim($_COOKIE['patlgcookie']);

kommt in Java nur ein "?" an (der Browser gibt es korrekt aus), wenn ich hingegen ausgeben lasse:

Code:
echo "testblublubblub";

dann kommt auch in java alles korrekt an.

Das kann ich mir nicht erklären, habe die Suchfunktion des Forum schon sehr ausgiebig genutzt, aber das Problem hatte bisher noch keiner.

LG
Tino
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben