4

Boss wants us to send a HTTP GET with parameters in the body. I can't figure out how to do this using org.apache.commons.httpclient.methods.GetMethod or java.net.HttpURLConnection;.

GetMethod doesn't seem to take any parameters, and I'm not sure how to use HttpURLConnection for this.

MedicineMan
  • 13,795
  • 30
  • 92
  • 137

2 Answers2

3

HTTP GET method should NEVER have a body section. You can pass your parameters using URL query string or HTTP headers.

If you want to have a BODY section. Use POST or other methods.

Loc
  • 8,364
  • 6
  • 36
  • 73
  • 2
    Posted before I was finished :|. The HTTP spec says that a GET can have a body, but an HTTP server is not required to read it. – Sotirios Delimanolis Dec 12 '13 at 20:01
  • Can you share me a link or resource saying that we can send Body section along with Http GET? – Loc Dec 12 '13 at 20:05
  • For now, just other SO questions: http://stackoverflow.com/questions/978061/http-get-with-request-body – Sotirios Delimanolis Dec 12 '13 at 20:11
  • We are discussing based on standard Http servers like Tomcat, JBoss, Http Apache. You are talking about custom Http server like elasticsearch ( mentioned in that link ). – Loc Dec 12 '13 at 20:18
  • Nono, I mean the HTTP specification doesn't say anything against putting content in the body of an HTTP request. Take a look at [Dave Durbin's answer](http://stackoverflow.com/a/15656884/438154). The server is of no importance. – Sotirios Delimanolis Dec 12 '13 at 20:21
  • Even HTTP spec does not mention about it. If you program to send body along with GET, does standard server like Tomcat, jBoss, IIS understand the body? and parse the body? – Loc Dec 12 '13 at 20:22
  • That's what I mean. It doesn't have to, but the Spec doesn't prevent you from sending it. – Sotirios Delimanolis Dec 12 '13 at 20:23
  • Yes. so what is why I say I am talking about standard http servers and you talking about non-standard http servers. Anyway. Thank you for your notes. I didn't know Http GET can have body before. :) – Loc Dec 12 '13 at 20:25
  • For a server to be called an HTTP server, it has to comply with the HTTP specification. The spec says that a resource requested through a GET can be identified with the Request-Uri without ever needing to check the request body. If the server _does_ check the request body, that doesn't make it any less of a compliant server. – Sotirios Delimanolis Dec 12 '13 at 20:29
  • Absolutely agree with you. – Loc Dec 12 '13 at 20:30
3

You can extends the HttpEntityEnclosingRequestBase class to override the inherited org.apache.http.client.methods.HttpRequestBase.getMethod() but by fact HTTP GET does not support body request and maybe you will experience trouble with some HTTP servers, use at your own risk :)

public class MyHttpGetWithEntity extends HttpEntityEnclosingRequestBase { public final static String GET_METHOD = "GET";

public MyHttpGetWithEntity(final URI uri) {
    super();
    setURI(uri);
}

public MyHttpGetWithEntity(final String uri) {
    super();
    setURI(URI.create(uri));
}

@Override
public String getMethod() {
    return GET_METHOD;
}

}

then

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpEntityGet {

    public static void main(String[] args) {

        try {
            HttpClient client = new DefaultHttpClient();
            MyHttpGetWithEntity e = new MyHttpGetWithEntity("http://....");
            e.setEntity(new StringEntity("mystringentity"));
            HttpResponse response = client.execute(e);
            System.out.println(IOUtils.toString(response.getEntity().getContent()));
        } catch (Exception e) {
            System.err.println(e);
        }
    }
} 
vzamanillo
  • 9,465
  • 1
  • 31
  • 54