You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Erwan Baynaud <eb...@gmail.com> on 2006/10/03 15:34:32 UTC

[httpclient] get vs post

Does somebody can tell me why this code works :

	HttpClient client = new HttpClient();
        PostMethod post = new
PostMethod("http://127.0.0.1:8080/interactivity/servlet/VoteResultsServlet");
        NameValuePair[] data = {
        		new NameValuePair("ID_USER","1"),
        		new NameValuePair("ID_INTERACTIVITY","1")
        };
        post.setQueryString(data);
		client.executeMethod(post);
        String response = new
String(post.getResponseBodyAsString().getBytes("8859_1"));
        System.out.println(response);
        post.releaseConnection();
		}



And this one not :

	HttpClient client = new HttpClient();
        GetMethod post = new
GetMethod("http://127.0.0.1:8080/interactivity/servlet/VoteResultsServlet");
        NameValuePair[] data = {
        		new NameValuePair("ID_USER","1"),
        		new NameValuePair("ID_INTERACTIVITY","1")
        };
        post.setQueryString(data);
		client.executeMethod(post);
        String response = new
String(post.getResponseBodyAsString().getBytes("8859_1"));
        System.out.println(response);
        post.releaseConnection();
		}



I would like to use a POST method but only the GET one works.

Thank you.
-- 
Erwan BAYNAUD
Alcatel - Research & Innovation

Re: [httpclient] get vs post

Posted by Roland Weber <ht...@dubioso.net>.
Hello Erwan,

> Does somebody can tell me why this code works :
> 
>        PostMethod post = new
> [...]
> And this one not :
> 
>        GetMethod post = new
> 
> I would like to use a POST method but only the GET one works.

So I take it you pasted the examples in the wrong order.
Make sure your Servlet implements doPost as well as doGet.
http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/http/HttpServlet.html#doGet(javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse)
http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/http/HttpServlet.html#doPost(javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse)
I usually have a handleRequest method which is called by
both, doGet and doPost.

If that isn't the problem, please post a detailed description
of *what* goes wrong in the POST case. Do you get an error
reply? Is there an exception thrown somewhere?

By the way, we have mailing list guidelines:
http://jakarta.apache.org/site/mail.html
They ask you *not* to cross-post into multiple lists.

cheers,
  Roland


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org