开发者

I couldn't send a request with Arabic letters in the URL

开发者 https://www.devze.com 2023-04-12 11:45 出处:网络
I\'m developing an iPhone app that takes advantage of Google search api开发者_如何转开发 and search through Arabic content.

I'm developing an iPhone app that takes advantage of Google search api开发者_如何转开发 and search through Arabic content.

Whenever I send a request that includes arabic letters in the URL to get a JSON file format I get error message indicates that the request has failed. this is what the request looks like:

https://www.googleapis.com/customsearch/v1?oe=utf-8&ie= ISO-8859-6&key=[MY KEY]&cx=013036536707430787589:_pqjad5hr1a&q=جوجل&alt=json

This is the method in my code that invokes the request and retrieves the data:

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.googleapis.com/customsearch/v1?oe=utf-8&ie= ISO-8859-6&key=[MY KEY]&cx=013036536707430787589:_pqjad5hr1a&q=جوجل&alt=json"]] delegate:self];


You have to use percent-encoding in the URLs to make things work as you cannot just pass any arbitrary string in a URL. See this question for more information. Basically, it boils down to running your string through the stringByAddingPercentEscapesUsingEncoding: method. Since this method supports UTF-8 encoding only, you'll also have to pass ie=utf-8 in the URL instead of ie=ISO-8859-6.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号