我无法使用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> headers = ycConnection.getHeaderFields(); List values = headers.get("Set-Cookie"); String cookieValue = null; for (java.util.Iterator 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为URL: http : //www.reddit.com/api/login/kagnito/ at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

但我也收到了很多“无效密码”的消息。

我怎么解决这个问题? 已经好几个小时!

顺便说一句。 这是我无法理解的: https : //github.com/reddit/reddit/wiki/API%3A-login我不知道如何发布这个? 它应该进入标题,还是? 我不熟悉HTTP协议。 (因此我的项目 – 我正在学习)

如果不深入研究其余部分可能不起作用的原因,就会出现以下问题:

  • 使用URLEncoder对您的post数据进行编码:您的post数据不会进入URL,因此请勿对其进行编码。

  • 您没有设置Content-Length标头。

以下是您应该开始的内容:

 public static Login POST(URL url, String user, String pw) throws IOException { String data= "api_type=json&user=" + user +"&passwd="+pw; 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"); ycConnection.setRequestProperty("Content-Length", data.length()); PrintWriter out = new PrintWriter(ycConnection.getOutputStream()); out.print(data.getBytes()); out.close(); BufferedReader in = new BufferedReader(new InputStreamReader(ycConnection.getInputStream())); String response = in.readLine(); Map> headers = ycConnection.getHeaderFields(); List values = headers.get("Set-Cookie"); String cookieValue = null; for (java.util.Iterator iter = values.iterator(); iter.hasNext(); ) { String v = iter.next(); if (cookieValue == null) cookieValue = v; else cookieValue = cookieValue + ";" + v; } return new Login(cookieValue, response); } 

使用这样的API时,你应该绝对安装Fiddler ,它是HTTP调试器。 您会立即看到问题,因为您的post数据看起来不像示例。

更新:

这是一个我刚刚进入测试的一些代码,它validation了我很好(显然将myusernamemypassword更改为你的(不要忘记在URL中更改它):

  @Test public void someTest() throws IOException { URL u = new URL( "https://ssl.reddit.com/api/login/myusername" ); login( u, "myusername", "mypassword" ); } public static void login( URL url, String user, String pw ) throws IOException { String data = "api_type=json&user=" + user + "&passwd=" + pw; 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" ); ycConnection.setRequestProperty( "Content-Length", String.valueOf( data.length() ) ); DataOutputStream wr = new DataOutputStream( ycConnection.getOutputStream() ); wr.writeBytes( data ); wr.flush(); wr.close(); InputStream is = ycConnection.getInputStream(); BufferedReader rd = new BufferedReader( new InputStreamReader( is ) ); String line; StringBuffer response = new StringBuffer(); while ( ( line = rd.readLine() ) != null ) { response.append( line ); response.append( '\r' ); } for ( Entry< String, List< String >> r : ycConnection.getHeaderFields().entrySet() ) { System.out.println( r.getKey() + ": " + r.getValue() ); } rd.close(); System.out.println( response.toString() ); }