Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

obtaining response charset of response to get or post request

I am working to extract response charset in a java web app, where I am using Apache HTTP Client.

For example, one possible value obtained from "Content-Type" header is

    text/html; charset=UTF-8

Then my code will extract all text after the "=" sign...

So the charset as extracted will be

    UTF-8

I just wanted to know, is the above method for obtaining response charset correct? Or is there some scenario where the above code will not work? Is there something I am missing here?

like image 971
Arvind Avatar asked Feb 02 '12 12:02

Arvind


People also ask

What is the charset value of a response?

The Charset value specifies to the browser how to decode the data when displaying the response. The CharsetName of Response.Charset must match the code page value or mixed characters are displayed in the browser.

What is a response in Python?

When one makes a request to a URI, it returns a response. This Response object in terms of python is returned by requests.method(), method being – get, post, put, etc. Response is a powerful object with lots of functions and attributes that assist in normalizing data or creating ideal portions of code.

What is the difference between response and raise_for_status?

response.status_code returns a number that indicates the status (200 is OK, 404 is Not Found). response.request returns the request object that requested this response. response.reason returns a text corresponding to the status code. response.raise_for_status () returns an HTTPError object if an error has occurred during the process.

What are the most commonly used response methods in http?

Some methods are most commonly used with response, such as response.json (), response.status_code, response.ok, etc. Requests is mostly used for making http request to APIs (Application Programming Interface). Some of commonly used response methods are discussed here –


2 Answers

The method provided by forty-two can work. But the method is deprecated, I find out that this website has a good example of method to find the charset.

HttpEntity entity = response.getEntity();
ContentType contentType = ContentType.getOrDefault(entity);
Charset charset = contentType.getCharset();
System.out.println("Charset  = " + charset.toString());
like image 183
Iching Chang Avatar answered Sep 20 '22 15:09

Iching Chang


Doesn't httpclient (or http core) already provide that functionality? Something like this:

HttpResponse response = ...
String charset = EntityUtils.getContentCharSet(response.getEntity());
like image 23
forty-two Avatar answered Sep 20 '22 15:09

forty-two