2015-04-15 26 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];