响应不是来自使用NSurlSession的服务器

时间:2016-03-03 09:32:11

标签: ios objective-c nsurlsession asiformdatarequest

嗨,我是ios的新手,在我的应用程序中,我早前使用ASIFormDataRequest来整合服务

现在我改变了格式,我使用的是NSURLSession而不是ASIFormDataRequest

但是当我使用ASIFormDataRequest请求将密码更改为服务器时,成功响应来自服务器但是当我使用NSURLSession来自服务器的响应失败时请帮助解决错误

NSURlsession: -

def = [NSUserDefaults standardUserDefaults];
 NSString *myString = [def stringForKey:@"AccesToken"];
 NSString *AccessToken = [NSString stringWithFormat:@"Bearer %@",myString];

 NSString *Finalstr = [NSString stringWithFormat: @"MedicaidId=%@&OldPassword=%@&NewPassword=%@&ConfirmPassword%@", medicaId,self.CurPwdTxt.text,self.NewPwdTxt.text,self.ConfPwdTxt.text];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:mainurl,BaseURL]]

                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy

                                                       timeoutInterval:60.0];

    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[Finalstr dataUsingEncoding:NSUTF8StringEncoding]];
    [request addValue:AccessToken forHTTPHeaderField:@"Authorization"];

    NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {


        if (error) {

            NSLog(@"dataTaskWithRequest error: %@", error);
        }

        else if ([response isKindOfClass:[NSHTTPURLResponse class]]) {

            NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];

            if (statusCode != 200) {

                NSError *parseError;
                id responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];

                 NSLog(@"responseobject is %@",responseObject);

            }else{

                NSError *parseError;

                id responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];

                NSLog(@"else condtion");

                if (!responseObject) {

                    NSLog(@"JSON parse error: %@", parseError);

                    NSLog(@"responseobject is%@",responseObject);

                } else {


                    NSLog(@"responseobject is %@",responseObject);

                }

                //if response was text/html, you might convert it to a string like so:

                NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                NSLog(@"final responseString = %@", responseString);
            }
        }
    }];

    [task resume];
}

ASIFormDataRequest: -

NSString *urlStr = [NSString stringWithFormat:@"myurl",BaseURL];
    NSLog(@"urlStr --->>> %@",urlStr);

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [request setRequestMethod:@"POST"];

    def = [NSUserDefaults standardUserDefaults];
    NSString *myString = [def stringForKey:@"AccesToken"];

    NSString *str = [NSString stringWithFormat:@"Bearer %@",myString];
    NSLog(@"token is %@",str);

    [request setPostValue:medicaId forKey:@"MedicaidId"];
    [request setPostValue:self.CurPwdTxt.text forKey:@"OldPassword"];
    [request setPostValue:self.NewPwdTxt.text forKey:@"NewPassword"];
    [request setPostValue:self.ConfPwdTxt.text forKey:@"ConfirmPassword"];
    [request addRequestHeader:@"Authorization" value:str];

    [request setDelegate:self];
    [request startAsynchronous];

1 个答案:

答案 0 :(得分:0)

您正在那里发送不同的请求。在新的'代码,您正在构建POST数据,如下所示:

@"MedicaidId=%@&OldPassword=%@&NewPassword=%@&ConfirmPassword%@"

看起来像GET参数而不是表单编码(您在'旧'请求中使用)。

使用something like Wireshark跟踪您的请求并查看实际请求可能有助于您下次自己发现问题。