Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Without delving too much into why the rest of it might not work, there is a problem in that you are:</p> <ul> <li><p>Using <code>URLEncoder</code> to encode your post data: your post data is not going to into the URL, so do not encode it.</p></li> <li><p>You are not setting the <code>Content-Length</code> header.</p></li> </ul> <p>Here is what you should have to get started:</p> <pre><code>public static Login POST(URL url, String user, String pw) throws IOException { String data= "api_type=json&amp;user=" + user +"&amp;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&lt;String, List&lt;String&gt;&gt; headers = ycConnection.getHeaderFields(); List&lt;String&gt; values = headers.get("Set-Cookie"); String cookieValue = null; for (java.util.Iterator&lt;String&gt; iter = values.iterator(); iter.hasNext(); ) { String v = iter.next(); if (cookieValue == null) cookieValue = v; else cookieValue = cookieValue + ";" + v; } return new Login(cookieValue, response); } </code></pre> <p>When working with APIs like this, you should definetly install <a href="http://www.fiddler2.com/fiddler2/" rel="noreferrer">Fiddler</a> which is HTTP debugger. You would have immediately seen the problem as your post data would look nothing like the example.</p> <p><strong><em>UPDATE:</em></strong></p> <p>Here is a little code I just threw into a test and it authenticated me just fine (obviously change <code>myusername</code> and <code>mypassword</code> to yours (don't forget to change it in the URL too):</p> <pre><code> @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&amp;user=" + user + "&amp;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&lt; String, List&lt; String &gt;&gt; r : ycConnection.getHeaderFields().entrySet() ) { System.out.println( r.getKey() + ": " + r.getValue() ); } rd.close(); System.out.println( response.toString() ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload