Sending unicode data for POST request through Java client results in?

huangapple go评论86阅读模式
英文:

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&lt;String, String&gt; requestParameters = new HashMap&lt;&gt;();
requestParameters.put(&quot;user_name&quot;, env.getProperty(GlobalConstants.SMSConstants.IPROMO_USERNAME));
requestParameters.put(&quot;api_key&quot;, appCtx.getBean(PropertyServiceForJasyptSimple.class).getIpromoKey());
requestParameters.put(&quot;gateway_type&quot;, GlobalConstants.SMSConstants.IPROMO_ECONOMY_GATEWAY_ID);
requestParameters.put(&quot;country_code&quot;, GlobalConstants.SMSConstants.SL_COUNTRY_CODE);
requestParameters.put(&quot;number&quot;, phoneNumber.substring(1));

String message = String.format(msgSource.getMessage(GlobalConstants.SMSConstants.SMS_PASSWORD_RECOVERY_MSG,
        new Object[]{}, LocaleContextHolder.getLocale()), loginId, recoveryCode);
requestParameters.put(&quot;message&quot;, 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.

{&quot;country_code&quot;:&quot;+94&quot;,&quot;number&quot;:&quot;XXX&quot;,&quot;api_key&quot;:&quot;XXX&quot;,&quot;user_name&quot;:&quot;XXX&quot;,&quot;gateway_type&quot;:&quot;1&quot;,&quot;message&quot;:&quot;හිතවත් වරණ පරිශීලකය,\nඔබගේ පිවිසුම් හැඳුනුම්පත: XX\nඔබගේ එක් වරක් පමණක් භාවිත කල හැකි මුරපද වෙනස් කිරීමේ කේතය:XX&quot;}

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.

{
  &quot;user_name&quot;: &quot;XXX&quot;,
  &quot;api_key&quot;: &quot;XXXX&quot;,
  &quot;gateway_type&quot;: &quot;1&quot;,
  &quot;country_code&quot;: &quot;94&quot;,
  &quot;number&quot;: &quot;XXXX&quot;,
  &quot;message&quot;: &quot;විතවත් වරණ පරිශීලකය,ඔබගේ පිවිසුම් හැඳුනුම්පත: XXඔබගේ එක් වරක් පමණක් භාවිත කල හැකි මුරපද වෙනස් කිරීමේ කේතය:XX&quot;
  }

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, &quot;utf-8&quot;)

huangapple
  • 本文由 发表于 2020年8月1日 16:01:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63203057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定