AFHTTPRequestOperationManager的post方法
使用afnetworking2.0下,AFHTTPRequestOperationManager的post方法向服务器发送用户名和密码,参数名都正确且都已经赋值,为什么服务端接收到得数据是空的
+ (AFHTTPRequestOperationManager *)httpRequestOperationManager { NSURL *baseUrl = [NSURL URLWithString:@"http://192.168.1.234:8081"]; AFHTTPRequestOperationManager *httpRequestOperationManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseUrl]; //httpRequestOperationManager.requestSerializer = [AFJSONRequestSerializer serializer];之前这里没有注释掉,服务端接收不到post过去的参数,注释掉之后正常,目前还不清楚是什么原因 httpRequestOperationManager.responseSerializer = [AFJSONResponseSerializer serializer]; httpRequestOperationManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; // 检测网络情况 [httpRequestOperationManager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { switch (status) { case AFNetworkReachabilityStatusReachableViaWWAN: NSLog(@"当前网络可用"); break; case AFNetworkReachabilityStatusReachableViaWiFi: NSLog(@"当前网络可用"); break; case AFNetworkReachabilityStatusNotReachable: NSLog(@"当前网络不可用"); break; default: break; } }]; // 开启检测 [httpRequestOperationManager.reachabilityManager startMonitoring]; return httpRequestOperationManager; }
StackOverflow 封装方法
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6NDE0LCJhdCI6NCwiYnQiOjAsImNtIjoxODY2MjQsImNoIjoxMTc4LCJjciI6Njk1NzM5LCJkaSI6IjAwODU1M2JjYTM4NzRlZTQ5YTZmYmM5ZWYwN2U2ZDY3IiwiZG0iOjEsImZjIjo3MjE4MzMsImZsIjo0MzE3ODAsImlwIjoiMTAxLjIzMS42OS4xOTQiLCJrdyI6Im9iamVjdGl2ZS1jLGlvczcsYWZuZXR3b3JraW5nLTIsYWZodHRwcmVxdWVzdG9wZXJhdGlvbiIsIm53IjoyMiwicGMiOjAsInByIjoxNjA0LCJydCI6MSwicmYiOiJodHRwOi8vY24uYmluZy5jb20vc2VhcmNoP3E9QUZIVFRQUmVxdWVzdE9wZXJhdGlvbk1hbmFnZXImcXM9biZmb3JtPVFCUkUmcHE9YWZodHRwcmVxdWVzdG9wZXJhdGlvbm1hbmFnZXImc2M9MC0wJnNwPS0xJnNrPSZjdmlkPWFhMDA3MmY3ZmQxNDQ1OWZhMmUyNzkxMmFhMTBlNTEwIiwic3QiOjgyNzcsInVrIjoidWUxLWZkMGRiMWFmMmNlNjQwZjA4ODEyOTg0YmM5ODk3NWVjIiwiem4iOjQ0LCJ0cyI6MTQyMTIxNTQyNDY4MCwiYmYiOnRydWV9&s=otzh16ZbX_6CEN7u1Sclx9mfxHA)
The server is sending back a response with status code 500, which means that your server encountered an error while attempting to process the request. There doesn't appear to be anything wrong with how you're using AFNetworking, but the only way to tell is to debug things on the server side first.
It has been a while since I solved this but maybe this could still help someone. Eventually, in my case, the upper level httpPOSTMultiPartRequestWithPath method did not cut and I needed more flexibility in the message structure (for instance, setting custom boundaries). I ended up using the HTTPRequestOperationWithRequest method and created the URLrequest manually.
|