Ios 目标C相当于使用JSON的cURL

Ios 目标C相当于使用JSON的cURL,ios,json,cocoa,nsurlconnection,Ios,Json,Cocoa,Nsurlconnection,我试着四处寻找,但找不到确切的答案 我的朋友通过使用cURL实现了以JSON格式返回数据的网站,如下所示 curl http://example.com//api/v1/opens_set_current_position -d '{"latitude":"53.041", "longitude":"-2.90545", "radius":"100000"}' -X GET -H "Content-type: application/json" 我试图编写代码,使我的iOS应用程序能够检索数据

我试着四处寻找,但找不到确切的答案

我的朋友通过使用cURL实现了以JSON格式返回数据的网站,如下所示

curl http://example.com//api/v1/opens_set_current_position -d '{"latitude":"53.041", "longitude":"-2.90545", "radius":"100000"}' -X GET -H "Content-type: application/json"
我试图编写代码,使我的iOS应用程序能够检索数据。但我不能。我总是得到错误的数据甚至无法通过NSURLConnection加载。 这是我的密码:

NSString *requestString = [NSString stringWithFormat:
                         @"http://example.com//api/v1/opens_set_current_position"];

NSURL *url = [NSURL URLWithString:requestString];

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"GET"];

[req addValue:@"application/json" forHTTPHeaderField:@"Content-type"];

NSString *dataString = @"{\"latitude\":\"53.041\", \"longitude\":\"-2.90545\", \"radius\":\"100000\"}";

NSData *requestData = [NSData dataWithBytes:[dataString UTF8String] length:[dataString length]];

[req setHTTPBody:requestData];

NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:req returningResponse:&theResponse error:&theError];
数据一点都没有。我收到以下错误消息:

无法完成该操作。(KCFerrorDomainCFN网络错误303。)


有人能帮忙吗

这是因为您在
GET
请求中传递了一个body。您需要一个
POST
请求

curl
手册页:

-d/--data <data>
(HTTP)  Sends  the  specified data in a POST request to the HTTP server,
in the same way that a browser does when a user has filled in an HTML form
and presses the submit button. This will cause curl to pass the data to the
server using the content-type application/x-www-form-urlencoded.
-d/--数据
(HTTP)将POST请求中的指定数据发送到HTTP服务器,
与用户填写HTML表单时浏览器所做的相同
然后按下提交按钮。这将导致curl将数据传递给
使用内容类型application/x-www-form-urlencoded的服务器。

感谢您的及时回复。如果我将httpMethod设置为“POST”,则(kCFErrorDomainCFNetwork error 303.)会消失,但我仍然无法获得所需的数据,返回的数据带有“500”代码。这似乎是服务器端的问题。无论如何,谢谢。@KongHantrakool我想我已经回答了您提出的问题。