最近遇到一个怪异的url问题,我是在公司wiki上复制的url(类似http://baidu.com/),然后加上需要传给平台的参数,用AFNetworking的post请求执行该url。然后报:Invalid parameter not satisfying: URLString
。
但是url只有%、&、字母数字没有其他特殊字符。在执行中断点打印该url,放到浏览器中http
后面少了一个:
,手动加上:
请求正确。在程序中继续执行,报上面的错误,这是再复制打印的url到浏览器中,没有:
丢失的情况,请求正确。
之前遇到过一次这种情况,手动去打url,解决了问题。但是现在好像不管用了,而且我很纳闷的是为什么会有这个问题,搞不明白,大神指点指点啊:0—0:
Oh, you are passing the parameter in the URL. Is the parameter also a URL?
In this case, you need to URLEncode (encode) the URL of the parameter. There is a code, it is very simple URLEncode
The encoding operation will convert special characters that affect the integrity of the URL into percent encoding so that they will not be lost.
Since URL supports 26 English letters, numbers and a few special characters, when the URL contains non-standard URL characters, it needs to be encoded. iOS provides the function stringByAddingPercentEscapesUsingEncoding to encode Chinese and some special characters. However, the function of stringByAddingPercentEscapesUsingEncoding is not perfect and is invalid for some special characters. For these characters, you can use the CFURLCreateStringByteAddingPercentEscapes function
As follows:
Good luck to you.