卷曲到 AFNetworking?
Posted
技术标签:
【中文标题】卷曲到 AFNetworking?【英文标题】:Curl to AFNetworking? 【发布时间】:2014-05-29 21:46:13 【问题描述】:我正在尝试从原生 ios 应用程序使用 paypal Express 结账(不能使用 paypal SDK,因为苹果需要我通过 safari 进行付款)。问题是当我尝试调用 SetExpressCheckout API 方法时。这是他们在网站上提供的 curl 声明:
curl -s --insecure https://api-3t.sandbox.paypal.com/nvp -d
"USER=callerID # User ID of the PayPal caller account
&PWD=callerPswd # Password of the caller account
&SIGNATURE=callerSig # Signature of the caller account
&METHOD=SetExpressCheckout
&VERSION=93
&PAYMENTREQUEST_0_PAYMENTACTION=SALE # type of payment
&PAYMENTREQUEST_0_AMT=19.95 # amount of transaction
&PAYMENTREQUEST_0_CURRENCYCODE=USD # currency of transaction
&RETURNURL=http://www.example.com/success.html # URL of your payment confirmation page
&CANCELURL=http://www.example.com/cancel.html" # URL redirect if customer cancels payment
我需要将此传递给 AFNetworking,以便我可以在应用程序中执行此操作,我已经尝试过,但它不起作用:
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: kPaypalClientId,@"USER",
kPaypalPassword, @"PWD",
kPaypalSignature, @"SIGNATURE",
@"SetExpressCheckout", @"METHOD",
@"93", @"VERSION",
@"SALE",@"PAYMENTREQUEST_0_PAYMENTACTION",
@"19.95",@"PAYMENTREQUEST_0_AMT",
@"USD", @"PAYMENTREQUEST_0_CURRENCYCODE",
@"shareapp://bien",@"RETURNURL",
@"shareapp://cancel", @"CANCELURL", nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[manager POST:@"https://api-3t.sandbox.paypal.com/nvp" parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject)
NSLog(@"%@", responseObject);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=%@",tokenValue]]];
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@", error);
];
这是我得到的错误:
Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/plain" UserInfo=0xba7de40 NSErrorFailingURLKey=https://api-3t.sandbox.paypal.com/nvp, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xd06e720> URL: https://api-3t.sandbox.paypal.com/nvp status code: 200, headers
Connection = "keep-alive";
"Content-Length" = 230;
"Content-Type" = "text/plain; charset=utf-8";
DC = "origin1-api-3t.sandbox.paypal.com";
Date = "Tue, 15 Apr 2014 13:36:59 GMT";
Server = Apache;
"Set-Cookie" = "DC=origin1-api-3t.sandbox.paypal.com; secure";
, NSLocalizedDescription=Request failed: unacceptable content-type: text/plain
谢谢你,对不起我的英语!!
【问题讨论】:
【参考方案1】:这条线为我解决了这个问题:
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
【讨论】:
正确。默认响应序列化程序是AFJSONResponseSerializer
。由于 PayPal API 返回纯文本响应,您需要使用AFHTTPRresponseSerializer
。【参考方案2】:
发帖到Paypal服务器时需要设置header:Content-Type: application/x-www-form-urlencoded
。
【讨论】:
我使用了[manager.requestSerializer setValue:@"application/x-www-form- urlencoded" forHTTPHeaderField:@"Content-Type"];
但我仍然遇到同样的错误
这不是问题中给出错误的原因以上是关于卷曲到 AFNetworking?的主要内容,如果未能解决你的问题,请参考以下文章
AFNetworking:如何知道响应是不是使用缓存? 304 或 200