I'm attempting to make a plugin that sends an HTTP GET Request to a PHP script, I'm extremely new to Java, and I've just been trying code samples from around google searches, and none of them are working, and all are throwing me errors =/ I wish I could provide code to give a push start, but I honestly haven't gotten a single one far into working. Has anyone developed anything with a working HTTP GET (Or POST, I'm not limited to GET) requests? Thanks for any and all help! Necro EDIT: I'm trying some code that can be displayed below, hopefully I can get this to work, Eclipse is informing me that line 3 has "Duplicate local variable url" and wants to change url to a string, very confused why. Code:java public static String get(String url) { try { URL url = new URL(url); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String str = in.readLine(); in.close(); } catch (java.io.IOException e1) { } }
Rename your string that is passed in or the URL because both variables have the same name and is probley confuzling poor eclipse. Other than that I know nothing about HTTP stuff so good luck.
Hmm.. yeah HTML stuff can be kind of tricky This is kind of an old (well old for me) method I used for getting something from a URL Code:java public static String sendGetRequest(String endpoint, String requestParameters){String result = null;if (endpoint.startsWith("http://")){// Send a GET request to the servlettry{// Send dataString urlStr = endpoint;if (requestParameters != null && requestParameters.length () > 0){urlStr += requestParameters;} URL url = new URL(urlStr); URLConnection conn = url.openConnection ();// Get the responseBufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));StringBuffer sb = new StringBuffer();String line;while ((line = rd.readLine()) != null){sb.append(line);}rd.close();result = sb.toString();} catch (Exception e){ }}return result; }
Wow, awesome! Works flawlessly, my only one issue, is that it seems to be requiring the syntax to be Code:java sendGetRequest("[url]http://gophobia.com/query.php?ign=test&argument=argumentLOL[/url]", commandLabel); And is adding my commandLabel onto the getRequest, I could obviously just put in a little hack like Code:java sendGetRequest("[url]http://gophobia.com/query.php?ign=test&argument=argumentLOL&plugin=[/url]", commandLabel); But how would I not require to provide the commandLabel?
Oh, I think you are confused on what the actuall agumetns are for you would do something like... Code:java sendGetRequest("yourwebsite.com/action.php?", "param1=10¶m2=20");
Doh! Well that makes sense, if I'm setting paramaters, how easy would it be to make this request POST? Or would it not be possible using this method?