Sending a GET request from Java to PHP (HELP)

Discussion in 'Plugin Development' started by TomShar, Aug 5, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    TomShar

    So at the moment I have this but it isn't sending the url. I know it is being formatted correctly and that the information does get put into the database at the other end (if it reached) so the problem is the url isn't being sent.

    Code:
    String user = p.getName();
                        String email = args[0].toString();
                        String pass = args[1].toString();
                        String urlParameters = "u=" + user + "&e=" + email + "&p=" + pass;
                        String request = "http://localhost/uhc/post.php?";
                        try {
                            URL url = new URL(request + urlParameters);
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                            connection.setDoOutput(true);
                            connection.setInstanceFollowRedirects(false);
                            connection.setRequestMethod("GET");
                            connection.setRequestProperty("Content-Type", "text/plain");
                            connection.setRequestProperty("charset", "utf-8");
                            connection.connect();
                            p.sendMessage(url.toString());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
    EDIT:

    What im trying to do it get it to send the URL that it formed under the variable: 'url' which is carrying some variables from my plugin to be stored into a mysql database.
     
  2. You're complicating yourself.
    I've searched and tested, this is all that's needed:
    Code:
    URL url = new URL("http://localhost/test.php?print=success");
    URLConnection connection = url.openConnection();
    connection.connect();
    And I've used it along with this code to print what the page returns:
    Code:
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;
    
    while((inputLine = in.readLine()) != null)
    {
    	System.out.println(inputLine);
    }
    
    in.close();
    test.php:
    Code:
    <?php
    
    echo $_GET['print'];
    
    ?>
    And I get "success" in Java :}
     
    akminecrafthowto and pigplushy like this.
  3. Offline

    Reactorx2

    Try something like this: [LINK]
     
  4. Offline

    TomShar

    Thank you both of you, now works (used digi)
     
Thread Status:
Not open for further replies.

Share This Page