objective-c - 转义字符百分比问题,XCode 中的 HTTP API 请求

标签 objective-c ios macos

我一直在开发一个Mac应用程序,我正在尝试从github API发出一个Get HTTP请求,但是这个请求是一个条件请求,它看起来像这样:

https://api.github.com/repos/soviettoly/sandbox/events -H "If-Modified-Since: Sat, 13 Oct 2012 23:35:10 GMT"

当我根据该请求执行curl -i 时,我得到了我想要的一切。然而,我一直在 XCode 中尝试这样做,并从 github 得到了 404。

这就是我提出请求的方式:

NSMutableString * theURL = [[NSMutableString alloc]initWithString:@"https://api.github.com/repos/soviettoly/sandbox/events -H \"If-Modified-Since: Sat, 13 Oct 2012 23:35:10 GMT\""];

NSLog(@"the normal %@",theURL);
NSString * escaped = [theURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"the escpaed %@", escaped);
NSURL * test = [NSURL URLWithString: escaped];
NSLog(@"actual URL %@",test);
NSURLRequest * request = [NSURLRequest requestWithURL:test];
[[NSURLConnection alloc]initWithRequest:request delegate:self];

NSLog 命令的打印输出给了我这个:

the normal https://api.github.com/repos/soviettoly/sandbox/events -H "If-Modified-Since: Sat, 13 Oct 2012 23:35:10 GMT"
the escpaed https://api.github.com/repos/soviettoly/sandbox/events%20-H%20%22If-Modified-Since:%20Sat,%2013%20Oct%202012%2023:35:10%20GMT%22
actual URL https://api.github.com/repos/soviettoly/sandbox/events%20-H%20%22If-Modified-Since:%20Sat,%2013%20Oct%202012%2023:35:10%20GMT%22

我不知道为什么curl命令给我返回正确的结果,而在XCode中发出请求却没有。我尝试过不执行转义字符,但 XCode 不喜欢该 URL,因为它包含非法字符。我不知道如何在 XCode 中进行这种调用。我一直在对 GitHub 进行其他 API 调用,没有问题,只是在这个方面遇到了问题。如果有人能帮忙那就太好了。非常感谢!

最佳答案

据推测,您正在使用的 curl 命令是

curl https://api.github.com/repos/soviettoly/sandbox/events -H "If-Modified-Since: Sat, 13 Oct 2012 23:35:10 GMT"

这不是请求页面 'https://api.github.com/repos/soviettoly/sandbox/events -H "If-Modified-Since: Sat, 13 Oct 2012 23:35:10 GMT"',它正在请求页面'https://api.github.com/repos/soviettoly/sandbox/events ',并发送一个额外的 HTTP header (-H),其中包含“If-Modified-Since: Sat, 13 Oct 2012 23:35:10 GMT”。

您的 Objective-C 代码正在请求页面 'https://api.github.com/repos/soviettoly/sandbox/events -H "If-Modified-Since: Sat, 13 Oct 2012 年 23:35:10 GMT””。您需要使用 NSMutableURLRequest 并将其设置为在对 的请求中包含 header If-Modified-Since: Sat, 13 Oct 2012 23:35:10 GMT >https://api.github.com/repos/soviettoly/sandbox/events

例如

NSURL *url = [NSURL URLWithString:@"http://api.github.com/repos/soviettoly/sandbox/events"];
NSMutableURLRequest *request = [NSMutableURLRequest requestForURL:url];
[request setValue:@"Sat, 13 Oct 2012 23:35:10 GMT" forHTTPHeaderField:@"If-Modified-Since"];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];

关于objective-c - 转义字符百分比问题,XCode 中的 HTTP API 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12878751/

相关文章:

ios - Collectionview 自定义单元格布局/行为

objective-c - Xcode 中的私有(private)方法

cocoa - 以编程方式更改 Mac 音量

macos - 在 Dreamweaver for MAC 上更改行尾(以便文件在 linux 上工作)

ios - 调整 CGPath 大小

java - 使用 Java 编写 Mac 应用程序包/文件夹

水平滚动方向的 iOS7 UICollectionView 不起作用

iPhone:如何将 UITextField 和 UITableView 放在同一个 View 上?推荐方式

ios - 应用程序在后台时是否可以使用 AVAssetExportSession `exportAsynchronouslyWithCompletionHandler:` 方法?

ios - 找不到 Objective-C 桥接头部分