本人现在正在做银企互联接口,由于银行前置机程序只支持SOCKET连接,所以现在在用JAVA开发了一个中间件,但是在ABAP中构建XML文件发送到中间件时,如果碰到奇数个中文汉字则最后一个汉字会变成乱码,而对于偶数汉字则没有任何问题。。。
附ABAP代码和JAVA代码:
ABAP构建XML文件:
call function 'SDIXML_DATA_TO_DOM'
exporting
name = objname
dataobject = dataobj
importing
data_as_dom = dom
changing
document = document
exceptions
others = 01.
if sy-subrc = 0.
call method document->append_child
exporting
new_child = dom
receiving
rval = rc.
if rc = 0.
* convert DOM to XML
call function 'SDIXML_DOM_TO_XML'
exporting
document = document
* pretty_print = 'X'
importing
xml_as_string = xml_as_string
tables
xml_as_table = xml_as_table
exceptions
others = 1.
call function 'HR_KR_XSTRING_TO_STRING'
exporting
* from_codepage = '8400'
from_codepage = '4110'
in_xstring = xml_as_string
importing
out_string = xmlstring "XML文件串
exceptions
others = 1.
endif.
endif.
发送文件:
l_len = strlen( xmlstring ) .
cl_http_client=>create_by_url(
exporting url = url
importing client = http_client ).
call method http_client->request->set_header_field
exporting
name = 'Content-Type'
value = 'text/xml; charset=utf-8'.
call method http_client->request->set_header_field
exporting
name = '~request_method'
value = 'POST'.
call method http_client->request->set_cdata
exporting
data = xmlstring
offset = 0
length = l_len.
call method http_client->send
exceptions
http_communication_failure = 1
http_invalid_state = 2.
if sy-subrc <> 0.
rc = sy-subrc.
return.
endif.
call method http_client->receive
exceptions
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
if sy-subrc <> 0.
rc = sy-subrc.
return.
endif.
JAVA端接受时的处理:
message = new String(message.getBytes(), "UTF-8");
message = new String(message.getBytes(), "GB2312");
Debug.printLog("Request:" + message);
message为SAP过来的字符串,经转换后就会出现上面的状况。
诚心求解!! 本人现在正在做银企互联接口,由于银行前置机程序只支持SOCKET连接,所以现在在用JAVA开发了一个中间件,但是在ABAP中构建XML文件发送到中间件时,如果碰到奇数个中文汉字则最后一个汉字会变成乱码,而对于偶数汉字则没有任何问题。。。
附ABAP代码和JAVA代码:
ABAP构建XML文件:
call function 'SDIXML_DATA_TO_DOM'
exporting
name = objname
dataobject = dataobj
importing
data_as_dom = dom
changing
document = document
exceptions
others = 01.
if sy-subrc = 0.
call method document->append_child
exporting
new_child = dom
receiving
rval = rc.
if rc = 0.
* convert DOM to XML
call function 'SDIXML_DOM_TO_XML'
exporting
document = document
* pretty_print = 'X'
importing
xml_as_string = xml_as_string
tables
xml_as_table = xml_as_table
exceptions
others = 1.
call function 'HR_KR_XSTRING_TO_STRING'
exporting
* from_codepage = '8400'
from_codepage = '4110'
in_xstring = xml_as_string
importing
out_string = xmlstring "XML文件串
exceptions
others = 1.
endif.
endif.
发送文件:
l_len = strlen( xmlstring ) .
cl_http_client=>create_by_url(
exporting url = url
importing client = http_client ).
call method http_client->request->set_header_field
exporting
name = 'Content-Type'
value = 'text/xml; charset=utf-8'.
call method http_client->request->set_header_field
exporting
name = '~request_method'
value = 'POST'.
call method http_client->request->set_cdata
exporting
data = xmlstring
offset = 0
length = l_len.
call method http_client->send
exceptions
http_communication_failure = 1
http_invalid_state = 2.
if sy-subrc <> 0.
rc = sy-subrc.
return.
endif.
call method http_client->receive
exceptions
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
if sy-subrc <> 0.
rc = sy-subrc.
return.
endif.
JAVA端接受时的处理:
message = new String(message.getBytes(), "UTF-8");
message = new String(message.getBytes(), "GB2312");
Debug.printLog("Request:" + message);
message为SAP过来的字符串,经转换后就会出现上面的状况。
诚心求解!!
ABAP http_client执行http请求
最新推荐文章于 2024-05-30 16:32:39 发布