发送POST HTTP请求时编码日语字符

时间:2019-04-11 21:16:56

标签: http post encoding

即使将内容类型设置为APPLICATION_JSON_UTF8,日语字符也无法在HTTP实体中正确编码。

例如,“アマゾン企画/アニマティクス制作费”成为“ ?????? / ?????????????”。

代码如下:

public static String executeRequest(@NonNull final String url,
                                    @NonNull final HttpMethod httpMethod,
                                  final String... inputPayload
) throws Exception {
  final HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

  HttpEntity<String> entity;

  if (inputPayload.length > 0) {
    entity = new HttpEntity<>(inputPayload[0], headers);
  } else {
    entity = new HttpEntity<>(headers);
  }

  final RestTemplate restTemplate = new RestTemplate();

   return retryTemplate.execute(new RetryCallback<String, Exception>() {
      @Override
      public String doWithRetry(RetryContext context) throws Exception { 
        return restTemplate.exchange(url, httpMethod, entity, String.class).getBody();
      }
    });
}

这是响应的一部分。内容类型已被更正,设置为application / json,字符集为UTF8。但是,日语或中文字符仍未成功编码。

"headers":{
"strict-transport-security":"max-age=16070400",
"x-content-type-options":"nosniff",
"x-runtime":"5.713655",
"x-frame-options":"SAMEORIGIN",
"set-cookie":"_mkra_ctxt=f34d5370c6dc2777b61c4e569a1c82e3--201; path=/; max-age=5; HttpOnly",
"transfer-encoding":"chunked",
"content-type":"application/json; charset=utf-8",
"status":"201 Created",
"date":"Sat, 13 Apr 2019 06:38:20 GMT",
"x-robots-tag":"none",
"cache-control":"max-age=0, private, must-revalidate",
"x-xss-protection":"1; mode=block",
"x-request-id":"41bb9d52-cbd0-436e-8499-28bef466ba03",
"front-end https":"on",
"etag":"W/\"3a7b149d73de2888c93231b4b17afdd7\""}},
"original":{
  "requested-by":{"login":"avischan"},
  "ship-to-address":{"location-code":"24937191"},
  "requisition-lines":[
  {
    "account":{"account-type":{"name":"2Y Amazon Digital Services LLC"},"code":"2Y-6040-4307-61414-1251-1710-0000"},
    "commodity":{"name":"Corporate Sponsorships"},
    "currency":{"code":"USD"},
    "description":"测试中文",
    "line-num":1,
    "need-by-date":"2019-04-10T23:39:03.951Z",
    "supplier":{"number":"66844577"},
    "unit-price":500,
    "invoice-matching":"2-way"}],
    "justification":"why i should spend"}}

0 个答案:

没有答案