Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Using CL_HTTP_CLIENT object to send HTTP request - GB2312 encoding

Former Member
0 Likes
5,312

Hello!

I have to create a program that sends an xml string via HTTP to a bank server to get encoded. The bank server then sends me the encoded data back.

I am trying to implement this using the CL_HTTP_CLIENT object, but it does not appear to be sending chinese characters correctly. (There will always be Chinese characters in the xml body string I am sending)

I am using the following code to build and execute the POST (please note, the content type is not standard - I'm not sure if this matters or not).

After my code executes, I am able to check a log file that is generated by the bank - and apparently my request is always being send as "utf-8" encoding. This causes the chinese characters to be read incorrectly by the bank.

Is there anything special I need to do to be able to send "gb2312" charset with my HTTP post?? Please help... I will greatly appreciate it!

My current code to send the request (after the code, i'll show you what the bank is saying it received):

Create the HTTP client object

CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL

EXPORTING

URL = url_string

IMPORTING

CLIENT = HTTP_CLIENT_XML

EXCEPTIONS

ARGUMENT_NOT_FOUND = 1

INTERNAL_ERROR = 2

PLUGIN_NOT_ACTIVE = 3

OTHERS = 4.

  • Error checking

IF SY-SUBRC <> 0.

ENDIF.

                                      • Set HTTP header properites

  • Set REQUEST METHOD

CALL METHOD HTTP_CLIENT_XML->REQUEST->SET_HEADER_FIELD

EXPORTING

NAME = '~request_method'

VALUE = 'POST'.

  • Set SERVER PROTOCOL

CALL METHOD HTTP_CLIENT_XML->REQUEST->SET_HEADER_FIELD

EXPORTING

NAME = '~server_protocol'

VALUE = 'HTTP/1.0'.

  • Set CONTENT-TYPE

CALL METHOD HTTP_CLIENT_XML->REQUEST->SET_HEADER_FIELD

EXPORTING

NAME = 'Content-Type'

VALUE = 'INFOSEC_SIGN/1.0; charset=GB2312'.

                                      • end of HTTP header properites

                                    • set my XML string as the CDATA

CALL METHOD HTTP_CLIENT_XML->REQUEST->SET_CDATA

EXPORTING

DATA = big_xml

OFFSET = 0

LENGTH = xml_length.

******************************************

                                                                            • Send the HTTP request

CALL METHOD HTTP_CLIENT_XML->SEND

  • EXPORTING

  • timeout =

EXCEPTIONS

HTTP_COMMUNICATION_FAILURE = 1

HTTP_INVALID_STATE = 2

HTTP_PROCESSING_FAILED = 3

OTHERS = 4.

  • Error checking

IF SY-SUBRC <> 0.

CALL METHOD HTTP_CLIENT_XML->GET_LAST_ERROR

IMPORTING

CODE = SUBRC

MESSAGE = ERRORTEXT.

ENDIF.

                                                                                                  • Receive HTTP response

CALL METHOD HTTP_CLIENT_XML->RECEIVE

EXCEPTIONS

HTTP_COMMUNICATION_FAILURE = 1

HTTP_INVALID_STATE = 2

HTTP_PROCESSING_FAILED = 3

OTHERS = 4.

  • Error checking

IF SY-SUBRC <> 0.

CALL METHOD HTTP_CLIENT_XML->GET_LAST_ERROR

IMPORTING

CODE = SUBRC

MESSAGE = ERRORTEXT.

ERRORTEXT.

ENDIF.

This code works, but the bank server isn't interpreting the chinese characters correctly...

When the bank gets the POST, this is the header it receives from my program:

POST / HTTP/1.0

content-type: INFOSEC_SIGN/1.0; charset=utf-8

content-length: 1501

user-agent: SAP Web Application Server (1.0;700)

host: 192.168.44.203:449

accept-encoding: gzip

This all looks correct, except for the "charset-utf-8" it appended to the header. How do I get rid of this? I do not want to send the HTTP post request as utf-8 because of the chinese characters!

See my code... I assign my content type like this, but it has not worked - it ignores my "gb2312" and puts the utf-8 back in the header:

  • Set CONTENT-TYPE

CALL METHOD HTTP_CLIENT_XML->REQUEST->SET_HEADER_FIELD

EXPORTING

NAME = 'Content-Type'

VALUE = 'INFOSEC_SIGN/1.0; charset=GB2312'.

Any ideas?

5 REPLIES 5
Read only

Former Member
0 Likes
2,969

I'm sorry for the formatting above! I typed everything in paragraphs very neatly, but it does not show that way in my original post.

Read only

0 Likes
2,969

(Apparently the code I copied and pasted into the post caused my formatting issue - here is the post without the code)

I have to create a program that sends an xml string via HTTP to a bank server to get encoded. The bank server then sends me the encoded data back.

I am trying to implement this using the CL_HTTP_CLIENT object, but it does not appear to be sending chinese characters correctly. (There will always be Chinese characters in the xml body string I am sending)

I am using the following code to build and execute the POST (please note, the content type is not standard - I'm not sure if this matters or not).

After my code executes, I am able to check a log file that is generated by the bank - and apparently my request is always being send as "utf-8" encoding. This causes the chinese characters to be read incorrectly by the bank.

Is there anything special I need to do to be able to send "gb2312" charset with my HTTP post?? Please help... I will greatly appreciate it!

My current code to send the request (after the code, i'll show you what the bank is saying it received):

Any ideas?

Edited by: Matt Sprague on Sep 18, 2009 2:35 PM

Edited by: Matt Sprague on Sep 18, 2009 2:37 PM

Read only

0 Likes
2,969

OK - here is the code - should be formatted ok now. Sorry for that!

  • Create the HTTP client object

CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL

EXPORTING

URL = url_string

IMPORTING

CLIENT = HTTP_CLIENT_XML

EXCEPTIONS

ARGUMENT_NOT_FOUND = 1

INTERNAL_ERROR = 2

PLUGIN_NOT_ACTIVE = 3

OTHERS = 4.

  • Error checking

IF SY-SUBRC <> 0.

ENDIF.

                                            • Set HTTP header properites

  • Set REQUEST METHOD

CALL METHOD HTTP_CLIENT_XML->REQUEST->SET_HEADER_FIELD

EXPORTING

NAME = '~request_method'

VALUE = 'POST'.

  • Set SERVER PROTOCOL

CALL METHOD HTTP_CLIENT_XML->REQUEST->SET_HEADER_FIELD

EXPORTING

NAME = '~server_protocol'

VALUE = 'HTTP/1.0'.

  • Set CONTENT-TYPE

CALL METHOD HTTP_CLIENT_XML->REQUEST->SET_HEADER_FIELD

EXPORTING

NAME = 'Content-Type'

VALUE = 'INFOSEC_SIGN/1.0; charset=GB2312'.

***********************************************

CALL METHOD HTTP_CLIENT_XML->REQUEST->SET_CDATA

EXPORTING

DATA = big_xml

OFFSET = 0

LENGTH = xml_length.

  • Send the HTTP request

CALL METHOD HTTP_CLIENT_XML->SEND

  • EXPORTING

  • timeout =

EXCEPTIONS

HTTP_COMMUNICATION_FAILURE = 1

HTTP_INVALID_STATE = 2

HTTP_PROCESSING_FAILED = 3

OTHERS = 4.

  • Error checking

IF SY-SUBRC <> 0.

CALL METHOD HTTP_CLIENT_XML->GET_LAST_ERROR

IMPORTING

CODE = SUBRC

MESSAGE = ERRORTEXT.

ENDIF.

  • Receive HTTP response

CALL METHOD HTTP_CLIENT_XML->RECEIVE

EXCEPTIONS

HTTP_COMMUNICATION_FAILURE = 1

HTTP_INVALID_STATE = 2

HTTP_PROCESSING_FAILED = 3

OTHERS = 4.

  • Error checking

IF SY-SUBRC <> 0.

CALL METHOD HTTP_CLIENT_XML->GET_LAST_ERROR

IMPORTING

CODE = SUBRC

MESSAGE = ERRORTEXT.

ENDIF.

  • Move response string into variable

  • RESULT = HTTP_CLIENT_XML->RESPONSE->GET_CDATA( ).

  • Close HTTP connection

CALL METHOD HTTP_CLIENT_XML->CLOSE

EXCEPTIONS

HTTP_INVALID_STATE = 1

OTHERS = 2.

  • Error checking

IF SY-SUBRC <> 0.

CALL METHOD HTTP_CLIENT_XML->GET_LAST_ERROR

IMPORTING

CODE = SUBRC

MESSAGE = ERRORTEXT.

ENDIF.

Read only

0 Likes
2,969

This is the header the bank is saying I am sending - note how it puts UTF-8 into it automatically!

POST / HTTP/1.0

content-type: INFOSEC_SIGN/1.0; charset=utf-8

content-length: 1501

user-agent: SAP Web Application Server (1.0;700)

host: 192.168.44.203:449

accept-encoding: gzip

Read only

2,969

I solved the issue:

If I assign the xml to the request using the HTTP_CLIENT_XML->REQUEST->SET_CDATA method it seems to always assign UTF-8 as the encoding scheme.

If I convert my xml string to a '8400' encoded byte string, and assign it to the request with the HTTP_CLIENT_XML->REQUEST->SET_DATA method it transmits the data correctly and the UTF-8 encoding is NOT used.