Results 1 to 9 of 9
  1. #1
    Blacknight is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default Java & php $_Get

    I've tried searching around on google about this, but nothing seemed to work.

    Here is what I have so far:

    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class submit {
        public static void main(String[] args) throws Exception {
    		String username = "test";
    
    		URL u = new URL("hxxp://127.0.0.1/test.php?var1=username");
    		URLConnection a = u.openConnection();
    		HttpURLConnection connection = (HttpURLConnection) uc;
    
    		connection.disconnect();
        } 
    }
    All I want to do is submit 1 variable to a php file, then disconnect.

    Edit: hxxp is suppose to be http, but It won't let me post "links".
    Last edited by Blacknight; 08-25-2009 at 02:38 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    22

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    What happened when you compiled/ran the code?
    What results did you get and what results did you expect to get?

  4. #4
    Blacknight is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Can you explain bit more what exactly you want to do with pHp and Java, I'm not clear?
    I wanted to submit the data using .php?var1=WhateverHere. Then the php file handles it from there.


    Example:

    Every time i visit hxxp://127.0.0.1/test.php?var1=Blah in browser(this is what I want to do, but without the browser), the php file gets "Blah" and then writes it to a file.

    I could use the users browser to simply visit the page and close it(in java), but that doesn't really seem like a great way of doing things.


    This is what test.php looks like:

    Java Code:
    <?php
    if (isset($_GET['var1'])) {
    	$file = fopen("test.txt", 'w') or die("error.");
    if($existingContents == "")
    	fwrite($file, $_GET['var1']);	
    fclose($file);
    ?>
    Last edited by Blacknight; 08-25-2009 at 12:35 PM.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    I understand perfectly what you are trying to do. The problem is that you haven't answered any of my questions.

  6. #6
    Blacknight is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    I understand perfectly what you are trying to do. The problem is that you haven't answered any of my questions.

    Sorry, looks like I quoted the wrong person.

    Anyways, If i were to run it I would get an error(which I can't post) at run-time. But even if I set setDoOutput() to true, the php file does not get anything.

    If anyone could point out what's wrong, or tell me which methods I should look up to get this working, I would appreciate it.
    Last edited by Blacknight; 08-25-2009 at 12:36 PM.

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    Here is a tutorial that should help.

  8. #8
    Blacknight is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    is a tutorial that should help.
    Thanks, everything is working now.

    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class Reverse {
        public static void main(String[] args) throws Exception {
    
    	URL url = new URL("hxxp://127.0.0.1/Test.php?var1=blah&var2=blah2");
    	URLConnection connection = url.openConnection();
    
    	BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    	in.close();
        }
    }
    One more question, the only line I don't really understand is #10.

    Java Code:
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    Why does this object have to be created(I'm not trying to get any data, or even use it?), however without it, it will not work.
    Last edited by Blacknight; 08-25-2009 at 01:34 PM.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    If you only want to write to it then perhaps you should have used

    Java Code:
    URL url = new URL("http://127.0.0.1/Test.php");
    URLConnection connection = url.openConnection();
    //connection.setDoOutput(true); only if you need to read the output
    OutputStreamWriter out = new OutputStreamWriter(                         connection.getOutputStream());
    out.write("var1=blah");
    out.close();
    P.S The above code has not been tested anywhere.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •