I want to login to application from java code. Here is my code...
String httpsURL = "https://www.abcd.com/auth/login/";
String query = "email="+URLEncoder.encode("abc@xyz.com","UTF-8");
query += "&";
query += "password="+URLEncoder.encode("abcd","UTF-8") ;
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-length", String.valueOf(query.length()));
con.setRequestProperty("Content-Type","application/x-www- form-urlencoded");
con.setRequestProperty("User-Agent", "Mozilla/4.0 (c开发者_运维技巧ompatible; MSIE 5.0;Windows98;DigExt)");
con.setDoOutput(true);
con.setDoInput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(query);
output.close();
DataInputStream input = new DataInputStream( con.getInputStream() );
for( int c = input.read(); c != -1; c = input.read() )
System.out.print( (char)c );
input.close();
System.out.println("Resp Code:"+con .getResponseCode());
System.out.println("Resp Message:"+ con .getResponseMessage());
but i can not login, it returns back only login page.
If anybody can, please help me to understand what am i doing wrong.
Wrong :- (Extra space is there in mid of www- form)
con.setRequestProperty("Content-Type","application/x-www- form-urlencoded");
Correct
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
You could be getting a 400 for a few reasons, but usually becaue the data isn't formatted correctly (maybe it takes XML?). Perhaps a record already existed. Try it using postman to see what result you get.
精彩评论