如何在 AFNetworking 的帮助下发出 Twilio api Post 请求?
Posted
技术标签:
【中文标题】如何在 AFNetworking 的帮助下发出 Twilio api Post 请求?【英文标题】:How to make Twilio api Post request with the help of AFNetworking? 【发布时间】:2015-08-05 08:06:08 【问题描述】:我想使用 AFNetworking 访问 Twilio api。我尝试了多种方法,但没有成功。如果有人使用 AFNetworking 发布请求,请帮助我。
案例 1:这是我的原生 Objective-c 工作代码。
NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages", kTwiliosID, kTwilioSecret, kTwilioSID];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
NSString *bodyString = [NSString stringWithFormat:@"From=%@&To=%@&Body=%@", from, to, message];
NSData *data = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
if (connectionError)
DLog(@"Error: %@", connectionError);
completionBlock(connectionError, NO);
else
completionBlock(connectionError, YES);
];
案例 2:使用 AFNetorking:代码不起作用:
代码:
NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages", kTwilioSID, kTwilioSecret, kTwilioSID];
NSDictionary *dict = @
@"From" : from,
@"To" : to,
@"Body" : message
;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:urlString parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject)
NSLog(@"Success: %@", responseObject);
failure:
^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@", error);
];
相关错误:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x1775e660 NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set., NSUnderlyingError=0x175101b0 "Request failed: bad request (400)"
案例 3:使用 AFNetorking:另一个同样不起作用的代码:
代码:
NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages", kTwilioSID, kTwilioSecret, kTwilioSID];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
NSString *bodyString = [NSString stringWithFormat:@"From=%@&To=%@&Body=%@", from, to, message];
NSData *data = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
NSLog(@"%@", responseObject);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"error: %@", error);
];
[operation start];
相关错误:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x177a3e70 NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.
谢谢。
【问题讨论】:
【参考方案1】:来自 Twilio 的 Ricky 在这里。首先是一个快速的免责声明。直接从 iOS 应用程序向 Twilio 发出请求需要您在应用程序中嵌入您的 Twilio 帐户凭据,这很危险。我的建议是从您的应用发出请求的服务器端脚本发送 SMS,以确保您的凭据安全。
话虽如此,您的代码非常接近。默认情况下,Twilio's REST API returns XML。如果您想解析默认返回的响应,您可以更新版本 2 中的代码以使用AFXMLParserResponseSerializer
:
operation.responseSerializer = [AFXMLParserResponseSerializer serializer];
如果您更愿意使用 JSON,则更新您发出 POST 请求的 Twilio URL 并表明您想要 JSON 响应:
NSString *urlString = [NSString stringWithFormat:@"https://%@:%@@api.twilio.com/2010-04-01/Accounts/%@/SMS/Messages.json", kTwilioSID, kTwilioSecret, kTwilioSID];
希望对您有所帮助。
【讨论】:
以上是关于如何在 AFNetworking 的帮助下发出 Twilio api Post 请求?的主要内容,如果未能解决你的问题,请参考以下文章
AFNetworking - 如何通过 ID 发出 POST 请求
如何使用 AFNetworking 在 iOS 目标 c 中发出此帖子请求
AFNetworking 将 NSMutableDictionary 作为 POST 发送
如何使用 AFNetworking 或 STHTTPRequest 发出 SOAP Web 服务请求?