Learn how to a build a cloud-first strategyRegister Now


Using HTTP GET in Java

Posted on 2004-11-12
Medium Priority
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

ID: 12563519
>      conn.setDoOutput(true);

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

Expert Comment

ID: 12563521
> 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

petmagdy earned 500 total points
ID: 12563529
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();

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 12563649
>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

ID: 12563662
> 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

ID: 12563782
Yeah!! It worked. Thanks to,

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

again, thanks men!


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month21 days, 4 hours left to enroll

810 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