Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSURLSession POST不工作,即使返回200_Ios_Post_Nsurlsession - Fatal编程技术网

Ios NSURLSession POST不工作,即使返回200

Ios NSURLSession POST不工作,即使返回200,ios,post,nsurlsession,Ios,Post,Nsurlsession,我试图发布一个字符串并检索服务器的响应,但由于某些原因,它似乎不起作用,我在过去使用过一些类似的代码,它工作得很好。。。 这是我的代码,如果你发现了错误或者有什么建议,请告诉我 NSURL *url = [NSURL URLWithString:@"http://www.NAME.info/test/check.php"]; NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionCon

我试图发布一个字符串并检索服务器的响应,但由于某些原因,它似乎不起作用,我在过去使用过一些类似的代码,它工作得很好。。。 这是我的代码,如果你发现了错误或者有什么建议,请告诉我

NSURL *url = [NSURL URLWithString:@"http://www.NAME.info/test/check.php"];
                    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
                    sessionConfiguration.HTTPAdditionalHeaders = @{


                                      @"Content-Type"        : @"application/json"
                                                               };
                    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
                    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
                    request.HTTPMethod = @"POST";
                    NSError *error = nil;
                    NSString* str = @"username=test101";
                    NSData* jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];

                    if (!er

ror) {

                    NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
                                                                               fromData:jsonData completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {


                                                                                   NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                                                                   NSString* result2 = [responseString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
                                                                                   NSString *secondString = [result2 stringByReplacingOccurrencesOfString:@" " withString:@""];
                                                                                   NSLog(@"*%@*",secondString);
                                                                                   NSLog(@"RESULT RETURNED%@",response.debugDescription);
                                                                                                                                                                                                                                                                                                       }];

                    [uploadTask resume];
                }
这样做的结果是:

2015-03-26 19:35:03.305 ChannerApp[690:61802] *error_post*
2015-03-26 19:35:03.305 ChannerApp[690:61802] RESULT RETURNED<NSHTTPURLResponse: 0x17023a3a0> { URL: http://www.NAME.info/test/check.php } { status code: 200, headers {
    Connection = "Keep-Alive";
    "Content-Length" = 10;
    "Content-Type" = "text/html; charset=UTF-8";
    Date = "Thu, 26 Mar 2015 17:35:35 GMT";
    "Keep-Alive" = "timeout=5, max=100";
    Server = "Apache/2.4.7 (Ubuntu)";
    "X-Powered-By" = "PHP/5.5.9-1ubuntu4.6";
} }

如果没有对服务器期望的内容进行描述,则无法回答此问题。服务器日志怎么说?我看到“内容长度”是10。它给了你什么内容?尝试打印原始NSData,然后使用十六进制到文本转换器查看收到的内容。