使用CL_HTTP_CLIENT时编码混乱

问题描述 投票:0回答:1

我在ABAP中做了一个http请求,是这样的。

    CALL METHOD cl_http_client=>create_by_url
      EXPORTING
        URL    = url
      IMPORTING
        client = client.

    client->request->set_header_field(
      name  = 'Content-Type'
      value = 'application/json; charset=utf-8'
    ).

    client->send(
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2
        http_processing_failed     = 3
        http_invalid_timeout       = 4
        OTHERS                     = 5
    ).

    CHECK sy-subrc = 0.

    client->receive(
      EXCEPTIONS
        http_communication_failure = 1
        http_invalid_state         = 2
        http_processing_failed     = 3
        OTHERS                     = 4
    ).

    string_response = client->response->get_cdata( ).

string_response 编码不正确,数据应该是例如: "{"CompanyName": "Uberlândia"}"但是,我越来越 "{"CompanyName": "Uberlândia"}"即使我经过 'applicationjson; charset=utf-8'。 在请求头 'Content-Type'

http request sap encode abap
1个回答
2
投票

可能是响应的编码为 UTF-8 但在HTTP响应中没有提到。

所以你必须使用 get_data 而不是 get_cdata类型的变量 XSTRING 字节串),然后对其进行解码。UTF-8 类型的变量中。STRING (字符串)。

DATA: xstring_response TYPE xstring,
      string_response  TYPE string.

" Get response as a string of bytes
xstring_response = client->response->get_data( ).

" Assume response is UTF-8 text, so decode it
" (UTF-8 is the default parameter value of method convert_from)
string_response = cl_abap_codepage=>convert_from( xstring_response ).
© www.soinside.com 2019 - 2024. All rights reserved.