简体   繁体   English

为什么可可粉不喜欢此URL?

[英]Why doesn't Cocoa like this URL?

I'd like to submit to the following URL in a Cocoa app and save the results to a string (although XML is probably better): 我想提交到Cocoa应用程序中的以下URL并将结果保存到字符串(尽管XML可能更好):

http://translate.google.com/translate_t#en|fr|hi%20there%20all

The page keeps returning an error. 页面不断返回错误。 Here's my code: 这是我的代码:

NSString *urlString = @"http://translate.google.com/translate_t#en|fr|hi%20there%20all";
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10];
NSData *urlData;
NSURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];

NSString *content = [[NSString alloc]  initWithBytes:[urlData bytes]

The above causes the URL to turn into: 上面的内容导致网址变成:

http://translate.google.com/translate_t%23en%7Cfr%7Chi%2520there%2520all

Which results in a 404 from Google. 结果是Google的404。 If I try the URL without escaping, I get zero bytes returned. 如果我尝试使用URL而不进行转义,则会返回零字节。 Any ideas on how to make this work? 关于如何进行这项工作的任何想法?

--- EDIT --- -编辑-

The source of the problem is that the value of url is nil. 问题的根源是url的值为nil。 Remove the EscapesUsingEncoding line (line 2) and check the value of url. 删除EscapesUsingEncoding行(第2行),然后检查url的值。 I've the pipes in the URL are causing [NSURL URLWithString:urlString]; 我在URL中的管道导致[NSURL URLWithString:urlString]; to return nil. 返回零。 Escaping the pipes with their hex values, 7C ( http://translate.google.com/translate_t#en%7cfr%7chi%20there%20all ), returns data. 使用十六进制值7C( http://translate.google.com/translate_t#en%7cfr%7chi%20there%20all )对管道进行转义,将返回数据。 But then I get zero bytes again on the NSString *content line. 但是然后我又在NSString * content行上得到了零字节。

The problem is that you're escaping the # character, which is needed by Google Translate to figure out what's going on. 问题在于您要转义#字符,Google Translate需要用它来找出正在发生的情况。 In fact, given that the URL you are passing includes the %20 (space) then you don't actually need the URL escape code at all - you can just use your literal as is. 实际上,考虑到您传递的URL包含%20(空格),那么您实际上根本不需要URL转义代码-您可以按原样使用文字。

If you're constructing it from an NSTextField or similar, just run the URL escaping on the textValue before adding your prefix on it. 如果要从NSTextField或类似的对象构造它,只需在textValue上运行转义的URL,然后在其上添加前缀。

You seem to be double-escaping the percent signs (see that if you'll take the final string into Safari it wouldn't load either, giivng a 404). 您似乎在对百分号进行了转义(请注意,如果将最终的字符串带入Safari,则给出404也不会加载)。

This causes the %20 to become %2520 (the %25 is the % sign). 这将导致%20变为%2520(%25是%符号)。 Also, not escaping at all doesn't escape the # and | 另外,根本不逃避不能逃脱#和| signs. 迹象。

Your origin string should be: 您的原始字符串应为:

http://translate.google.com/translate_t#en|fr|hi there all

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM