gpt4 book ai didi

ios - 在 Objective-C 中执行基于 curl 的操作

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:05:08 29 4
gpt4 key购买 nike

我正在尝试在 Objective-C 中实现以下目标:

curl -X POST -u "<application key>:<master secret>" \
-H "Content-Type: application/json" \
--data '{"aps": {"badge": 1, "alert": "The quick brown fox jumps over the lazy dog."}, "aliases": ["12345"]}' \
https://go.urbanairship.com/api/push/

我可以使用某种库来实现这一点吗?显然,我已准备好所有值,可以继续提出请求,但我不确定如何在 Objective-C 中执行此操作。

我使用的是 TouchJSON,但是我不太确定如何在上面构建正确的 JSON 有效负载并将其发布到服务器(而且我更希望这是一个异步请求而不是同步请求)。

NSError *theError = NULL;

NSArray *keys = [NSArray arrayWithObjects:@"aps", @"badge", @"alert", @"aliases", nil];
NSArray *objects = [NSArray arrayWithObjects:?, ?, ?, ?, nil];
NSDictionary *theRequestDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSURL *theURL = [NSURL URLWithString:@"https://go.urbanairship.com/api/push/"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];
[theRequest setHTTPMethod:@"POST"];

[theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSData *theBodyData = [[CJSONSerializer serializer] serializeDictionary:theRequestDictionary error:&theError];
[theRequest setHTTPBody:theBodyData];

NSURLResponse *theResponse = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
NSDictionary *theResponseDictionary = [[CJSONDeserializer deserializer] deserialize:theResponseData error:&theError];

最佳答案

NSURL *url = [NSURL URLWithString:@"https://go.urbanairship.com/api/push/"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
//... set everything else
NSData *res = [NSURLConnection sendSynchronousRequest:req returningResponse:NULL error:NULL];

或发送异步请求

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:some];

看看NSMutableURLRequest Class Reference看看如何设置。

关于ios - 在 Objective-C 中执行基于 curl 的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5991789/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com