html - How to login to reddit.com using the api? -
i'm not sure how format request url login reddit using api. i've tried page not found error
https://ssl.reddit.com/api/login/?user=myusername&passwd=mypassword&api_type=json
refer this question. user strelok has great answer
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 = ycconnection.getinputstream(); bufferedreader rd = new bufferedreader( new inputstreamreader( ) ); string line; stringbuffer response = new stringbuffer(); while ( ( line = rd.readline() ) != null ) { response.append( line ); response.append( '\r' ); } ( entry< string, list< string >> r : ycconnection.getheaderfields().entryset() ) { system.out.println( r.getkey() + ": " + r.getvalue() ); } rd.close(); system.out.println( response.tostring() ); }
Comments
Post a Comment