I have a servlet which accepts HTML content as part of the request param. The HTML is a localized one which may be a french, spanish etc... content.
I'm also using apache HTTP client to make a request to this servlet for test purpose, which has the following header definition:
HttpClient client = new HttpClient();
PostMethod method = new PostMethod("<URL>");
String html = FileUtils.readFileToString(inputHTMLFile, "UTF-8");
method.addParameter("html", html);
method.addRequestHeader("Accept", "*/*");
method.setRequestHeader("accept-charset", "UTF-8");
Whatever HTML is read has the character encoding utf-8, sample text:
Télécharger un fichier
However when i get the html from the request param that texts becomes T?l?charger un fichier
I went through few links such as http://www.oracle.com/technetwork/articles/javase/httpcharset-142283.html which talks about charset and how normally a browser would encode the special characters. If i were to URLEncode the html with UTF-8 and then decode that with same charset in the servlet i get the HTML as expected.
Is this the only thing i can do to preserve the charsets? Am i missing something?
Thanks.
Now that the issue with the file itself is fixed, try modifying your code as follows:
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod("<URL>");
postMethod.getParams().setContentCharset("utf-8"); //The line I added
...
Note that the client needs to decode the request as UTF-8 now. French and Spanish worked correctly because their characters are included in the default ISO-8859-1 charset. Chinese characters are not. If the French and Spanish were decoded correctly on client, the client is decoding the request as ISO-8859-1, and sending UTF-8 could fail.
So you could try also adding this:
postMethod.setRequestheader("Content-Type", "application/x-www-form-url-encoded; charset=utf-8");
Just try this for post method.
HttpPost request = new HttpPost(webServiceUrl);
StringEntity str = new StringEntity(YourData);
str.setContentType("application/json");
HttpPost.setEntity(new StringEntity(str, HTTP.UTF_8));
You should better change string to base64 encoded and then send.
I think I've found the cause by examining EntityBuilder decompiled code: the EntityBuilder ignores the contentEncoding field regarding the parameters, it uses the one from contentType field. And by looking on org.apache.http.entity.ContentType the only one predefined value having UTF-8 is org.apache.http.entity.ContentType.APPLICATION_JSON.
So in my case
HttpPost method = new HttPost("<URL>");
EntityBuilder builder = EntityBuilder.create();
builder.setContentType(ContentType.APPLICATION_JSON);
builder.setContentEncoding(StandardCharsets.UTF_8.name());
...
method.setEntity(builder.build());
did the job (although I think setting contentType is redundant here).
I'm using httpclient-osgi version 4.5.4.
PostMethod method = new PostMethod("URL");
method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
Related
I want my app to response with body utf-8 and iso-8859-1 encoded
per requests with Accept-Charset="utf-8" or Accept-Charset="iso-8859-1".
The response body is always JSON.
In my controller, when I doing this
render(json: data, status: :created)
It response with Content-Type="application/json; charset=utf-8" as well.
But how to make a response with body iso-8859-1 encoded when request Accept-Charset="iso-8859-1"?
In order to do this, you can use the method force_encoding and encoding for example
data = {'name'=>'raghav'}.to_json
data.encoding #This would return what encoding the value as #<Encoding:UTF-8>
new_data = data.force_encoding('ISO-8859-1') #This would force the encoding
new_data.encoding #<Encoding:ISO-8859-1>
Also to do this on the specific case you can always read the request.headers hash to determine the encoding.
There is also another method called encode the main difference between these are force_encoding changes the way string is being read from bytes, and encode changes the way string is written without changing the output (if possible)
I'm having troubles getting the correct encoding for a text file with xhr.
xhr(content.getContentUrl(), {
handleAs: "text",
headers: { 'Content-Type': 'text/plain; charset=iso-8859-1' }
}).then(function (data) {
console.log("DATA");
console.log(data); ... );
The data object is a text file that should be with ISO-8859-1 characters, but I get a ? instead of the special character, it's like the response encoding is UTF-8
Example: "PER-RW-C-MC-013,B,ABB, P�rtico 5B. Fundaciones. Memoria de
C�lculo,17/06/2011,23/06/2011,17/06/2011,01/07/2011,24/06/2011,20/07/2011,24/06/2011,19/07/2011,0,PER-RW-C-MC-013-C,PER-RW-C-MC-013-A"
Note: The content.getContentUrl() is a method from IBM filenet API that returns the text file URL in a filenet Repository.
Thanks in advance.
In response to your xhr request, you have code on your server that reads the file into a string and sends back that string as part of the response. This may very well be where the problem arises. See for example here (case of php) for a situation where this happened and a solution is suggested.
I'm trying to decompress a gzip:ed response i receive from a REST service:
Content-Encoding=[gzip], Content-Type=[application/json], Content-Length=[710] ...
I'm using the Grails REST Client Builder Plugin:
def response = new RestBuilder().get(HOST + "/api/..."){
contentType "application/json"
accept "application/json"
}
The returned response is a Spring ResponseEntity. I'm trying to decompress the data using GZIPInputStream:
String body = response.getBody()
new GZIPInputStream(new ByteArrayInputStream(body.getBytes())).text
This fails to Caused by ZipException: Not in GZIP format
Obviously there is something I'm doing wrong, but I can't figure out what. All advice is appriciated.
If you really need to keep using the Rest Client Builder you only need to modify your client code slightly:
def response = new RestBuilder().get(HOST + "/api/..."){
contentType "application/json"
accept byte[].class, "application/json" }
Note the extra parameter in the accept call - byte[].class - which signifies that RestTemplate should refrain from any parsing of the response.
To decompress you can now do:
new GZIPInputStream(new ByteArrayInputStream(response.body))
Yeah, I know, already answered with accept but some might still find it helpful in case switching Rest components is not an option.
I never managed to get it to work with grails / groovy libraries, so i switched to spring and httpcomponents:
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(HttpClientBuilder.create().build());
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
ResponseEntity<String> response = restTemplate.exchange(
"some/url/", HttpMethod.GET, new HttpEntity<Object>(requestHeaders),
String.class);
Which decoded gzip automatically and thus there are no longer any need for manual decoding.
httpclient hc=new httpclient();
var postcontent = new FormUrlEncodedContent(new Dictionary<string, string> {
{"subject",""},
{"message",content}
});
var post = await hc.PostAsync(postURI, postcontent);
FormUrlEncodedContent encoding for UTF-8
content is Chinese
how can i do for Encoding.GBK
httpclient PostAsync Chinese encoding GBK
I don't believe what you are trying to do is possible. From the specification of application/www-form-urlencoded here it says,
Note: The media type does not have a 'charset' parameter, it
is incorrect specify one and to associate any significance to
it if specified. The character encoding is always UTF-8. The
Unicode encoding form signature is not supported; a leading
U+FEFF character will be considered part of a .
Apparently this is one difference between application/x-www-form-urlencoded and application/www-form-urlencoded. The latter didn't explicitly force UTF-8 but apparently it was a best practice. It looks like you might have to write your own HttpContent class and do the serialization yourself.
I create an application that sends some data to a secured network.
At the server side they need the data as JSON object. For that am creating the data as JSON object and writing that data in the OutputStream of the connection.
But the response from the server side telling it is not getting the data that I am passing.
The code snippet that am using is something like given below:
HttpsConnection _connection = (HttpsConnection)Connector.open("https://gmail.com/",Connector.READ_WRITE, true); _connection.setRequestMethod(HttpsConnection.POST);
_connection.setRequestProperty("If-Modified-Since", "29 Oct 1999 19:43:31 GMT");
_connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
_connection.setRequestProperty("Content-Language", "en-US");
_connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
byte[] postData = jsonObject.toString().getBytes("UTF-8");
_connection.setRequestProperty("Content-Length", Integer.toString(postData.length));
_connection.setRequestProperty("jsondata",jsonObject.toString());
OutputStream os = _connection.openOutputStream();
os.write(postData);
os.flush();
Please help me to solve the issue.
I guess the reason is "Content-Type" => "application/x-www-form-urlencoded". This type of a POST exists for sending a list of key=value pairs. So the server on its side will parse the post data in terms of key=value pairs. I believe in your case it just fails to parse the got post data, because you don't send the data in the key=value pairs form (instead you just pour the entire json string jsonObject.toString().getBytes("UTF-8") in it).
So basically you need to form a key value pair "json=YOUR_JSON_HERE". Then on the server you will get your data as the json parameter value:
URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);
encPostData.append("json", jsonObject.toString());
byte[] postData = encPostData.toString().getBytes("UTF-8");
Another option (and BTW it would be the most proper way to do this particular task) would be to use "multipart/form-data" POST type. However it will be a bit harder to implement it if you've never done that before on BB.
You have to append appropriate suffix to to your url
eg: If you use simulator use:https://gmail.com/;deviceside=true etc
I have same this problem but finally find solution:
HttpConnection c = (HttpConnection)Connector.open(url);
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty(
HttpProtocolConstants.HEADER_CONTENT_TYPE, PostData.getContentType());
c.setRequestProperty(
HttpProtocolConstants.HEADER_CONTENT_LENGTH,String.valueOf(oPostData.size()));
c.setRequestProperty("Content-Length", Integer.toString(oPostData.size()));
c.setRequestProperty("Content-Type","application/json");
byte [] postDataBytes = jobj.toString().getBytes("UTF-8");
os = c.openOutputStream();
os.write(postDataBytes);
os.flush();