Results 1 to 9 of 9
Thread: Java & php $_Get
- 08-25-2009, 02:34 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 13
- Rep Power
- 0
Java & php $_Get
I've tried searching around on google about this, but nothing seemed to work.
Here is what I have so far:
All I want to do is submit 1 variable to a php file, then disconnect.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(); } }
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.
- 08-25-2009, 04:49 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you explain bit more what exactly you want to do with pHp and Java, I'm not clear?
- 08-25-2009, 08:39 AM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
What happened when you compiled/ran the code?
What results did you get and what results did you expect to get?
- 08-25-2009, 08:53 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 13
- Rep Power
- 0
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.
- 08-25-2009, 09:08 AM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
I understand perfectly what you are trying to do. The problem is that you haven't answered any of my questions.
- 08-25-2009, 12:30 PM #6
Member
- Join Date
- Feb 2009
- Posts
- 13
- Rep Power
- 0
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.
- 08-25-2009, 12:40 PM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Here is a tutorial that should help.
- 08-25-2009, 01:30 PM #8
Member
- Join Date
- Feb 2009
- Posts
- 13
- Rep Power
- 0
Thanks, everything is working now.
One more question, the only line I don't really understand is #10.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(); } }
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.Java Code:BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
Last edited by Blacknight; 08-25-2009 at 01:34 PM.
- 08-25-2009, 01:44 PM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
If you only want to write to it then perhaps you should have used
P.S The above code has not been tested anywhere.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();


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks