英文:
Sending unicode data for POST request through Java client results in?
问题
我正在尝试通过我的Spring Boot应用程序中的Java客户端调用短信发送API。以下是我正在使用的代码。
HashMap<String, String> requestParameters = new HashMap<>();
requestParameters.put("user_name", env.getProperty(GlobalConstants.SMSConstants.IPROMO_USERNAME));
requestParameters.put("api_key", appCtx.getBean(PropertyServiceForJasyptSimple.class).getIpromoKey());
requestParameters.put("gateway_type", GlobalConstants.SMSConstants.IPROMO_ECONOMY_GATEWAY_ID);
requestParameters.put("country_code", GlobalConstants.SMSConstants.SL_COUNTRY_CODE);
requestParameters.put("number", phoneNumber.substring(1));
String message = String.format(msgSource.getMessage(GlobalConstants.SMSConstants.SMS_PASSWORD_RECOVERY_MSG,
new Object[]{}, LocaleContextHolder.getLocale()), loginId, recoveryCode);
requestParameters.put("message", message);
try {
ObjectMapper mapper = new ObjectMapper();
String requestBody = mapper.writeValueAsString(requestParameters);
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(GlobalConstants.SMSConstants.IPROMO_ENDPOINT);
request.setEntity(new StringEntity(requestBody));
HttpResponse response = client.execute(request);
System.out.println(requestBody);
System.out.println(request);
} catch (IOException e) {
e.printStackTrace();
}
以下是我在打印语句中看到的示例请求正文。
{
"country_code":"+94",
"number":"XXX",
"api_key":"XXX",
"user_name":"XXX",
"gateway_type":"1",
"message":"හිතවත් වරණ පරිශීලකය,\nඔබගේ පිවිසුම් හැඳුනුම්පත: XX\nඔබගේ එක් වරක් පමණක් භාවිත කල හැකි මුරපද වෙනස් කිරීමේ කේතය:XX"
}
我正在使用 ipromo短信网关。
然而,我在短信中收到的是????字符,而不是Unicode字符。然而,当我使用高级Rest客户端发送以下请求时,我收到带有正确字符的消息。
{
"user_name": "XXX",
"api_key": "XXXX",
"gateway_type": "1",
"country_code": "94",
"number": "XXXX",
"message": "විතවත් වරණ පරිශීලකය,ඔබගේ පිවිසුම් හැඳුනුම්පත: XXඔබගේ එක් වරක් පමණක් භාවිත කල හැකි මුරපද වෙනස් කිරීමේ කේතය:XX"
}
似乎问题出在我使用的Java代码上。对于我的代码有什么问题吗?
英文:
I am attempting a invoke an SMS sending API through a java client in my spring boot application. Following is the code I am using.
HashMap<String, String> requestParameters = new HashMap<>();
requestParameters.put("user_name", env.getProperty(GlobalConstants.SMSConstants.IPROMO_USERNAME));
requestParameters.put("api_key", appCtx.getBean(PropertyServiceForJasyptSimple.class).getIpromoKey());
requestParameters.put("gateway_type", GlobalConstants.SMSConstants.IPROMO_ECONOMY_GATEWAY_ID);
requestParameters.put("country_code", GlobalConstants.SMSConstants.SL_COUNTRY_CODE);
requestParameters.put("number", phoneNumber.substring(1));
String message = String.format(msgSource.getMessage(GlobalConstants.SMSConstants.SMS_PASSWORD_RECOVERY_MSG,
new Object[]{}, LocaleContextHolder.getLocale()), loginId, recoveryCode);
requestParameters.put("message", message);
try {
ObjectMapper mapper = new ObjectMapper();
String requestBody = mapper.writeValueAsString(requestParameters);
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(GlobalConstants.SMSConstants.IPROMO_ENDPOINT);
request.setEntity(new StringEntity(requestBody));
HttpResponse response = client.execute(request);
System.out.println(requestBody);
System.out.println(request);
} catch (IOException e) {
e.printStackTrace();
}
Following is a sample request body that I see in my print statement.
{"country_code":"+94","number":"XXX","api_key":"XXX","user_name":"XXX","gateway_type":"1","message":"හිතවත් වරණ පරිශීලකය,\nඔබගේ පිවිසුම් හැඳුනුම්පත: XX\nඔබගේ එක් වරක් පමණක් භාවිත කල හැකි මුරපද වෙනස් කිරීමේ කේතය:XX"}
I am using ipromo sms gateway.
However, what I receive in my SMS shows ???? characters instead of unicode characters. However, when I send the following request using the Advanced Rest Client, I receive a message with proper characters.
{
"user_name": "XXX",
"api_key": "XXXX",
"gateway_type": "1",
"country_code": "94",
"number": "XXXX",
"message": "විතවත් වරණ පරිශීලකය,ඔබගේ පිවිසුම් හැඳුනුම්පත: XXඔබගේ එක් වරක් පමණක් භාවිත කල හැකි මුරපද වෙනස් කිරීමේ කේතය:XX"
}
It appears the issue is with the Java code I am using. Any idea on what is wrong with my code?
答案1
得分: 1
Create your entity with utf-8
in your client code.
new StringEntity(requestBody, "utf-8")
英文:
Create your entity with utf-8
in your client code.
new StringEntity(requestBody, "utf-8")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论