其他分享
首页 > 其他分享> > ABAP-调用HTTP请求模板

ABAP-调用HTTP请求模板

作者:互联网

背景:第三方提供的webservice WSDL创建消费者服务的时候报错,SAP解析不了对方的wsdl,此时就可以使用HTTP调用的方式进行系统对接

 

 

 

以上,反正就是各种报错,但是对方提供的WSDL在SOAPUI中可以正常使用,此时就需要使用另外一种方法

 

 

首先在postman中测试对方提供的wsdl 文件

 

 

 

 

 测试成功!

 

以下就需要在ABAP 中实现:代码及注释如下

*&---------------------------------------------------------------------*
*& Report Z_HTTPTEST02_CPF
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT z_httptest02_cpf.


DATA: lo_http_client TYPE REF TO if_http_client,  "定义一个HTTP Client
      lv_uri         TYPE string,                 "URL
      lv_results     TYPE string.                 "Response
DATA: itab TYPE TABLE OF string.                  "response inter table
DATA: p_code   TYPE i,                            "status code
      p_reason TYPE string.                       "status title

DATA gt_res TYPE STANDARD TABLE OF string.

data lv_result TYPE string.

* Create an instance of the HTTP client:
CALL METHOD cl_http_client=>create_by_url
  EXPORTING
    url                = 'http://IP地址/web/ws/r/aws_ttsrv2'
  IMPORTING
    client             = lo_http_client
  EXCEPTIONS
    argument_not_found = 1
    plugin_not_active  = 2
    internal_error     = 3
    OTHERS             = 4.


"设置HTTP header
CALL METHOD lo_http_client->request->set_header_field
  EXPORTING
    name  = 'Content-Type'
    value = 'text/xml'.

CALL METHOD lo_http_client->request->set_header_field
  EXPORTING
    name  = 'SOAPAction'
    value = '""'.

CALL METHOD lo_http_client->request->set_header_field
  EXPORTING
    name  = 'Accept-Encoding'
    value = 'gzip,deflate'.


"设置HTTP 执行的方法
CALL METHOD lo_http_client->request->set_method( 'POST' ).

INCLUDE Z_HTTPTEST02_CPF_XML.      "根据实际业务拼接XML

"cl_demo_output=>display( lv_xml ).

DATA: len TYPE i .
len = strlen( lv_xml ) .

"设置HTTP参数
CALL METHOD lo_http_client->request->set_cdata
  EXPORTING
    data   = lv_xml
    offset = 0
    length = len.

"调用HTTP
lo_http_client->send(
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2 ).

"接受HTTP response
lo_http_client->receive(
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2
    http_processing_failed     = 3 ).
IF sy-subrc EQ 0.
  lv_result =
    lo_http_client->response->get_cdata( ).
  cl_demo_output=>display( lv_result ).                             "输出webservice返回结果

  CALL METHOD lo_http_client->response->get_status
    IMPORTING
      code   = p_code
      reason = p_reason.
ELSE.
*      RAISE EXCEPTION TYPE lcx_icf_exception.
ENDIF.

SPLIT lv_result AT cl_abap_char_utilities=>cr_lf INTO TABLE gt_res.  "把html内容放到内表

WRITE: / 'Status:',p_code, p_reason.
ULINE.
CLEAR lv_result.
LOOP AT gt_res INTO lv_result.
  WRITE: / lv_result.
ENDLOOP.

"关闭连接
CALL METHOD lo_http_client->close.

 

 

INCLUDE 程序如下

*&---------------------------------------------------------------------*
*& 包含               Z_HTTPTEST02_CPF_XML
*&---------------------------------------------------------------------*

"设置POST body的报文
DATA: lv_xml   TYPE string,
      lv_xml01 TYPE string,
      lv_xml02 TYPE string,
      lv_xml03 TYPE string,
      lv_xml04 TYPE string,
      lv_xml05 TYPE string,
      lv_xml06 TYPE string,
      lv_xml07 TYPE string,
      lv_xml08 TYPE string,
      lv_xml09 TYPE string,
      lv_xml10 TYPE string,
      lv_xml11 TYPE string,
      lv_xml12 TYPE string,
      lv_xml13 TYPE string,
      lv_xml14 TYPE string,
      lv_xml15 TYPE string,
      lv_xml16 TYPE string,
      lv_xml17 TYPE string,
      lv_xml18 TYPE string,
      lv_xml19 TYPE string,
      lv_xml20 TYPE string,
      lv_xml201 TYPE string,
      lv_xml21 TYPE string,
      lv_xml22 TYPE string,
      lv_xml23 TYPE string,
      lv_xml24 TYPE string,
      lv_xml25 TYPE string,
      lv_xml26 TYPE string,
      lv_xml27 TYPE string,
      lv_xml28 TYPE string,
      lv_xml29 TYPE string,
      lv_xml30 TYPE string,
      lv_xml31 TYPE string,
      lv_xml32 TYPE string,
      lv_xml33 TYPE string,
      lv_xml34 TYPE string,
      lv_xml35 TYPE string,
      lv_xml36 TYPE string,
      lv_xml37 TYPE string,
      lv_xml38 TYPE string,
      lv_xml39 TYPE string,
      lv_xml40 TYPE string,
      lv_xml41 TYPE string,
      lv_xml42 TYPE string,
      lv_xml43 TYPE string,
      lv_xml44 TYPE string,
      lv_xml45 TYPE string,
      lv_xml46 TYPE string.

