Using HTTP GET in Java

Hello,

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;
                  conn.setDoOutput(true);
                  conn.setRequestMethod("GET");
                  conn.connect();
                  out = new PrintStream(conn.getOutputStream());

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


                  out.println(url
                              + "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 */,
                                          "ISO-8859-1")
+ "&detail="
+ URLEncoder.encode(text, "ISO-8859-1")
                              + "&type="
                              + URLEncoder.encode(String.valueOf(contentType),
                                          "ISO-8859-1")
                              + "&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"));

                  out.close();

............
...........
...........

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.

Melvin

mwhcAsked:
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.

objectsCommented:
>      conn.setDoOutput(true);

that should be false
you need to pass the parameters in the url for GET
0
girionisCommented:
> 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?
0
petmagdyCommented:
why don't u just do this

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

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

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,

//DEBUG
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...

Melv
0
objectsCommented:
> 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

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

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

again, thanks men!

Melv
0
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
Java

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.