NSURLErrorBadURL 错误

发布于 2024-08-26 08:21:05 字数 1813 浏览 9 评论 0原文

我的 iPhone 应用名为 Google 本地搜索(非 JavaScript 版本)开展一些搜索业务。

下面是我形成url的代码:

NSString *url = [NSString stringWithFormat:@"http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=%@", keyword];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"GET"];

//get response
NSHTTPURLResponse* urlResponse = nil;  
NSError *error = [[[NSError alloc] init] autorelease];  
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

当关键字引用英文字符时,它工作正常,但是当引用中文字符(以UTF8编码,例如'天安门',其UTF8编码为'e5a4a9 e5ae89 e997a8')时,它会报告 NSURLErrorBadURL 错误(-1000,当 URL 格式严重错误导致无法发起 URL 请求时返回)。为什么?

然后我进行进一步调查,我使用 Safari 并输入以下网址: http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=天安门

它也有效,并且输出我从 Macsniffer 得到的是: /ajax/services/search/local?v=1.0&q=%E5%A4%A9%E5%AE%89%E9%97%A8

所以我直接在我的 :

NSString *url = [NSString stringWithFormat:@"http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=%E5%A4%A9%E5%AE%89%E9%97%A8"];

我从 Macsniffer 得到的是其他东西 /ajax/services/search/local?v=1.0&q=1.687891E-28750X1.417C0001416CP-102640X1.4CC2D04648FBP-9999-1.989891E+0050X1.20DC00184CC67P-953E8E99A8

> 看来我的关键字“% E5%A4%A9%E5%AE%89%E9%97%A8”被翻译成其他东西。那么如何才能形成一个有效的url呢?我确实需要帮助!

My iphone app called Google Local Search(non javascript version) to behave some search business.

Below is my code to form a url:

NSString *url = [NSString stringWithFormat:@"http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=%@", keyword];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"GET"];

//get response
NSHTTPURLResponse* urlResponse = nil;  
NSError *error = [[[NSError alloc] init] autorelease];  
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

When the keyword refers to english characters, it works fine, but when refers to chinese characters(encoded in UTF8, such as '天安门' whose UTF8 code is 'e5a4a9 e5ae89 e997a8'), it will report NSURLErrorBadURL error(-1000, Returned when a URL is sufficiently malformed that a URL request cannot be initiated). Why?

Then I carry out further investigation, I use Safari and type in the url below:
http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=天安门

It also works, and the output I got from Macsniffer is:
/ajax/services/search/local?v=1.0&q=%E5%A4%A9%E5%AE%89%E9%97%A8

So I write a testing url directly in my app

NSString *url = [NSString stringWithFormat:@"http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=%E5%A4%A9%E5%AE%89%E9%97%A8"];

And what I got from the Macsniffer is some other thing:
/ajax/services/search/local?v=1.0&q=1.687891E-28750X1.417C0001416CP-102640X1.4CC2D04648FBP-9999-1.989891E+0050X1.20DC00184CC67P-953E8E99A8

It seems my keyword "%E5%A4%A9%E5%AE%89%E9%97%A8" was translated into something else. So how can I form a valid url? I do need help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷清清 2024-09-02 08:21:05

您是否尝试过对搜索字符串进行编码:

NSString* escapedKeyword = [keyword stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

Have you tried encoding the search string:

NSString* escapedKeyword = [keyword stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文