2012-11-28 78 views
0

我使用这个代码的JSON发送到.NET服务注册用户:检索错误响应POST

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
           deviceID, @"DeviceId", 
           model, @"DeviceModel", 
           user, @"Username", 
           pass, @"Password", 
           email, @"email", 
           nil]; 

    NSDictionary *consumer = [NSDictionary dictionaryWithObject:dictionary forKey:@"consumer"]; 
    NSLog(@"%@", consumer); 

    NSError *error = nil; 
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:consumer 
                 options:kNilOptions 
                 error:&error]; 
    NSLog(@"%@", error); 

    NSString *urlString = @"http://aservice.co.uk/services/service.svc/RegisterConsumer"; 

    NSURL *url = [NSURL URLWithString:urlString]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 

    [request setHTTPBody:jsonData]; 
    NSURLResponse *response = NULL; 
    NSError *requestError = NULL; 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError]; 
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ; 
    NSLog(@"%@", responseString); 
    NSLog(@"%@", requestError); 

我领回{"RegisterConsumerResult":"True"}

一个JSON结果也能正常工作

现在,当我尝试注册一个用户名,我知道已经存在,我得到以下HTML作为responseString:

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> 
    <title>Request Error</title> 
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style> </head> <body> 
    <div id="content"> 
     <p class="heading1">Request Error</p> 
     <p>The server encountered an error processing the request. See server logs for more details.</p> 
    </div> </body> </html> 

显然,这不是返回实际ERR或者,只是一个网页告诉我有一个错误。 错过了从服务中检索实际错误代码的方法吗? 服务是否有可能以某种方式作为响应发送错误?

编辑 - 我忘了提及,实际的errorResponse是零,我假设因为HTML被返回,实际的请求被归类为成功。

+0

我想我们需要看你的asp.net代码。你如何处理你的服务器上的错误? – mkral

+0

不幸的是我没有编写服务器代码。 – Darren

+0

所以错误是由响应代码触发的。有关NSURLError的更多信息,请参阅此文档https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html#//apple_ref/doc/constant_group/URL_Loading_System_Error_Codes – mkral

回答

1

首先,以检查其状态代码是什么,所以你可以处理> 400错误,像这样:

NSURLResponse *response = NULL; 
NSError *requestError = NULL; 
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError]; 
NSUInteger statusCode = [(NSHTTPURLResponse*)response statusCode]; 
//Then do your error checking. 

如果你想使用AFNetworking它可以很简单的做到这一点,只需添加库(在这里找到它:​​),那么像这样做:

//NOTHING CHANGED TIL NEXT COMMENT 
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
          deviceID, @"DeviceId", 
          model, @"DeviceModel", 
          user, @"Username", 
          pass, @"Password", 
          email, @"email", 
          nil]; 

NSDictionary *consumer = [NSDictionary dictionaryWithObject:dictionary forKey:@"consumer"]; 
NSLog(@"%@", consumer); 

NSError *error = nil; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:consumer 
                options:kNilOptions 
                error:&error]; 
NSLog(@"%@", error); 
//Start to use AFNetworking 

    NSURL *baseUrl = [NSURL URLWithString:@"http://aservice.co.uk/services/service.svc/"]; 
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:baseUrl]; 

NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"RegisterConsumer" parameters:dictionary]; 
[request setHTTPBody:jsonData]; 

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
    NSLog(@"SUCESS"); 
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
    NSLog(@"FAILED WITH STATUS CODE %d - error description: %@", response.statusCode, error); 
}]; 

[operation start]; 

这是使用异步连接(你应该!),那么你可以调用成功阻止你的下一个步骤。

我没有测试这个,但它应该工作(或非常接近),你需要做什么。

更新 也许你可以使用此代码(我从HERE抓起),看看有什么数据的种类 - 他们都在头提供,看看它是否是有用的 - 如果有的话。

if ([response respondsToSelector:@selector(allHeaderFields)]) { 
    NSDictionary *dictionary = [response allHeaderFields]; 
    NSLog([dictionary description]); 
} 
+0

谢谢。这确实给了我可以使用的statusCode 400。我认为该服务可能实际上没有发送任何有用的信息,为什么它失败了,但我认为如果用户名已经存在,它可能只发送一个400。谢谢 – Darren

+1

不幸的是,你在这里服务器的摆布。查看我的更新,看看他们是否在头文件中传递了可能对您有用的信息。 – mkral