在Objective-C中执行基于curl的操作 [英] Executing curl based actions in Objective-C

查看:159
本文介绍了在Objective-C中执行基于curl的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Objective-C中实现以下目的:

  curl -X POST -u< application key> ;:< master secret> \ 
-HContent-Type:application / json\
--data'{aps:{badge:1,alert: the lazy dog。},aliases:[12345]}'\
https://go.urbanairship.com/api/push/

有没有某种类型的库可以实现这个?显然,我有所有的价值准备好,并提出我的请求,但我不知道如何做的Objective-C。



我使用TouchJSON,但我不太确定如何构造正确的JSON有效载荷和POST POST到服务器(我也更喜欢这是一个异步请求而不是同步)。

  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 / jsonforHTTPHeaderField:@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];
// ...设置一切
NSData * res = [NSURLConnection sendSynchronousRequest:req returningResponse:NULL error:NULL];

或发送异步请求

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

查看 NSMutableURLRequest类参考以了解如何设置。


I am trying to achieve the following in 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/

Is there some sort of library I can use that achieves this? Obviously I have all of the values ready to go ahead and make my request, but I'm not exactly sure how to do it in Objective-C.

I am using TouchJSON, however I'm not quite sure how to construct the correct JSON payload above and POST this to the server (also I'd prefer this to be an asynchronous request rather than a synchronous).

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];

or send asynchronous request with

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

have a look at NSMutableURLRequest Class Reference to see how to set what.

这篇关于在Objective-C中执行基于curl的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