Using HTTP GET in Java


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.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

>      conn.setDoOutput(true);

that should be false
you need to pass the parameters in the url for GET
> 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?
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();

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

mwhcAuthor Commented:
>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...

> 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

mwhcAuthor Commented:
Yeah!! It worked. Thanks to,

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

again, thanks men!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.