HTTP GET Request?

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

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

    necrohhh

    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
    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 (java.io.IOException e1) {
    9.  
    10. }
    11.  
    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

    dillyg10

    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
    1.  
    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. }
    15.  
    16. URL url = new URL(urlStr);
    17.  
    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. {
    30.  
    31. }
    32. }
    33. return result;
    34.  
    35. }
    36.  
     
  4. Offline

    necrohhh


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

    Code:java
    1. 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
    1. sendGetRequest("[url]http://gophobia.com/query.php?ign=test&argument=argumentLOL&plugin=[/url]", commandLabel);


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

    dillyg10

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

    you would do something like...
    Code:java
    1.  
    2. sendGetRequest("yourwebsite.com/action.php?", "param1=10&param2=20");
    3.  
     
  6. Offline

    necrohhh


    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