【发布时间】:2012-02-17 10:56:54
【问题描述】:
我在为我的 Objective C 代码转换这个 curl 命令时遇到了困难:
curl -u 0ef106c78146a23becba9105d1e:X -H "Accept: application/json" -H "Content-Type: application/json" https://boomboom.c27.imonggo.com/api/products.json
这是我的目标 C 代码:
NSError *theError = NULL;
NSURL *theURL = [NSURL URLWithString:@"https://boomboom.c2.imonggo.com/api/products.json"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];
[theRequest setValue:@"0ef106c78146a23becba9105d1e" forHTTPHeaderField:@"username"];
[theRequest setValue:@"x" forHTTPHeaderField:@"password"];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept:"];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type:"];
NSURLResponse *theResponse = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
我的问题很可能是标题字段。
【问题讨论】:
-
我在你的 curl 命令中看到了
-u,在你的可变请求中看到了username和password。你知道你的服务器可以接受username和password这样的参数吗? -
错误是什么?不知道怎么申请?还是没有反应? (顺便说一句,你想设置标题字段的键而不带尾随冒号。)
-
@MichaelDautermann:就是这样,我不确定我的服务器如何接受用户名和密码的参数。 (顺便说一句,我正在使用 imonggo POS 的 API)。你知道我将如何解决这个问题吗?谢谢..
-
@H2CO3:它响应但我收到错误:rror Domain=NSURLErrorDomain Code=-1012“操作无法完成。(NSURLErrorDomain 错误-1012。)”UserInfo=0x6ab39d0 {NSErrorFailingURLKey= acebonita.c2.imonggo.com/api/products.json, NSErrorFailingURLStringKey=acebonita.c2.imonggo.com/api/products.json, NSUnderlyingError=0x6ab2f40 “操作无法完成。(kCFErrorDomainCFNetwork 错误 -1012。)”} 和 HTTP Basic:访问被拒绝。我猜我的标题字段格式错误。顺便说一句,感谢有关删除冒号的建议。
标签: iphone objective-c json curl