【问题标题】:iOS Bad URL for NSUrlConnectionNSUrlConnection 的 iOS 错误 URL
【发布时间】:2012-10-01 01:57:18
【问题描述】:

我在一个名为 myUrl 的 NSString 中有一个 url。当我 NSLog myUrl 我看到以下内容:

http://dl.dropbox.com/u/57665723%2FChocolatesRUs%2FJellies%2FUn-sugared%2Fgen%20no%20sugar.pdf

然后我尝试使用以下网址进行连接:

NSURL* url = [ NSURL URLWithString:nextFileURL ];
NSMutableURLRequest* request =  [ [ NSMutableURLRequest alloc ] initWithURL: url ];
NSURLConnection* conn = [ [ NSURLConnection alloc ] initWithRequest: request delegate: self ];

我收到以下错误:

errorcode=-1000, error=Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x27e540 {NSUnderlyingError=0x26d800 "bad URL", NSLocalizedDescription=bad URL}

我尝试过使用

NSString* myUrl = [myUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

但这也不起作用。

谁能看出问题所在?

【问题讨论】:

    标签: objective-c ios nsurlconnection nsurl nsurlrequest


    【解决方案1】:

    也许您可以使用stringByAddingPercentEscapesUsingEncoding 而不是stringByReplacingPercentEscapesUsingEncoding 来转义您的网址。

    【讨论】:

      【解决方案2】:

      stringByAddingPercentEscapesUsingEncoding 是去掉空格加百分号

      NSString *urlString =[NSString stringWithFormat:@"&street=%@&street2=&city=%@&state=%@&
      zipcode=%@&candidates=10", address.address2,address.city, address.state, address.zip5];
      NSLog(@"BAD URL - %@",urlString ); // there is space in url
      
      NSString *encodedUrl = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
      NSLog(@" CORRECT URL - %@", encodedUrl); // url encode that space by %20
      
      
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL RLWithString:encodedUrl]];
      

      【讨论】:

        猜你喜欢
        • 2017-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-26
        • 1970-01-01
        • 2018-07-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多