我正在尝试使用 Reddit API 来做一些事情。除了更改页面和登录之外,我一切正常。
我需要登录才能使用我的程序,我知道如何使用我得到的 cookie,但我就是无法登录。
这是代码:
public static Login POST(URL url, String user, String pw) throws IOException
{
String encodedData = URLEncoder.encode("api_type=json&user=" + user +"&passwd="+pw, "UTF-8");
HttpURLConnection ycConnection = null;
ycConnection = (HttpURLConnection) url.openConnection();
ycConnection.setRequestMethod("POST");
ycConnection.setDoOutput(true);
ycConnection.setUseCaches (false);
ycConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
PrintWriter out = new PrintWriter(ycConnection.getOutputStream());
out.print(encodedData.getBytes());
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(ycConnection.getInputStream()));
String response = in.readLine();
Map<String, List<String>> headers = ycConnection.getHeaderFields();
List<String> values = headers.get("Set-Cookie");
String cookieValue = null;
for (java.util.Iterator<String> iter = values.iterator(); iter.hasNext(); ) {
String v = iter.next();
if (cookieValue == null)
cookieValue = v;
else
cookieValue = cookieValue + ";" + v;
}
return new Login(cookieValue, response);
}
我得到的最典型的例外是:
java.io.IOException:服务器返回 HTTP 响应代码:URL 504:http ://www.reddit.com/api/login/kagnito/ at sun.net.www.protocol.http.HttpURLConnection.getInputStream(未知来源)
但我也收到了很多“无效密码”的消息。
我该如何解决这个问题?坚持了几个小时!
顺便提一句。这就是我无法理解的内容:https ://github.com/reddit/reddit/wiki/API%3A-login 我不知道如何发布这个?它应该进入标题,还是?我对 HTTP 协议不是很熟悉。(因此我的项目 - 我正在学习)