Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using HTTP GET in Java

Posted on 2004-11-12
6
Medium Priority
?
292 Views
Last Modified: 2010-03-31
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

0
Comment
Question by:mwhc
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12563519
>      conn.setDoOutput(true);

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

Expert Comment

by:girionis
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?
0
 
LVL 13

Accepted Solution

by:
petmagdy earned 500 total points
ID: 12563529
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

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

 

Author Comment

by:mwhc
ID: 12563649
>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
 
LVL 92

Expert Comment

by:objects
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

0
 

Author Comment

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

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

again, thanks men!

Melv
0

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