如何在Java中发送Https Post请求

我想从Java代码login到应用程序。 这是我的代码…

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 (compatible; 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()); 

但我无法login,它只返回login页面。

如果有人可以,请帮助我了解我做错了什么。

错误: – (额外的空间在wwwforms中间)

 con.setRequestProperty("Content-Type","application/x-www- form-urlencoded"); 

正确

  con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");