Witam,
Chcę napisać prosty program, za pomocą którego mógłbym się poruszać po stronie internetowej.
To jest program, który napisałem, żeby zobaczyć, co odpowie mi strona po wysłaniu odpowiedniego nagłówka.
public class HttpConnection {
public static void main(String[] args) {
URL gatewayURL;
HttpURLConnection gc = null;
DataOutputStream dos = null;
String gatewayStr = "http://www.kaara.pl/updates.php";
String query = null;
query ="POST /updates.php HTTP/1.1 \r\n";
query +="Host: kaara.pl \r\n";
query +="User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; pl; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1 \r\n";
query +="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 \r\n";
query +="Accept-Language: pl,en-us;q=0.7,en;q=0.3 \r\n";
query +="Accept-Encoding: gzip,deflate \r\n";
query +="Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7 \r\n";
query +="Keep-Alive: 300 \r\n";
query +="Connection: keep-alive \r\n";
query +="Referer: http://kaara.pl/ \r\n";
query +="Cookie: PHPSESSID=a7cd220e4bf01af1c8bdaf263cecff5c \r\n";
query +="Content-Type: application/x-www-form-urlencoded \r\n";
query +="Content-Length: 67 \r\n";
query +="email=jezozwierzak%40o2.pl&pass=Wwwiesniak08&submit.x=64&submit.y=23\r\n";
try {
gatewayURL = new URL(gatewayStr);
gc = (HttpURLConnection) gatewayURL.openConnection();
gc.setDoOutput(true);
gc.setDoInput(true);
gc.setAllowUserInteraction(false);
gc.setUseCaches(false);
gc.setRequestMethod("POST");
gc.connect();
dos = new DataOutputStream(gc.getOutputStream());
dos.writeBytes(query);
dos.close();
int code = gc.getResponseCode();
String message = gc.getResponseMessage();
System.out.println(query);
System.out.println(gc.getURL());
System.out.println(code + " " + message);
System.out.println("");
DataInputStream out = new DataInputStream(gc.getInputStream());
BufferedReader inbr = new BufferedReader(new InputStreamReader(out));
String line="";
try {
while ((line = inbr.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("Read/Write Error");
}
gc.disconnect();
} catch (Exception e) {
System.out.println("Blad");
}
}
}
Niestety zamiast przejść na stronę po zalogowaniu, dostaję komunikat, żeby wypełnić wszystkie pola.
W czym tkwi problem?
Przecież podałem parametry wszystkich zmiennych w linijce:
query +="email=jezozwierzak%40o2.pl&pass=Wwwiesniak08&submit.x=64&submit.y=23\r\n";
Proszę ewentualnie o uwagi w jaki efektywniejszy sposób mogę napisać program do wysyłania żądań
Pozdro,
xiom