2014-07-11 19 views
1

我正在使用AFNetworking框架將一些數據發送到.Net後端。我正在使用POST:parameters:success:failure:方法。POST Http請求的正文部分爲空

在.NET後端使用FromBody屬性

[System.Web.Mvc.HttpPost] 
     public HttpResponseMessage Post([FromBody] string objCourseData, string securityToken, string appVer, string deviceDesc, string deviceOSDesc) 

讀取數據但它接收它爲空。我試過用PHP,我可以在$_POST對象中看到我的身體數據(雖然我看不到它在$HTTP_RAW_POST_DATA中)。可能是什麼問題呢?

我要發送的數據是JSON對象。

+0

哪些數據您嘗試後,是不是JSON? – RaffAl

+0

是的,它是JSON。 –

+0

你之前用過NSURLSession和NSURLConnection嗎?這很簡單。 – Claudio

回答

2

我不使用AFNetworking,但使用本地代碼很簡單:

NSString *urlString = @"http://www.urlpost.com"; 

//setting request 
NSURL *url = [NSURL URLWithString:urlString]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url 
                  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
                 timeoutInterval:5]; 
[request setHTTPMethod:@"POST"]; 

//setting post body with JSON data 
NSMutableDictionary *postDictionary = [NSMutableDictionary dictionary]; 
//set the data of your json 
[postDictionary setValue:@"some value" forKey:@"myKey"]; 
NSError *jsonError; 

//transform NSMutableDictionary in JSON data 
NSData *postData = [NSJSONSerialization dataWithJSONObject:postDictionary 
                options:0 
                error:&jsonError]; 

if (!jsonError) { 
    //set POST body 
    [request setHTTPBody:postData]; 
} 

//Here is the code for the post: 
[[[NSURLSession sharedSession] dataTaskWithRequest:request 
           completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 

    if (!error) { 
     //No errors, post done 
     NSLog(@"POST done!"); 
    } else { 
     //error handling here 
    } 
}] resume]; 
+0

我已經接受了這個,因爲它讓我清楚地訪問請求的細節,我相信類似的可以通過'AFNetworking'實現,但未能找到解析度。 –

+0

我還沒有使用AFNetworking,但設置請求後,你只需要做下載,因爲你一直在做 – Claudio