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 4 of 4

Thread: Problem sending POST request with Java..

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem sending POST request with Java..

    Hey there, for some reason my post request meaning to be: http://myurl.net/index.php?nmb=random_chars_etc wont work, here's my code:

    import java.util.UUID;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLEncoder;
     
     
    public class Main{
     
        private static String uuid = UUID.randomUUID().toString().replaceAll("-", "");
     
        public static void main(String[] args)throws Exception{
            String data = URLEncoder.encode("nmb", "UTF-8") + "=" + URLEncoder.encode("test", "UTF-8");
            URL url = new URL("http://myurl.net");
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
              System.out.println(line);
            }
            wr.close();
            rd.close();
        }
    }

    It definatlely reaches my page where I process the POST with php, however the UUID isnt sent over the url for some reason, a bit stuck..
    Last edited by lost; October 20th, 2010 at 08:29 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem sending POST request with Java..

    Quote Originally Posted by lost View Post
    however the UUID isnt sent over the url for some reason
    Please define what you mean by the UUID isn't sent. Does this mean the PHP page does not retrieve the 'nmb' value? In the php page, are you using $_GET, $_POST, $_REQUEST?

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem sending POST request with Java..

    As in the UUID isnt getting sent over the url for instance it should be like:

    http://myurl.net/index.php?nmb=<UUID HERE>

    but instead its just not being sent, I use $UID = $_GET['nmb']; to grab the UUID

    When I test my php page with a url say like:

    http://myurl.net/index.php?numb=randomblahblah

    ^ That works, so its not the PHP

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem sending POST request with Java..

    But that is the problem. $_GET retrieves the value using 'get' (values in the url), $_POST retrieves the value using 'post' (values written within the message body), and $_REQUEST retrieves either (and then some). Change the php to post or request and see what happens

Similar Threads

  1. Java problem -- sending email via JSP page
    By java_beginner in forum Java Theory & Questions
    Replies: 2
    Last Post: June 28th, 2010, 02:35 AM
  2. Java: Handling cookies when logging in with POST
    By cloakbot in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 16th, 2010, 04:31 PM
  3. Http post to login to forum
    By durenir in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2010, 11:03 AM
  4. Post variable set to non-null vlaues
    By ss7 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2009, 12:04 PM
  5. Replies: 6
    Last Post: October 20th, 2009, 06:33 AM