Alternativa de HttpMethodBase en Java HttpClient4

I am using HttpMethodBase in common method like submitRequest(HttpMethodBase method,String input) for PostMethod method = new PostMethod(url) or GetMethod method = new GetMethod(url)

but we just updated to HttpClient4 and there is no clear way to use the common object as it was in version 3.

Whats the best way to get some common object for Post,Get,Delete etc. I don't want to maintain the different methods for post,get or delete.

preguntado el 01 de febrero de 12 a las 01:02

Here are few examples which may help you - -

1 Respuestas

You can create a DefaultHttpClient and different methods (GET,POST) and then execute the method using the DefaultHttpClient. For example,


import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;

public class MyHttp

    public class Constants
        public final static int METHOD_GET = 1;
        public final static int METHOD_POST = 2;            

    public void executeHttpRequest(String targetUrl, int methodType) throws ClientProtocolException, IOException
        HttpRequestBase method = null;
        DefaultHttpClient httpclient = new DefaultHttpClient();

        switch (methodType)
            case Constants.METHOD_GET : 
                method = new HttpGet(targetUrl);
            case Constants.METHOD_POST : 
                method = new HttpPost(targetUrl);

        HttpResponse response = httpclient.execute(method);


Respondido 01 Feb 12, 08:02

I want to do something like HttpMethodBase method; switch (methodType) { case Constants.METHOD_GET : method = new HttpGet(targetUrl); case Constants.METHOD_GET : method = new HttpPost(targetUrl); - Rahul Reddy

Rahul Reddy, thanks, much clearer, I have edited the answer. Take a look. - Jasonw

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.