调试中的结果如图:
代码:
DATA: http_client TYPE REF TO if_http_client. "URL填全部,带参数 CALL METHOD cl_http_client=>create_by_url EXPORTING url = 'http://www.baidu.com' IMPORTING client = http_client. http_client->propertytype_logon_popup = http_client->co_enabled . http_client->propertytype_redirect = http_client->co_disabled . CALL METHOD http_client->request->set_header_field EXPORTING name = '~request_method' value = 'GET'. "如果用POST status code为302 不懂啥意思 CALL METHOD http_client->request->set_header_field EXPORTING name = '~request_protocol' "http请求则改为'~service_protocol' 好像两个都可以 value = 'HTTPS/1.1'. " 'HTTP/1.1' CALL METHOD http_client->request->set_header_field EXPORTING name = 'Content-Type' value = 'text/html'. " text/html 看情况填application/json 可能用POST * "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. IF sy-subrc <> 0. DATA:l_sysubrc TYPE sysubrc, l_error_text TYPE string. l_sysubrc = sy-subrc. CALL METHOD http_client->get_last_error IMPORTING code = l_sysubrc message = l_error_text. ENDIF. "Read HTTP RETURN CODE DATA:http_status_code TYPE i, status_text TYPE string. CALL METHOD http_client->response->get_status IMPORTING code = http_status_code reason = status_text . WRITE: / 'HTTP_STATUS_CODE = ', http_status_code, / 'STATUS_TEXT = ', status_text. "READ RESPONSE DATA DATA:w_result TYPE string. CALL METHOD http_client->response->get_cdata RECEIVING data = w_result . BREAK-POINT. * 获取返回的数据 DATA: r_value TYPE string, r_value1 TYPE string, r_fields TYPE tihttpnvp, r_h_fields TYPE tihttpnvp. http_client->response->get_header_fields( CHANGING fields = r_h_fields ). "返回表数据 BREAK-POINT. ****注意: ****1.url必须带"http://"或者“https://”。 ****2. http_client->propertytype_redirect = http_client->co_disabled .这一行是设置是否允许重定向,是为“co_enabled”,否为“co_disabled”。 ****3. 可能出现的错误 404 Hostname Unknow,是因为服务器未配置该url的dns ****4. 可能出现的错误 401 refused,可能是被防火墙挡住了,以上两个问题可以找basis协助解决