HTTP GET Request?

Discussion in 'Plugin Development' started by necrohhh, Jun 24, 2013.

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


    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!


    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.

    1. public static String get(String url) {
    2. try {
    3. URL url = new URL(url);
    4. BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    5. String str = in.readLine();
    6. in.close();
    7. }
    8. catch ( e1) {
    10. }
    12. }
  2. 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.
  3. Offline


    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

    2. public static String sendGetRequest(String endpoint, String requestParameters){
    3. String result = null;
    4. if (endpoint.startsWith("http://"))
    5. {
    6. // Send a GET request to the servlet
    7. try
    8. {
    9. // Send data
    10. String urlStr = endpoint;
    11. if (requestParameters != null && requestParameters.length () > 0)
    12. {
    13. urlStr += requestParameters;
    14. }
    16. URL url = new URL(urlStr);
    18. URLConnection conn = url.openConnection ();
    19. // Get the response
    20. BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    21. String line;
    22. while ((line = rd.readLine()) != null)
    23. {
    24. sb.append(line);
    25. }
    26. rd.close();
    27. result = sb.toString();
    28. } catch (Exception e)
    29. {
    31. }
    32. }
    33. return result;
    35. }
  4. Offline


    Wow, awesome! Works flawlessly, my only one issue, is that it seems to be requiring the syntax to be

    1. sendGetRequest("[url][/url]", commandLabel);

    And is adding my commandLabel onto the getRequest, I could obviously just put in a little hack like

    1. sendGetRequest("[url][/url]", commandLabel);

    But how would I not require to provide the commandLabel?
  5. Offline


    Oh, I think you are confused on what the actuall agumetns are for

    you would do something like...
    2. sendGetRequest("", "param1=10&param2=20");
  6. Offline


    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?
Thread Status:
Not open for further replies.

Share This Page