You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Jaya Srinivasan <ja...@hotmail.com> on 2006/08/04 20:32:23 UTC

setRequestHeader question

Hi

I am using Apache's HttpClient 3.0 and was noticing the following 

When I set the content header on the post method I am not able to parse the parameters on the backend i.e request.getParameters returns null. 
I am currently sending Latin-1 characters but we need to make it UTF-8 compliant. Basically my question is how do I change the charset for the request body?

Here's what I am doing right now that doesn't work:

        // Create a method instance.
        PostMethod postmethod= new PostMethod(url);
        postmethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"););

        // set the input parameters
        Iterator it = paramList.keySet().iterator();
        while (it.hasNext()) {
                 String paramName = (String) it.next();
                  postmethod.addParameter(paramName, paramList.get(paramName));
         }


        try {

            HttpClient client = new HttpClient();
            // Execute the method.
            client.executeMethod(postmethod);
       } catch (HttpException e) { 
           ...
        } catch (IOException e) {
           ....
        } finally {
            // Release the connection.
            method.releaseConnection();
        }

Thanks
Jaya

Re: setRequestHeader question

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Fri, 2006-08-04 at 22:38 +0200, Roland Weber wrote:
> Hello Jaya,
> 
> > When I set the content header on the post method I am
> > not able to parse the parameters on the backend i.e
> > request.getParameters returns null. 
> 
> Do not touch the Content-Type header. It's one of the
> things that's handled by HttpClient. Whatever you do
> to that header can only make things worse.
> 
> > I am currently sending Latin-1 characters but we need
> > to make it UTF-8 compliant. Basically my question is
> > how do I change the charset for the request body?
> > 
> > Here's what I am doing right now that doesn't work:
> > 
> >         // Create a method instance.
> >         PostMethod postmethod= new PostMethod(url);
> >         postmethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"););
> 
> www-form-urlencoded is US-ASCII by definition. The only
> charset allowed in URLs is US-ASCII. It is pure luck
> (or tolerant behavior of libraries and servers) that it
> worked with Latin-1 at all.
> 
> >         // set the input parameters
> >         Iterator it = paramList.keySet().iterator();
> >         while (it.hasNext()) {
> >                  String paramName = (String) it.next();
> >                   postmethod.addParameter(paramName, paramList.get(paramName));
> >          }
> 
> If you need to support charsets, you can't use
> PostMethod.addParameter(). Use PostMethod.setRequestEntity()
> instead, with a MultiPartRequestEntity:
> http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/methods/multipart/MultipartRequestEntity.html
> You can set the HTTP_CONTENT_CHARSET parameter to UTF-8:
> http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/params/HttpMethodParams.html#HTTP_CONTENT_CHARSET
> 

MultiPartRequestEntity may be an overkill in this situation. This should
suffice:

NameValuePair[] params = new NameValuePair[] {
  new NameValuePair("name", "value")
}; 
String content = EncodingUtil.formUrlEncode(params, "UTF-8");
StringRequestEntity entity = new StringRequestEntity(content, 
        PostMethod.FORM_URL_ENCODED_CONTENT_TYPE, "US-ASCII"); 

Oleg

> hope that helps,
>   Roland
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> 
> 


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


Re: setRequestHeader question

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

> When I set the content header on the post method I am
> not able to parse the parameters on the backend i.e
> request.getParameters returns null. 

Do not touch the Content-Type header. It's one of the
things that's handled by HttpClient. Whatever you do
to that header can only make things worse.

> I am currently sending Latin-1 characters but we need
> to make it UTF-8 compliant. Basically my question is
> how do I change the charset for the request body?
> 
> Here's what I am doing right now that doesn't work:
> 
>         // Create a method instance.
>         PostMethod postmethod= new PostMethod(url);
>         postmethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"););

www-form-urlencoded is US-ASCII by definition. The only
charset allowed in URLs is US-ASCII. It is pure luck
(or tolerant behavior of libraries and servers) that it
worked with Latin-1 at all.

>         // set the input parameters
>         Iterator it = paramList.keySet().iterator();
>         while (it.hasNext()) {
>                  String paramName = (String) it.next();
>                   postmethod.addParameter(paramName, paramList.get(paramName));
>          }

If you need to support charsets, you can't use
PostMethod.addParameter(). Use PostMethod.setRequestEntity()
instead, with a MultiPartRequestEntity:
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/methods/multipart/MultipartRequestEntity.html
You can set the HTTP_CONTENT_CHARSET parameter to UTF-8:
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/params/HttpMethodParams.html#HTTP_CONTENT_CHARSET

hope that helps,
  Roland

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