2015-07-10 95 views
0

我知道有一些類似的問題,我一直堅持這一兩天。我有一個項目,我使用ASIHTTPRequest對REST服務器執行POST和GET請求。一切工作正常,但由於ASIHTTPRequest開發已經停止,我打算將所有請求遷移到AFNetworking。事實上,我正在嘗試它,但讀取服務器響應似乎是其中一個參數(GST)沒有被讀取。將來自AsiHTTPRequest的POST請求轉換爲AFNetworking

的ASIHTTPRequest(工作正常):

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:[[NSString stringWithFormat:@"%sexteriorroute/listall", KServerUrl] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; 
[request addRequestHeader:@"Content-Type" value:@"application/json;charset=UTF-8"]; 
[request addRequestHeader:@"Cookie" value:[NSString stringWithFormat:@"GST=%@", [user getToken]]]; 
[request setRequestMethod:@"POST"]; 
[request setPostBody:[NSMutableData dataWithData:[[NSString stringWithFormat:@"{\"filters\":\"{\\\"type\\\":\\\"FastFilter\\\",\\\"groups\\\":[{\\\"groupOp\\\":\\\"AND\\\",\\\"rules\\\":[{\\\"field\\\":\\\"lang\\\",\\\"op\\\":\\\"eq\\\",\\\"data\\\":\\\"%@\\\"}]}],\\\"rules\\\":[],\\\"groupOp\\\":\\\"AND\\\"}\"}", [user getLang]] dataUsingEncoding:NSUTF8StringEncoding]]]; 
[request setDelegate:self]; 
[request setDidFailSelector:@selector(getExteriorRoutesPreviewFailed:)]; 
[request setDidFinishSelector:@selector(getExteriorRoutesPreviewFinished:)]; 
[request startAsynchronous]; 

的AFNetworking請求(不工作):

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:[NSString stringWithFormat:@"%sexteriorroute/listall", KServerUrl] parameters:[NSDictionary dictionaryWithObject:[user getToken] forKey:@"GST"] error:nil]; 
[request setHTTPShouldHandleCookies:NO]; 
[request setValue:[NSString stringWithFormat:@"GST=%@", [user getToken]] forHTTPHeaderField:@"Cookie"]; 
NSData *postBody = [self base64DataFromString:[NSString stringWithFormat:@"{\"filters\":\"{\\\"type\\\":\\\"FastFilter\\\",\\\"groups\\\":[{\\\"groupOp\\\":\\\"AND\\\",\\\"rules\\\":[{\\\"field\\\":\\\"lang\\\",\\\"op\\\":\\\"eq\\\",\\\"data\\\":\\\"%@\\\"}]}],\\\"rules\\\":[],\\\"groupOp\\\":\\\"AND\\\"}\"}", [user getLang]]]; 
[request addValue:@"application/json;UTF-8" forHTTPHeaderField:@"Content-Type"]; 
[request addValue:[NSString stringWithFormat:@"%@",[user getToken]] forHTTPHeaderField:@"GST"]; 
[request setHTTPMethod:@"POST"]; 
[request setHTTPBody:postBody]; 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

    NSLog(@"JSON: %@", responseObject); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

    NSLog(@"Error: %@", operation.responseString); 
}]; 
[operation start]; 
+0

我注意到要添加兩個引用「GST」,一個用於請求頭「曲奇」和包含用於請求頭「GST」用戶令牌的另一個。這是你的意圖嗎?你不在你的ASI版本中執行這兩個操作。 –

+0

我嘗試了很多東西,我只是複製了我從XCode獲得的最後一個請求。 Cookie頭應該可以完成這項工作,但它不會:/ –

+0

* 3參考。我錯過了作爲GET參數傳入的那個。也許你應該簡化問題。找到最不起作用的最簡單情況,然後試着弄清楚爲什麼不行。而不是僅僅添加「解決方案」和希望。 –

回答

0

好,最後我得到了它。這與混合AFNetworking和ASIHTTP有關。 AFNetworking請求本身很好。

似乎在使用ASIHTTP請求登錄到後端系統後,使用AFNetworking的請求無法正常工作。不知道爲什麼,但只使用AFNetworking請求解決了這個問題。

感謝大家=)

0

你有沒有試過這種

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
NSDictionary *parameters = @{@"foo": @"bar"}; 
[manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"JSON: %@", responseObject); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 
}]; 
相關問題