1

So I have this piece of code, and let's assume I'm filling out a form on a website and want to submit it, how do I find the keys and values to submit? I'll explain better, I have this code, it's supposed to send an HTTP POST Request, but I have no idea what to put in place of the "key1" and "value1", same goes with "key2" and "value2":

public String sendPostRequest(String url) {

    StringBuffer sb = null;

    try {
        String data = URLEncoder.encode("key1", "UTF-8") + "="
                + URLEncoder.encode("value1", "UTF-8");
        data += "&" + URLEncoder.encode("key2", "UTF-8") + "="
                + URLEncoder.encode("value2", "UTF-8");

        URL requestUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) requestUrl
                .openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");

        OutputStreamWriter osw = new OutputStreamWriter(
                conn.getOutputStream());
        osw.write(data);
        osw.flush();

        BufferedReader br = new BufferedReader(new InputStreamReader(
                conn.getInputStream()));

        String in = "";
        sb = new StringBuffer();

        while ((in = br.readLine()) != null) {
            sb.append(in + "\n");
        }

        osw.close();
        br.close();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return sb.toString();
}

Can you give me an example site with the working replica of this code? (key1/key2 and value1/value2 replaced with something that works)

You can add more keys/values if that works better, I just have no idea how to know what to fill in there for every different site.

4
  • Any reason you don't want to use an HTTP library? Commented Mar 30, 2012 at 19:08
  • I don't want to use any external library, if that's what you're talking about, and that's because I'm pretty certain this code works if I just knew what I should insert into the string "data". Commented Mar 30, 2012 at 19:16
  • Why would you not want to use tried, tested, optimized etc code from elsewhere? Do you apply this policy to all problems? There are many really good Java libraries out there - avoid "not invented here" syndrome. Commented Mar 30, 2012 at 19:24
  • Trust me I've used many external libraries, I'm just doing this as practice. If I use an external library that makes my problem extremely easy to solve, it wouldn't be that much of a practice would it? Besides, the libraries weren't the issue, the attributes were. Commented Mar 30, 2012 at 19:37

1 Answer 1

3

The keys would be the name attributes of the input elements of the html form. The values would be the values you want to set.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.