POST方法在ios中不起作用

时间:2016-03-09 06:49:04

标签: ios objective-c http-post

我已尝试使用NSURLSession,AFNetworking,NSURLConnection在post方法中发送数据的所有方法;但每次我的后端开发人员说他们没有收到任何数据,虽然我的co android开发人员在POST中成功发送数据。我是现在完全无助。请帮忙。

----使用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);
}];

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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];