3. 注意點(diǎn), URL的中文轉(zhuǎn)碼
-
注意, 如果請(qǐng)求的URL中存在中文的話, 那么請(qǐng)求發(fā)送就會(huì)失敗
Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL"
因此, 當(dāng)你的URL請(qǐng)求中存在中文, 如:
http://192.168.2.109/login?username=我的賬戶&password=123-
這時(shí), 需要將URL轉(zhuǎn)碼為服務(wù)器可識(shí)別的UTF8字符串才可以
// 1. 當(dāng)請(qǐng)求地址中,包含中文,需要轉(zhuǎn)碼 NSString *URLStr = @"http://120.25.226.186:32812/login2?username=我的賬戶&pwd=520it&type=JSON"; NSLog(@"轉(zhuǎn)碼前:%@", URLStr); URLStr = [URLStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"轉(zhuǎn)碼后:%@", URLStr); 轉(zhuǎn)碼前:http://120.25.226.186:32812/login2?username=我的賬戶&pwd=520it&type=JSON 轉(zhuǎn)碼后:http://120.25.226.186:32812/login2?username=%E6%88%91%E7%9A