Witam,
Ostatnio zainteresowałem się protokołem HTTP i zacząłem pisać próbne programy do obsługi tego protokołu.
Stanąłem przy metodzie POST która za nic nie chce się wywołać.
Na XAMPP'owym serwerze zamieściłem testowy plik test.php i gz.php
Oto kod test.php:
<form action="gz.php" method="post" >
<input id="user" name="user" class="text" type="text" value=""/>
<input type="submit" id="login_button" value="Klik!"/>
</form>
Kod gz.php wyświetla tylko wiadomość.
A oto kod w JAVIE jakim się posługiwałem żeby wywołać metodę POST:
class Cx {
public Cx(){
try{
URL url = new URL("http://127.0.0.1/test.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setFollowRedirects(true);
connection.setRequestMethod("POST");
String post = URLEncoder.encode("user=Asdfgh", "UTF-8");
connection.setRequestProperty("User-Agent", "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; pl; rv:1.9.0.13) Gecko/2009073022 Firefox/3.0.13");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-length", String.valueOf(post.length()));
OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
osw.write(post);
osw.close();
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String s = null;
while ((s = br.readLine()) != null) {
System.out.println(s);
}
}catch(Exception ex){System.out.println(ex);}
}
}
W kodzie Javy na pewno jest jakiś błąd ponieważ próbowałem na niektórych stronach internetowych użyć tej metody i tez nie działała.
Czy wie ktoś co jest źle ?
Za pomoc, dzięki :)