有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我无法使用Reddit的API登录

我正在尝试使用RedditAPI做一些事情。除了更改页面和登录,我的一切都正常工作

我需要登录才能使用我的程序,我知道如何使用我得到的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: Server returned HTTP response code: 504 for 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协议不太熟悉。 (因此我的项目——我在学习)


共 (1) 个答案

  1. # 1 楼答案

    如果不深入探究为什么其余部分可能不起作用,那么问题在于你:

    • 使用URLEncoder对你的帖子数据进行编码:你的帖子数据不会进入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<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);
    }
    

    当使用这样的API时,您应该明确地安装Fiddler,这是HTTP调试器。您会立即看到问题,因为您的帖子数据与示例完全不同

    更新:

    下面是我刚刚在一个测试中输入的一个小代码,它对我的身份验证很好(显然将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() );
        }