data: lv_xml_head type string,
      lv_xml_body type string,
      lv_xml_foot type string.

lv_xml01 = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tip="http://www.dsc.com.tw/tiptop/TIPTOPServiceGateWay">'.
lv_xml02 = '   <soapenv:Header/>'.
lv_xml03 = '   <soapenv:Body>'.
lv_xml04 = '      <tip:OaMroItemRequest>'.
lv_xml05 = '         <tip:request><![CDATA[<Request>'.
lv_xml06 = '  <Access>'.
lv_xml07 = '    <Authentication user="xxxx" password="xxxx"/>'.
lv_xml08 = '    <Organization name="WCTZ"/>'.
lv_xml09 = '  </Access>'.
lv_xml10 = '  <Parameter>'.
lv_xml11 = '    <Record>'.
lv_xml12 = '      <Field name="pgplant" value="WCTZ"/>'.
lv_xml13 = '    </Record>'.
lv_xml14 = '  </Parameter>'.
lv_xml15 = '  <RequestContent>'.
lv_xml16 = '    <Document>'.

CONCATENATE lv_xml01 lv_xml02 lv_xml03 lv_xml04 lv_xml05 lv_xml06 lv_xml07 lv_xml08 lv_xml09 lv_xml10 lv_xml11 lv_xml12 lv_xml13 lv_xml14 lv_xml15 lv_xml16 INTO lv_xml_head.

DO 1 TIMES.
    lv_xml17 = |<RecordSet id="{ sy-index }">|.
    lv_xml18 = '        <Master name="ima_file">'.
    lv_xml19 = '          <Record>'.
    lv_xml20 = '            <Field name="ima02" value="PO152选择焊治具"/>'.
    lv_xml201 = '           <Field name="ima021" value="不含钛合金"/>'.
    lv_xml21 = '            <Field name="ima24" value="N"/>'.
    lv_xml22 = '            <Field name="ima44" value="套"/>'.
    lv_xml23 = '            <Field name="ima25" value="套"/>'.
    lv_xml24 = '            <Field name="hsl" value="1"/>'.
    lv_xml25 = '            <Field name="ima08" value="P"/>'.
    lv_xml26 = '            <Field name="ima71" value="1080"/>'.
    lv_xml27 = '            <Field name="ima06" value="7001"/>'.
    lv_xml28 = '            <Field name="ima35" value="D016"/>'.
    lv_xml29 = '            <Field name="pinpai" value=""/>'.
    lv_xml30 = '            <Field name="ima09" value="MRO_04"/>'.
    lv_xml31 = '            <Field name="ima45" value="1"/>'.
    lv_xml32 = '            <Field name="ima46" value="1"/>'.
    lv_xml33 = '            <Field name="erjie" value="DZ_A03"/>'.
    lv_xml34 = '            <Field name="sanjie" value="0401"/>'.
    lv_xml35 = '            <Field name="gdfqun" value="400101"/>'.
    lv_xml36 = '            <Field name="plant" value="WCTZ"/>'.
    lv_xml37 = '            <Field name="ima43" value=""/>'.
    lv_xml38 = '          </Record>'.
    lv_xml39 = '        </Master>'.
    lv_xml40 = '      </RecordSet>'.
    CONCATENATE lv_xml_body lv_xml17 lv_xml18 lv_xml19 lv_xml20 lv_xml201 lv_xml21 lv_xml22 lv_xml23 lv_xml24 lv_xml25 lv_xml26 lv_xml27 lv_xml28 lv_xml29 lv_xml30
                lv_xml31 lv_xml32 lv_xml33 lv_xml34 lv_xml35 lv_xml36 lv_xml37 lv_xml38 lv_xml39 lv_xml40 into lv_xml_body.
ENDDO.

lv_xml41 = '    </Document>'.
lv_xml42 = '  </RequestContent>'.
lv_xml43 = '</Request>]]></tip:request>'.
lv_xml44 = '      </tip:OaMroItemRequest>'.
lv_xml45 = '   </soapenv:Body>'.
lv_xml46 = '</soapenv:Envelope>'.

CONCATENATE lv_xml41 lv_xml42 lv_xml43 lv_xml44 lv_xml45 lv_xml46 INTO lv_xml_foot.

CONCATENATE lv_xml_head lv_xml_body lv_xml_foot INTO lv_xml.

 

标签:xml,HTTP,string,lv,ABAP,client,http,TYPE,模板
来源: https://www.cnblogs.com/smarttony/p/16532532.html