SAP调用HTTP和HTTPS

HTTPS

https://archive.sap.com/discussions/thread/482084

Note 510007

 

 SAP Kernel版本为721     SAP ECC 6.0     SAP_BASIS700

调用公司的https还是不行     500 Native SSL error   不行

调用https://www.baifubao.com/callback?cmd=1059&callback=phone&phone=132*********

REPORT ZTEST040.


  DATA: LV_URL TYPE STRING,
        HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT,
        RETURN_STR TYPE STRING,
        LV_START TYPE I,
        LV_END TYPE I,
        LV_PARAM TYPE STRING,
        LV_BODY TYPE STRING,
        LV_LEN TYPE I .
  data: L_PHONE(20),
        L_STR TYPE STRING,
        str_in type STRING,
        str_out type STRING.
  data: wf_proxy type string ,
        wf_port type string .





  "HTTP
  str_in = '你猜'.
 "在abap中调用url时,先要对url中的汉字转码ASCII
  CALL METHOD cl_http_utility=>escape_url
        EXPORTING
          unescaped = str_in
        RECEIVING
          escaped   = str_out.

  CONCATENATE '/weather_mini?city=' str_out INTO LV_PARAM.


  call method cl_http_client=>create
    exporting
      host          = 'wthrcdn.etouch.cn'
      service       = '80'
      scheme        = '1'                        "http
      proxy_host    = wf_proxy
      proxy_service = wf_port
    importing
      client        = http_client.

  HTTP_CLIENT->PROPERTYTYPE_LOGON_POPUP = HTTP_CLIENT->CO_ENABLED .

  CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
    EXPORTING
      NAME  = '~request_method'
      VALUE = 'GET'.

  CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
    EXPORTING
      NAME  = '~server_protocol'
      VALUE = 'HTTP/1.1'.
  "host后的url
  call method http_client->request->set_header_field
    exporting
      name  = '~request_uri'
      value = LV_PARAM.

  CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
    EXPORTING
      NAME  = 'Content-Type'
      VALUE = 'application/json'.

*  "set body
*  CALL METHOD HTTP_CLIENT->REQUEST->SET_CDATA
*    EXPORTING
*      DATA   = LV_PARAM
*      OFFSET = 0
*      LENGTH = LV_LEN.

*  "get the param of url----没用
*  clear LV_PARAM.
*  CALL METHOD HTTP_CLIENT->REQUEST->GET_URI_PARAMETER
*    EXPORTING
*      NAME  = 'city'
*    RECEIVING
*      VALUE = LV_PARAM.

  CALL METHOD HTTP_CLIENT->SEND
    EXCEPTIONS
      HTTP_COMMUNICATION_FAILURE = 1
      HTTP_INVALID_STATE         = 2.
  CALL METHOD HTTP_CLIENT->RECEIVE
    EXCEPTIONS
      HTTP_COMMUNICATION_FAILURE = 1
      HTTP_INVALID_STATE         = 2
      HTTP_PROCESSING_FAILED     = 3.

* 获取返回的数据
  RETURN_STR = HTTP_CLIENT->RESPONSE->GET_CDATA( ).
  if RETURN_STR is INITIAL.
    write: /'用cl_http_client=>create方法:' ,/ 'faild'.
  else.
    write: /'用cl_http_client=>create方法:' ,/ RETURN_STR.
  endif.
  CALL METHOD HTTP_CLIENT->CLOSE.






  CONCATENATE 'http://wthrcdn.etouch.cn/weather_mini'
              '?'
              'city=' str_out INTO LV_URL.

"URL填全部,带参数
  CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
    EXPORTING
      URL    = LV_URL
    IMPORTING
      CLIENT = HTTP_CLIENT.

  HTTP_CLIENT->PROPERTYTYPE_LOGON_POPUP = HTTP_CLIENT->CO_ENABLED .

  CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
    EXPORTING
      NAME  = '~request_method'
      VALUE = 'GET'.

  CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
    EXPORTING
      NAME  = '~server_protocol'
      VALUE = 'HTTP/1.1'.

  CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
    EXPORTING
      NAME  = 'Content-Type'
      VALUE = 'application/json'. " text/html 看情况填

