Using HTTP GET in Java

Posted on 2004-11-12
Last Modified: 2010-03-31

I'm desperately trying to use HTTP GET method to send data to another server (sadly this server only takes GET not POST). So, I use HttpURLConnection with the setRequestMethod("GET"). But, i still get a POST method instead  of a GET. This is my code,


            try {
URLConnection connection = null;
                  preregResults = new URL(url);
                  connection = preregResults.openConnection();
                  HttpURLConnection conn = (HttpURLConnection) connection;
                  out = new PrintStream(conn.getOutputStream());

                  System.out.println("requestMethod=" + conn.getRequestMethod());

                              + "dummyfield="
                              + URLEncoder.encode("empty", "ISO-8859-1")
                              + "&username="
                              + URLEncoder.encode(username, "ISO-8859-1")
                              + "&password="
                              + URLEncoder.encode(password, "ISO-8859-1")
                              + "&price_code="
                              + URLEncoder.encode("0030"/* fourDigitCharge */,
+ "&detail="
+ URLEncoder.encode(text, "ISO-8859-1")
                              + "&type="
                              + URLEncoder.encode(String.valueOf(contentType),
                              + "&to="
                              + URLEncoder.encode("+" + receiverNumber, "ISO-8859-1")
                              + "&sender="
                              + URLEncoder.encode("+" + receiverNumber, "ISO-8859-1")
                              + "&telco="
                              + URLEncoder.encode(telcoPrefix, "ISO-8859-1")
                              + "&service_id="
                              + URLEncoder
                                          .encode(String.valueOf(serviceId), "ISO-8859-1")
                              + "&keyword="
                              + URLEncoder.encode(serviceKeyword, "ISO-8859-1")
                              + "&short_code="
                              + URLEncoder
                                          .encode(String.valueOf(shortCode), "ISO-8859-1")
                              + "&dummy=" + URLEncoder.encode("empty", "ISO-8859-1"));



I know that,
conn.setDoOutput(true) makes the method to POST.

But, shouldn't,
conn.setRequestMethod("GET"), change it back to GET?

I'm desperate for an answer. Please help.


Question by:mwhc
    LVL 92

    Expert Comment

    >      conn.setDoOutput(true);

    that should be false
    you need to pass the parameters in the url for GET
    LVL 35

    Expert Comment

    > conn.setDoOutput(true) makes the method to POST.

    I am not sure if this is correct. The above method indicates that we want to use the connection for input and output as well.

    Do you get any error messages? How do you know that is is a POST instead of a GET?

    GET has a limit. You are trying to send too much data with the GET request. I will make a wild assumption, maybe the server cannot handle it and then it converts it to a POST automatically?

    Try to send less parameters and see what happens?
    LVL 13

    Accepted Solution

    why don't u just do this

    url += dummyfield= + URLEncoder.encode("empty", "ISO-8859-1") + ... + ...+

    URLConnection connection = null;
                   preregResults = new URL(url);
                   connection = preregResults.openConnection();

    Author Comment

    >Do you get any error messages? How do you know that is is a POST instead of a GET?
    I do this,

    System.out.println("requestMethod=" + conn.getRequestMethod());

    and i get a requestMethod=POST.

    Yeah, i know that GET has limitation. Tell that to the other server guy. They insisted on GET! :P

    ok, petmagdy

    I'll try that. I'll be back...

    LVL 92

    Expert Comment

    > why don't u just do this
    > url += dummyfield= + URLEncoder.encode("empty", "ISO-8859-1") + ... + ...+

    thats whatbi was referring to in my comment.
    you have no choice you have to pass the params like that for a post
    + send no output


    Author Comment

    Yeah!! It worked. Thanks to,

     petmagdy and objects....too bad i cant let you guys share the points..

    again, thanks men!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Suggested Solutions

    Title # Comments Views Activity
    Convert ArrayList of custom objects to JSON 4 44
    scoresAverage challenge 8 58
    wordsWithout 49 64
    Increment alphanumeric sequence 6 52
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now