2015-04-15 30 views
0

AFNetworking如何发送一串请求?AFNetworking如何发送一串邮寄请求?

//restrinBASE64STRING is a NSString ,Program error 
[manager POST:URLString parameters:restrinBASE64STRING success:^(AFHTTPRequestOperation *operation, id responseObject) { 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

}]; 
+0

默认情况下,AFNetworking假定该请求是形式'键1 =值&键2 = value2'的标准'应用程序/ x-www-窗体urlencoded'请求。它也可以轻松地使用不同的请求序列化程序来发送JSON请求。 '参数'不应该是一个简单的字符串。然而,目前尚不清楚你的服务器需要这种base64字符串的格式。在什么'Content-Type'是你的服务器期望的请求?它预计base64字符串本身是非常不寻常的。 – Rob

+0

如果你真的需要发送一个非标准的请求,你不会使用'POST',而是你可能会创建你自己的'AFHTTPRequestOperation'对象。但在我们走上这条路之前,确认你的服务器确实期待'POST'请求​​的这种好奇的格式可能是有意义的。 – Rob

+0

解决了这个问题,非常感谢 –

回答

1

问题解决了

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:[NSURL URLWithString:URLString]]; 
[request setHTTPMethod:@"POST"]; 
NSMutableData *postBody = [NSMutableData data]; 
[postBody appendData:[restrinBASE64STRING dataUsingEncoding:NSUTF8StringEncoding]]; 

[request setHTTPBody:postBody]; 
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request]; 

operation.responseSerializer = [AFHTTPResponseSerializer serializer]; 

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

}]; 
[[NSOperationQueue mainQueue] addOperation:operation];