*  "set body
*  CALL METHOD HTTP_CLIENT->REQUEST->SET_CDATA
*    EXPORTING
*      DATA   = LV_PARAM
*      OFFSET = 0
*      LENGTH = LV_LEN.

  CALL METHOD HTTP_CLIENT->SEND
    EXCEPTIONS
      HTTP_COMMUNICATION_FAILURE = 1
      HTTP_INVALID_STATE         = 2.
  CALL METHOD HTTP_CLIENT->RECEIVE
    EXCEPTIONS
      HTTP_COMMUNICATION_FAILURE = 1
      HTTP_INVALID_STATE         = 2
      HTTP_PROCESSING_FAILED     = 3.

* 获取返回的数据
  RETURN_STR = HTTP_CLIENT->RESPONSE->GET_CDATA( ).
  if RETURN_STR is INITIAL.
    write: /'用CL_HTTP_CLIENT=>CREATE_BY_URL方法:' ,/ 'faild'.
  else.
    write: /'用CL_HTTP_CLIENT=>CREATE_BY_URL方法:' ,/ RETURN_STR.
  endif.
  CALL METHOD HTTP_CLIENT->CLOSE.



  call method cl_http_client=>create
    exporting
      host          = 'www.baifubao.com'
      service       = '443'
      scheme        = '2'                        "https
      SSL_ID        = 'ANONYM'
      proxy_host    = wf_proxy
      proxy_service = wf_port
    importing
      client        = http_client.

  HTTP_CLIENT->PROPERTYTYPE_LOGON_POPUP = HTTP_CLIENT->CO_ENABLED .

  CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
    EXPORTING
      NAME  = '~request_method'
      VALUE = 'GET'.

  CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
    EXPORTING
*      NAME  = '~server_protocol'
      name  = '~request_protocol'
      VALUE = 'HTTPS/1.1'.
  "host后的url
  call method http_client->request->set_header_field
    exporting
      name  = '~request_uri'
      value = '/callback?cmd=1059&callback=phone&&phone=13205334302'.

  CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
    EXPORTING
      NAME  = 'Content-Type'
*      VALUE = 'application/x-www-form-urlencoded'.
      VALUE = 'application/json'.


*  "set body
*  CALL METHOD HTTP_CLIENT->REQUEST->SET_CDATA
*    EXPORTING
*      DATA   = LV_BODY
*      OFFSET = 0
*      LENGTH = LV_LEN.

*  "get the param of url----没用
*  clear LV_PARAM.
*  CALL METHOD HTTP_CLIENT->REQUEST->GET_URI_PARAMETER
*    EXPORTING
*      NAME  = 'city'
*    RECEIVING
*      VALUE = LV_PARAM.

  CALL METHOD HTTP_CLIENT->SEND
    EXCEPTIONS
      HTTP_COMMUNICATION_FAILURE = 1
      HTTP_INVALID_STATE         = 2.
  CALL METHOD HTTP_CLIENT->RECEIVE
    EXCEPTIONS
      HTTP_COMMUNICATION_FAILURE = 1
      HTTP_INVALID_STATE         = 2
      HTTP_PROCESSING_FAILED     = 3.

* 获取返回的数据
  RETURN_STR = HTTP_CLIENT->RESPONSE->GET_CDATA( ).
  if RETURN_STR is INITIAL.
    write: /'HTTPS:' ,/ 'faild'.
  else.
    write: /'HTTPS:' ,/ RETURN_STR.
  endif.
  CALL METHOD HTTP_CLIENT->CLOSE.

 

转载于:https://www.cnblogs.com/CtrlS/p/10218438.html

你可能感兴趣的:(SAP调用HTTP和HTTPS)