So I've got a problem here that I'm finding it difficult to crack.
I have a method here that sends a String request to a url and reads back the response. This works completely fine normally. But now I'm receiving response containing UTF-8 encoded characters, which I'm not able to read properly.
Request:
<Request>
<requestId>1071977</requestId>
<datas>
<parameter>
<id>CATEGORY</id>
<value>ALL</value>
</parameter>
</datas>
</Request>
Response(the one I'm facing issue with):
<Response>
<ResponseId>1071977</ResponseId>
<datas>
<parameter>
<id>CATEGORY</id>
<value>ALL</value>
</parameter>
<parameter>
<id>MSG</id>
<value>رنت ما</value>
</parameter>
</datas>
</Response>
public static String Post(String urlString, String request) throws Exception {
String response = null;
OutputStreamWriter out = null;
InputStream in = null;
URL url = null;
URLConnection connection = null;
StringBuilder sb = null;
try {
url = new URL(urlString);
connection = url.openConnection();
connection.setReadTimeout(60000);
connection.setDoOutput(true);
connection.setDoInput(true);
out = new OutputStreamWriter(connection.getOutputStream());
out.write(request);
out.flush();
out.close();
out = null;
int i = -1;
in = connection.getInputStream();
sb = new StringBuilder();
while ((i = in.read()) != -1) {
sb.append((char) i);
}
response = sb.toString();
in.close();
in = null;
} finally {
sb = null;
connection = null;
url = null;
}
return response;
}
I know I can use something like
ByteBuffer bb = StandardCharsets.UTF_8.encode(utfstring);
String normalString = StandardCharsets.UTF_8.decode(bb).toString();
to read utf-8
strings in java, but I'm not sure how to do the same while reading the response from URLConnection
class. Would appreciate some help. Thanks.