2016-03-09 34 views
-2

我已經嘗試了一切可能的方法來發送使用NSURLSession,AFNetworking,NSURLConnection的在崗方法的數據,但每次我的後端開發人員說,一次,他們沒有收到任何數據,儘管我的合作Android開發者被成功發送數據在POST.I現在完全無奈。請幫助。POST方法在IOS不工作

----使用NSURLConnection的----

NSString *post = [NSString stringWithFormat:@"user_id=%@&email=%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"UserId"],emailString]; 
NSLog(@"------%@-------",post); 
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding ];//[post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@app_user_service/app_invite_email",App_Domain_Url]]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

if(conn) { 
    NSLog(@"Connection Successful"); 
} else { 
    NSLog(@"Connection could not be made"); 
} 

// ----使用AFNetworking ----

NSDictionary *dic2 = @{@"user_id": [[NSUserDefaults standardUserDefaults] valueForKey:@"UserId"] , @"email" : emailString}; 

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 
manager.requestSerializer = [AFJSONRequestSerializer serializer]; 
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 

[manager POST:urlstring parameters:dic2 progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 
    NSLog(@"success!"); 



    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil]; 
    NSLog(@"Response----> %@",dict); 

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 
    NSLog(@"error: %@", error); 
}]; 
+0

東西可以在你的代碼需要錯誤發生,以看着你的代碼 – Harshavardhan

+0

檢查我在這裏回答:http://stackoverflow.com/questions/35570676/how-to-post-data-from-ios-app-to-mysql-database/35571006#35571006 –

+0

@SibaPrasadHota,你答案使用'NSURLConnection' wh ich已被棄用。我建議你不要推薦它。 –

回答

1

在iOS9中,Apple增加了名爲App Transport Security(ATS)的新功能。

ATS強制執行期間,網絡電話的最佳實踐,包括使用HTTPS的。

添加在你的Info.plist

enter image description here

你可以在你的Info.plist添加例外特定域的以下:

<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSExceptionDomains</key> 
    <dict> 
     <key>testdomain.com</key> 
     <dict> 
      <key>NSIncludesSubdomains</key> 
      <true/> 
      <key>NSExceptionAllowsInsecureHTTPLoads</key> 
      <true/> 
      <key>NSExceptionRequiresForwardSecrecy</key> 
      <true/> 
      <key>NSExceptionMinimumTLSVersion</key> 
      <string>TLSv1.2</string> 
      <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key> 
      <false/> 
      <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> 
      <true/> 
      <key>NSThirdPartyExceptionMinimumTLSVersion</key> 
      <string>TLSv1.2</string> 
      <key>NSRequiresCertificateTransparency</key> 
      <false/> 
     </dict> 
    </dict> 
</dict> 
+0

在你的plist中添加一次並嘗試一次,如果你沒有得到答案,我們將繼續進行步驟 –

+0

我已經添加了那個兄弟/先生,因爲我可以在GET方法中完成,問題僅在於POST方法: - (儘管我申請POST的方法已經在所有其他項目中測試過,並且已經成功運行。 :/ –

+0

@KoustovBasu - 你可以用請求URL和參數顯示你的POST方法, –

0

請檢查您是否像下面

設置屬性
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:[NSURL URLWithString:url]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 
+0

是的,我已經這樣申請....我每次都從後端是當它們不從我的端獲取數據他們發送的響應 [請求的setValue:@「應用程序/ x WWW的形式進行了urlencoded」 forHTTPHeaderField:@「內容類型」]; –

+0

我已經申請@」。 Content-Type「屬性與你所提到的相同@Kiran –

+0

向Android開發者詢問他設置的值是什麼? –