I want to create a Login Form in Swing JAVA and when the user clicks on the submit button it should use a GET call to GET the user information from a URL.
My login form is having two fields:
username:
password:
and “submit” button
I have used the following code. I am working in NetBeans 7.1.2 .I new to Java.
public void actionPerformed(ActionEvent ae)throws IOException
{
String value1=text1.getText();
String value2=text2.getText();
URL url = new URL("http://link? userName=value1&password=value2");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true); httpCon.setRequestMethod("GET");
OutputStreamWriter out = new OutputStreamWriter( httpCon.getOutputStream());
System.out.println(httpCon.getResponseCode());
System.out.println(httpCon.getResponseMessage());
out.close();
}
but it is not working .
Can anyone please tell me where I am wrong OR can give me better way to do this.
* @author tapasweni
*/
class Login extends JFrame implements ActionListener
{
I am using this but now I am getting error code 404 whereas if I typed the URL in the browser it is giving me the correct result.
This is because you coded the litterals “value1” and “value2” and not their variables in the URL. Try this instead:
Note that you should use a URLEncoder (with charset UTF-8) to encode those values.