首页 > 代码库 > LR URL编码和解码方法
LR URL编码和解码方法
问题:URL=http://www.baidu.com/s?wd=%E6%B5%B7%E6%B7%80%E9%BB%84%E5%BA%84"中要对%E6%B5%B7%E6%B7%80%E9%BB%84%E5%BA%84进行参数化,但是由“中国”进行URL编码过来的(原因:URL的传参值,中文是无法进行ASCII编码的),所以无法直接用中文参数化,由于LR提供了lr_convert_string_encoding方法,所以要将中文进行URL编码和解码,是很容易的,故解决方法如下:
//URL编码
lr_convert_string_encoding("中国",LR_ENC_SYSTEM_LOCALE, LR_ENC_UTF8 , "UnicodeString");
lr_save_string (lr_eval_string("{UnicodeString}"),"nURLencoder" );
web_convert_param("nURLencoder", "SourceEncoding=PLAIN", "TargetEncoding=URL",LAST );
lr_output_message("Url编码 = %s",lr_eval_string("{nURLencoder}"));
//URL解码
lr_save_string (lr_eval_string("{nURLencoder}"),"URLencoder" );
web_convert_param("URLencoder", "SourceEncoding=URL", "TargetEncoding=PLAIN",LAST );
lr_convert_string_encoding(lr_eval_string("{URLencoder}"),LR_ENC_UTF8 ,LR_ENC_SYSTEM_LOCALE, "UnicodeString1");
lr_output_message("Url解码 = %s",lr_eval_string("{UnicodeString1}"));
---------------------------------------------------------------------------------------------------
///
/// url编码
///
lr_convert_string_encoding(lr_eval_string("{NewParam}"),LR_ENC_SYSTEM_LOCALE, LR_ENC_UTF8 , "UnicodeString");
lr_save_string (lr_eval_string("{UnicodeString}"),"nURLencoder" );
web_convert_param("nURLencoder", "SourceEncoding=PLAIN", "TargetEncoding=URL",LAST );
//lr_output_message("nURLencoder=%s",lr_eval_string("{nURLencoder}"));
///
/// url解码
///
lr_save_string(lr_eval_string("{nURLencoder}"),"URLencoder");
web_convert_param("URLencoder", "SourceEncoding=URL", "TargetEncoding=PLAIN",LAST );
//lr_output_message("URLencoder=%s",lr_eval_string("{URLencoder}"));
lr_convert_string_encoding(lr_eval_string("{URLencoder}"),LR_ENC_UTF8, LR_ENC_SYSTEM_LOCALE , "UnicodeString2");
//lr_output_message("UnicodeString=%s",lr_eval_string("{UnicodeString}"));
lr_error_message("原始值为=%s,编码为=%s,解码为=%s",lr_eval_string("{NewParam}"),lr_eval_string("{nURLencoder}"),lr_eval_string("{UnicodeString2}"));
LR URL编码和解码方法