0

我正在尝试通过 GET HTTP 请求从云中下载文件,并且在我的 REST 客户端(Chrome 的邮递员)中我得到了预期的结果,但是当我在 iOS 中传递请求时,我得到一个 NULL 响应。可能是什么问题呢?

在我的 REST 客户端中,我将 URLhttp://myserver.com/api/download.json?filepath=/&fileid=document:1pLNAbof_dbXGn43GtMNafABMAr_peTToh6wEkXlab7U&filename=My Info.doc与授权密钥的标头字段一起传递,它工作得非常好。在 iOS 中,如果我这样做:

[request setURL:[NSURL URLWithString:URLString]]; // I tried even to hardcode the 
                                                 // URLString to be one of the 
                                                // working URLs from the Postman 
                                               // Client, doesn't work as well.
[request setHTTPMethod:@"GET"];
NSData* response = [NSURLConnection sendSynchronousRequest:request 
                                          returningResponse:&urlResponseList
                                          error:&requestErrorList];

响应为空,我得到一个状态码 500。有人知道如何使它工作吗?谢谢。

4

2 回答 2

1

当 Web 服务无法处理您的请求时,会出现 HTTP 状态代码 500。发生内部错误。

尝试在您的请求中添加更多详细信息。

[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
于 2013-05-07T14:39:15.990 回答
1

看起来您的 URL 中有一个空格,您需要在将字符串发送到 NSURL 之前对其进行转义。

如果这不起作用,了解服务器的完整错误也是有帮助的。requestErrorList 包含什么?

但如果唯一的问题是非转义字符,这样的东西应该可以工作。

NSString *encodedString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[request setURL:[NSURL URLWithString:encodedString];
于 2013-05-07T14:39:17.613 回答