gpt4 book ai didi

java - 如何在 java 中发送 Https Post 请求

转载 作者:IT老高 更新时间:2023-10-28 20:54:06 25 4
gpt4 key购买 nike

我想从 java 代码登录到应用程序。这是我的代码...

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

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

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

最佳答案

错误:-(www-form中间有多余的空格)

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

正确

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

关于java - 如何在 java 中发送 Https Post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6927427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com