hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: setRequestHeader question
Date Fri, 04 Aug 2006 20:55:55 GMT
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


Mime
View raw message