如何发送POST和GET请求?
我想将我的 JSON
发送到 URL(POST
和 GET
)。
NSMutableDictionary *JSONDict = [[NSMutableDictionary alloc] init];
[JSONDict setValue:"myValue" forKey:"myKey"];
NSData *JSONData = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:nil];
我当前的请求代码不起作用。
NSMutableURLRequest *requestData = [[NSMutableURLRequest alloc] init];
[requestData setURL:[NSURL URLWithString:@"http://fake.url/"];];
[requestData setHTTPMethod:@"POST"];
[requestData setValue:postLength forHTTPHeaderField:@"Content-Length"];
[requestData setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestData setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[requestData setHTTPBody:postData];
使用ASIHTTPRequest
不是一个可靠的答案。
I want to send my JSON
to a URL (POST
and GET
).
NSMutableDictionary *JSONDict = [[NSMutableDictionary alloc] init];
[JSONDict setValue:"myValue" forKey:"myKey"];
NSData *JSONData = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:nil];
My current request code isn't working.
NSMutableURLRequest *requestData = [[NSMutableURLRequest alloc] init];
[requestData setURL:[NSURL URLWithString:@"http://fake.url/"];];
[requestData setHTTPMethod:@"POST"];
[requestData setValue:postLength forHTTPHeaderField:@"Content-Length"];
[requestData setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestData setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[requestData setHTTPBody:postData];
Using ASIHTTPRequest
is not a liable answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 iOS 中发送
POST
和GET
请求非常简单;并且不需要额外的框架。POST
请求:我们首先将
POST
的body
(因此,我们想要发送的内容)创建为NSString
,并将其转换为NSData
。objective-c
接下来,我们读取
postData
的length
,以便我们可以在请求中传递它。现在我们已经有了要发布的内容,我们可以创建一个
NSMutableURLRequest
,并包含我们的postData
。swift
最后,我们可以发送我们的请求,并且通过创建新的
NSURLSession
来读取回复:目标-c
swift
GET
请求:使用
GET
请求基本上是一样的,只是没有HTTPBody
和Content-Length
。objective-c
swift
在旁注中,您可以添加
Content-Type
(和其他数据),将以下内容添加到我们的NSMutableURLRequest
。服务器在请求时可能需要这样做,例如 json< /a>.objective-c
响应代码也可以使用
[(NSHTTPURLResponse*)response statusCode]
读取。swift
更新: < code>sendSynchronousRequest 已从 ios9 和 osx-elcapitan (10.11) 及之后。
Sending
POST
andGET
requests in iOS is quite easy; and there's no need for an additional framework.POST
Request:We begin by creating our
POST
'sbody
(ergo. what we'd like to send) as anNSString
, and converting it toNSData
.objective-c
Next up, we read the
postData
'slength
, so we can pass it along in the request.Now that we have what we'd like to post, we can create an
NSMutableURLRequest
, and include ourpostData
.swift
And finally, we can send our request, and read the reply by creating a new
NSURLSession
:objective-c
swift
GET
Request:With the
GET
request it's basically the same thing, only without theHTTPBody
andContent-Length
.objective-c
swift
On a side note, you can add
Content-Type
(and other data) by adding the following to ourNSMutableURLRequest
. This might be required by the server when requesting, e.g, a json.objective-c
Response code can also be read using
[(NSHTTPURLResponse*)response statusCode]
.swift
Update:
sendSynchronousRequest
is deprecated from ios9 and osx-elcapitan (10.11) and out.通过使用 RestKit 您可以发出一个简单的 POST 请求(请参阅此 GitHub 页面了解更多详细信息)。
在头文件中导入 RestKit 。
然后您可以开始创建一个新的
RKRequest
。然后指定您要发出的请求类型(在本例中为
POST
请求)。然后在
additionalHTTPHeaders
中将您的请求设置为 JSON。最后,您可以发送请求。
此外,您可以
NSLog
您的请求以查看结果。资料来源: RestKit.org 和 我。
By using RestKit you can make a simple POST request (see this GitHub page for more details).
Import RestKit in your header file.
Then you can start by creating a new
RKRequest
.Then specify what kind of request you want to make (in this case, a
POST
request).And then set your request as a JSON in
additionalHTTPHeaders
.Finally, you can send the request.
Additionally you can
NSLog
your request to see the outcome.Sources: RestKit.org and Me.
视图control.h
view.m
tablecell.h
view control.h
view.m
tablecell.h