解决过程
1、cl_http_utility打开URL失败原因是URL地址有空格,考虑到有空格直接去掉(CONDENSE l_str NO-GAPS),结果去掉之后URL打开是一推乱码 失败告终!
2、既然浏览器能打开看看浏览器打开的时候空格转换成什么通用字符,浏览器直接把空格转换成%20,于是在程序debug里面把空格改成%20结果成功了!!
从度娘上找到URL相关转义字符:
+ URL 中+号表示空格 %2B
空格 URL中的空格可以用+号或者编码 %20
/ 分隔目录和子目录 %2F
? 分隔实际的URL和参数 %3F
% 指定特殊字符 %25
# 表示书签 %23
& URL 中指定的参数间的分隔符 %26
= URL 中指定参数的值 %3D
接下来把URL空格替换成%20过程
1、直接REPLACE ALL OCCURRENCES OF REGEX ' ' IN url WITH '%20'. 失败!! 直接dump,替换不支持空格替换成别的字符,只能把别的字符替换成空格。
2、度娘查到translate函数可以空格替换 url = translate( val = url from = ` ` to = `%20` ).结果只能替换一个字符
失败告终!
3、成功方案先用translate替换成别的字符,然后再用REPLACE替换
REPORT ztest_ma01.
DATA: lv_string TYPE string VALUE '这 是 一 个 测 试 字 符 串'.
lv_string = translate( val = lv_string from = ` ` to = `~` ).
REPLACE ALL OCCURRENCES OF REGEX '~' IN lv_string WITH '%20'.
WRITE: / lv_string.
成功源代码:
url = translate( val = url from = ` ` to = `~` ).
REPLACE ALL OCCURRENCES OF REGEX '~' IN url WITH '%20'.
lv_url = url..
* 必输
IF lv_url IS INITIAL.
code = 'F'.
msg = '文件地址必输'.
RETURN.
ENDIF.
* 检查地址格式
* TRANSLATE lv_url TO LOWER CASE.
l_pat = '^(((http|https|file)(:\/\/)).*)+$'.
CREATE OBJECT lv_reg EXPORTING pattern = l_pat.
CALL METHOD cl_http_utility=>if_http_utility~is_valid_url
EXPORTING
url = lv_url
white_pattern = lv_reg
restrictive = abap_true
RECEIVING
is_ok = lv_ok.
IF lv_ok IS INITIAL..
code = 'F'.
msg = '无效的url地址信息'.
RETURN.
ENDIF.
通过URL下载文件代码:
lv_url = url..
* 必输
IF lv_url IS INITIAL.
code = 'F'.
msg = '文件地址必输'.
RETURN.
ENDIF.
* 检查地址格式
* TRANSLATE lv_url TO LOWER CASE.
l_pat = '^(((http|https|file)(:\/\/)).*)+$'.
CREATE OBJECT lv_reg EXPORTING pattern = l_pat.
CALL METHOD cl_http_utility=>if_http_utility~is_valid_url
EXPORTING
url = lv_url
white_pattern = lv_reg
restrictive = abap_true
RECEIVING
is_ok = lv_ok.
IF lv_ok IS INITIAL..
code = 'F'.
msg = '无效的url地址信息'.
RETURN.
ENDIF.
* TRANSLATE lv_url TO LOWER CASE .
* TRANSLATE filelx TO LOWER CASE.
IF '.png,.jpg,.gif,.pdf'
CS filelx AND filelx IS NOT INITIAL .
CONCATENATE lv_url '?isLoad=1' INTO url.
ENDIF.
CALL FUNCTION 'CALL_BROWSER'
EXPORTING
url = url
EXCEPTIONS
frontend_not_supported = 1
frontend_error = 2
prog_not_found = 3
no_batch = 4
unspecified_error = 5
OTHERS = 6.
IF sy-subrc <> 0.
code = 'F'.
msg = '无效的文件地址'.
ENDIF.
标签:空格,20,cl,URL,lv,url,http,打开
From: https://blog.csdn.net/ma_edu/article/details/137131326