Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can't form a simple POST or GET http request. No data returns.

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    21
    My Mood
    Sleepy
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question Can't form a simple POST or GET http request. No data returns.

    Hi,
    I'm having problems forming an http request in java.
    Here's what I do:

    I've got a simple php (java.php) file on my apache server, here's it's code:
    <?php
    	$post_var = $_POST['post_var'];
    	if (!isset($post_var)) $post_var = $_GET['post_var'];
    	if ($post_var == 1) echo "works";
    	else echo $post_var . ": post_var";
    ?>
    If I do this request:
    http://warfarex.com/java.php?post_var=1
    from browser, I get "works" output. The same if I send POST or GET request from a ActionScript 3 client.
    But Java keeps on giving me this:

    : post_var
    So, as you can see, it's the output of an "else" block.

    And finally, here is the Java code I use to make these requests:

    String request = "?post_var=1";
    URL url = new URL("http://warfarex.com/java.php");
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());
    outputStreamWriter.write(request);
    outputStreamWriter.flush();
    System.out.println(request);
     
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String response;
    while ((response = bufferedReader.readLine()) != null) {
    	System.out.println(response);
    }
    outputStreamWriter.close();
    bufferedReader.close();

    What's wrong with this code?

    regards, Konstantin.
    Last edited by goodguy; May 29th, 2011 at 04:36 AM.


  2. #2
    Junior Member
    Join Date
    Jul 2010
    Posts
    21
    My Mood
    Sleepy
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Can't form a simple POST or GET http request. No data returns.

    I've just found the problem myself.
    It was the question mark in my request string.
    This request:
    String request = "post_var=1";
    will work as a charm

Similar Threads

  1. J2ME-Show RMS data in tabular form
    By Sunil Raghuvanshi in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: January 24th, 2011, 08:06 AM
  2. Any way to write html form data to file?
    By nathan.fortier@gmail.com in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 14th, 2011, 03:03 PM
  3. storing data using form
    By anupam.j2ee in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 27th, 2010, 10:43 AM
  4. Simple Form Help
    By dsen in forum Web Frameworks
    Replies: 1
    Last Post: September 8th, 2009, 11:32 PM
  5. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM