1

I am trying to send a HTTP/1.1 POST to http://uploaded.net/io/login, using HttpURLConnection, in order to login to the website with my username and password.

For some reason it appears like byte[] postData is not being sent to the server.

I receive the response code and message 200 OK, but the server replies with the JSON-formatted error message {"err":"Please type in id and password"} as if I never actually sent the data.

I've tried different methods (sending a String, byte[] etc., using different output writers) of sending the data but so far none of them have worked and I can't figure out where exactly it fails.

Here is the code snippet that I am working with:

package post;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;

public class PostTest {

    public static void login() throws MalformedURLException, IOException {
        String urlParameters = String.format("id=%s&password=%s", 
                URLEncoder.encode(Config.getProperty("account.1.username"), "UTF-8"), 
                URLEncoder.encode(Config.getProperty("account.1.password"), "UTF-8")
        );

        byte[] postData = urlParameters.getBytes(Charset.forName("UTF-8"));

        URL url = new URL(Config.getProperty("login.url"));
        HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
        httpConn.setDoInput(true);
        httpConn.setDoOutput(true);
        httpConn.setUseCaches(false);
        httpConn.setRequestMethod("POST");
        httpConn.setRequestProperty("Content-Length", 
                    Integer.toString(postData.length));

        //prints: 26
        System.out.println(postData.length);
        //prints: id=test&password=123456789
        System.out.println(urlParameters);

        httpConn.getOutputStream().write(postData);
        httpConn.getOutputStream().flush();

        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(httpConn.getInputStream()))) {
            for (String line; (line = reader.readLine()) != null;)
                System.out.println(line);
        }
    }
}

Live HTTP Headers (Firefox plugin) output:

POST /io/login HTTP/1.1
Host: uploaded.net
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0
Accept: text/javascript, text/html, application/xml, text/xml, */*
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
X-Requested-With: XMLHttpRequest
X-Prototype-Version: 1.6.1
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://uploaded.net/
Content-Length: 20
Cookie: __utma=9[censored...]
Connection: keep-alive
  id=test&pw=123456789

HTTP/1.1 200 OK
[...]

Edit: Added the Firefox' Live HTTP Headers output which I used to see what data is being sent through POST.

Is my assumption that byte[] postData is not sent correctly, if at all, correct?

What changes do I have to make to get the login to work properly?

phew
  • 808
  • 1
  • 15
  • 34
  • 1
    POST data isn't sent in URL-encoded format; GET data is. – Dave Newton Mar 11 '16 at 04:32
  • Ok, good to know, thank you. Though, removing the `URLEncoder.encode()` still results in the same error reply. – phew Mar 11 '16 at 04:46
  • I guess that the values of username and password are not being sent as you think. – Keerthivasan Mar 11 '16 at 04:48
  • Please check this other SO answer - http://stackoverflow.com/questions/9767952/how-to-add-parameters-to-httpurlconnection-using-post – Keerthivasan Mar 11 '16 at 04:51
  • All the URLEncoder does is... encode. It doesn't magically turn a URL string into what a POST request looks like. – Dave Newton Mar 11 '16 at 04:52
  • maybe the server is not expecting the parameters `id` and `password` – Scary Wombat Mar 11 '16 at 04:58
  • Yes, I just assumed that the protocol may ignore faulty formatted data. – phew Mar 11 '16 at 04:59
  • @scary Wombat I've checked it using Firefox' HTTP LIVE Headers plugin, the data that Firefox sends when I press "Login" is `id=&password=`. – phew Mar 11 '16 at 05:01
  • why are you using a `DataOutputStream` ? Maybe a ByteArrayOutputStream ? – Scary Wombat Mar 11 '16 at 05:09
  • @DaveNewton POST data *of content-type x-www-form-urlencoded* is indeed URLencoded; see https://en.wikipedia.org/wiki/Percent-encoding#The_application.2Fx-www-form-urlencoded_type . Note that URLencoding is only the query *component* of a GET URL – dave_thompson_085 Mar 11 '16 at 07:25
  • @ScaryWombat BAOS isn't a solution; it wouldn't write to the host at all. But OP: you can lose the `DataOutputStream`, and just `write` on the stream `conn.getOutputStream()` gives you. Also `Charset` is not a header: if you specify it at all it should be an attribute on `Content-type`; but you don't need it precisely because URLencoding is used and is designed to be safe for any default charset. But I don't think either of these should cause your problem. Can you monitor exaactly what is sent with wireshark or similar, or route through a debugging proxy like fiddler or webscarab? – dave_thompson_085 Mar 11 '16 at 07:27
  • @ScaryWombat Its a leftover from testing. Originally I was using `httpConn.getOutputStream().write(postData);`, when it didn't work I started trying different methods to send the data. It shouldn't matter though, but I've tried `ByteArrayOutputStream()` too which also fails. – phew Mar 11 '16 at 10:28
  • @dave_thompson_085 Allright, I'm updating the code snippet with recent changes. I haven't used WireShark in years and I'm not familiar with Fiddler and Webscrab, but I'll give it a shot. Thank you for the tips. – phew Mar 11 '16 at 11:04

1 Answers1

0

Uhh, I've finally noticed my mistake. Hence I tried the first logins using my localhost/test.php where the parameter was password=<pw> I completly overlooked that the actual website I send the data to is using pw=<pw> instead.

After changing String.format("id=%s&password=%s", [...] to String.format("id=%s&pw=%s", [...] it works like a charm.

phew
  • 808
  • 1
  • 15
  • 34