2

Here is the code I am using. Instead of posting data to the page, it is downloading it like a regular text file. So, I am actually getting the html code returned and the form is not being submitted.

I know the html form works. If I open it in a browser, I can post to it and my data appears in my database. I'm guessing I'm missing a parameter or something.

public void testComm ()
{
try
    {
    URL         url;
    URLConnection   urlConn;
    DataOutputStream    printout;
    DataInputStream input;

    url = new URL ("http://mysite.com/myform.html");

    // URL connection channel.
    urlConn = url.openConnection();

    // Let the run-time system (RTS) know that we want input.
    urlConn.setDoInput (true);

    // Let the RTS know that we want to do output.
    urlConn.setDoOutput (true);

    // No caching, we want the real thing.
    urlConn.setUseCaches (false);

    // Specify the content type.
    urlConn.setRequestProperty
    ("Content-Type", "application/x-www-form-urlencoded");

    // Send POST output.
    printout = new DataOutputStream (urlConn.getOutputStream ());

    String content =
    "txtLevelName=" + URLEncoder.encode ("level1") +
    "&txtLevelData=" + URLEncoder.encode ("abcd");

    printout.writeBytes (content);
    printout.flush ();
    printout.close ();

    // Get response data.
    input = new DataInputStream (urlConn.getInputStream ());

    String str;
    while (null != ((str = input.readLine())))
    {
    System.out.println (str);
    }

    input.close ();

    }
catch (MalformedURLException me)
    {
    System.err.println("MalformedURLException: " + me);
    }
catch (IOException ioe)
    {
    System.err.println("IOException: " + ioe.getMessage());
    }
}   
Shawn Chin
  • 74,316
  • 17
  • 152
  • 184
Mike Brascome
  • 23
  • 1
  • 3
  • 4
    Any reason you're avoiding something like [HttpClient](http://hc.apache.org/httpcomponents-client-ga/) and letting it do most of the work for you? And I'm not sure what you mean by getting HTML back; are you saying you don't get HTML back when you post the form from the browser? – Dave Newton Nov 14 '11 at 18:08
  • Are you posting it to the form or the action??? please print your html and the action script. – Sid Malani Nov 14 '11 at 18:11

4 Answers4

8

You're already sending a POST request. It's the urlConn.setDoOutput(true) which sets the request method to POST. Apparently you're sending the request to the wrong URL. Open the HTML page in your browser. Rightclick and View Source. Locate the HTML <form> element in the HTML source. It should look something like:

<form action="http://mysite.com/somescript" method="post">

Check its action attribute. That's exactly the URL which you have to POST to.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
3

Probably you are not doing it right. Looks like you are posting it to the form instead of the action.

Sid Malani
  • 1,972
  • 1
  • 12
  • 12
2

what I'm doing (I know there are frameworks for this, but i wanted it light-weight):

        final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        if(!postString.isEmpty()) {
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Length", Integer.toString(postString.getBytes().length));
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            final OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
            wr.write(postString);
            wr.flush();
            wr.close();
        }

postString is the same as your content string

// edit: oh and as sid mentioned above: your url is a html page???

Alex
  • 444
  • 3
  • 10
2

you can try jsoup: http://jsoup.org/

it's VERY easy to use:

Document doc = Jsoup.connect("http://example.com")
  .data("query", "Java")
  .post();

Load a Document from a URL: http://jsoup.org/cookbook/input/load-document-from-url

Koerr
  • 13,337
  • 25
  • 70
  • 107