I'm having strange issue with a response got from API. I'm using apache HTTP Client to get response. Response header has the following
Content-Type=[application/json; charset=utf-16]
Transfer-Encoding=[chunked]
X-Powered-By=[ASP.NET] // Yes, people using ASP.NET
So based on this, when I get response, my response looks like follows
笀∀匀琀愀琀甀猀䌀漀搀攀∀㨀㈀
So I tried the following.
String body = "笀∀匀琀愀琀甀猀䌀漀搀攀∀㨀㈀";
String charSetString = "utf-8|utf-16|utf-16le, all possible combination"
body = new String(body.getBytes(Charset.forName(charSetString));
body = body.replaceAll("[^\\x00-\\x7F]", "");
But no luck. Started to look at first char. Actual response in first char is {
I converted first char from response to ascii
(int)body.charAt(0)
Value is 31488
; Whereas Ascii value of {
is 123; if I do 31488/256 = 123
and converting this to char giving me {
so I did the following
String encoded = "";
for(int i=0; i< body.length(); i++) {
encoded += ((char) ((int)body.charAt(i) / 256 ));
}
And it worked. But this is so bad conversation for single API. What I'm missing, what exactly the charset of response if I get 31488
for {
Update
My API call code.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.SerializationUtils;
import org.springframework.web.client.RestTemplate;
public class HTTPClientManager {
RestTemplate restTemplate = null;
public void setup() {
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = null;
clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
HttpClientBuilder.create().build());
clientHttpRequestFactory.setReadTimeout(5 * 1000);
clientHttpRequestFactory.setConnectTimeout(5 * 1000);
restTemplate = new RestTemplate(clientHttpRequestFactory);
}
public static void main(String...strings) throws FileNotFoundException, IOException {
HTTPClientManager ht = new HTTPClientManager();
ht.setup();
Map<String, Object> properties = new LinkedHashMap<>();
properties.put(Const.METHOD, "GET");
properties.put(Const.URL, strings[0]);
properties.put(Const.CHAR_SET, "UTF-16LE");
Map<String, Object> ob = ht.getResponse(properties);
try {
String res = ob.get(Const.RESPONSE).toString();
System.out.println("Response ->>>>>>>>> \n " + res);
}catch(Exception e) {
e.printStackTrace();
}
try (FileOutputStream fos = new FileOutputStream("response")) {
fos.write(SerializationUtils.serialize(ob));
}
}
public static class Const {
public static final String REQUEST = "request";
public static final String URL = "url";
public static final String CHAR_SET = "charSet";
public static final String RESPONSE = "response";
public static final String METHOD = "method";
public static final String REQUEST_HEADER = "reqHeader";
public static final String RESPONSE_HEADER = "resHeader";
}
public Map<String, Object> getResponse(Map<String, Object> properties) {
HttpHeaders headers = new HttpHeaders();
HttpEntity requestEntity = null;
Map<String, Object> responseReturn = new LinkedHashMap<>();
HttpMethod method = null;
if (properties.get(Const.METHOD).toString().equals("GET")) {
method = HttpMethod.GET;
requestEntity = new HttpEntity<String>("", headers);
} else if (properties.get(Const.METHOD).toString().equals("POST")) {
method = HttpMethod.POST;
requestEntity = new HttpEntity<String>(properties.get(Const.REQUEST).toString(), headers);
}else if (properties.get(Const.METHOD).toString().equals("PUT")) {
method = HttpMethod.PUT;
requestEntity = new HttpEntity<String>(properties.get(Const.REQUEST).toString(), headers);
}else if (properties.get(Const.METHOD).toString().equals("DELETE")) {
method = HttpMethod.DELETE;
requestEntity = new HttpEntity<String>(properties.get(Const.REQUEST).toString(), headers);
}
ResponseEntity<String> response = null;
try {
response = restTemplate.exchange(properties.get(Const.URL).toString(), method, requestEntity, String.class);
String body = response.getBody();
if(properties.get(Const.CHAR_SET) != null) {
try {
body = new String(body.getBytes(Charset.forName(properties.get(Const.CHAR_SET).toString())));
body = body.replaceAll("[^\\x00-\\x7F]", "");
}catch(Exception e) {
e.printStackTrace();
}
}
responseReturn.put(Const.RESPONSE, body!=null?body:"");
responseReturn.put(Const.RESPONSE_HEADER, response.getHeaders());
} catch (org.springframework.web.client.HttpClientErrorException |org.springframework.web.client.HttpServerErrorException exception) {
exception.printStackTrace();
}catch(org.springframework.web.client.ResourceAccessException exception){
exception.printStackTrace();
}catch(Exception exception){
exception.printStackTrace();
}
return responseReturn;
}
}