xcode 相当于 curl 请求的 Objective-C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11451205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:52:04  来源:igfitidea点击:

Objective-C equivalent of curl request

iphoneobjective-ciosxcodecurl

提问by Lloyd Powell

I'm trying to manipulate this curl request in Objective-C:

我试图在 Objective-C 中操作这个 curl 请求:

curl -u username:password "http://www.example.com/myapi/getdata"

I've implemented the following and I'm getting a data get error Domain=kCFErrorDomainCFNetwork Code=303with NSErrorFailingURLKey=http://www.example.com/myapi/getdata:

我实现了以下内容,我得到一个数据出现错误Domain=kCFErrorDomainCFNetwork Code=303NSErrorFailingURLKey=http://www.example.com/myapi/getdata

// Make a call to the API to pull out the categories
NSURL *url = [NSURL URLWithString:@"http://www.example.com/myapi/getdata"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

// Create the username:password string for the request
NSString *loginString = [NSString stringWithFormat:@"%@:%@", API_USERNAME, API_PASSWORD];

// Create the authorisation string from the username password string
NSData *postData = [loginString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

[request setURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

I was hoping that someone could spot where I am going wrong with my attempt to manipulate the curl request and point me in the correct direction. Is there something obvious I'm missing out? The data returned from the API is in a JSON format.

我希望有人能发现我试图操纵 curl 请求时出错的地方,并指出我正确的方向。有什么明显的我错过了吗?API 返回的数据为 JSON 格式。

回答by Lloyd Powell

I found that the best thing to do was to not attempt the authentication within the code and to put it straight in the URL itself. The working code looks as follows:

我发现最好的做法是不要尝试在代码中进行身份验证,而是将其直接放在 URL 本身中。工作代码如下所示:

NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"http://%@:%@@www.example.com/myapi/getdata", API_USERNAME, API_PASSWORD]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

回答by lyptt

This guide seems to do what you're looking for: http://deusty.blogspot.co.uk/2006/11/sending-http-get-and-post-from-cocoa.html

本指南似乎可以满足您的需求:http: //deusty.blogspot.co.uk/2006/11/sending-http-get-and-post-from-cocoa.html

Just as an FYI, a lot of classes accept initWithData, and NSDatahas a method dataWithContentsOfURL, if you want to avoid setting up NSURLConnectionsyourself this could be an easier way of achieving what you're looking for.

仅供参考,很多类都接受 initWithData,并且NSData有一个方法dataWithContentsOfURL,如果您想避免设置NSURLConnections自己,这可能是实现您正在寻找的东西的更简单方法。