我正在嘗試使用Reddit API來做一些事情。我有一切工作,但更改頁面和登錄。我無法使用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響應代碼:504網址:http://www.reddit.com/api/login/kagnito/ 在陽光下。 net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
但是我也收到了很多「invalid pa ssword「消息。
我該如何解決這個問題?一直在它幾個小時!
Btw。這是我無法理解的:https://github.com/reddit/reddit/wiki/API%3A-login 我不知道如何發佈這個?它應該進入標題,還是? 我不熟悉HTTP協議。 (因此我的項目 - 我在學習)
檢查更新到我的回答 – Strelok