Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.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
.net 从html POST检索错误响应_.net_Ios_Xcode_Soap - Fatal编程技术网

.net 从html POST检索错误响应

.net 从html POST检索错误响应,.net,ios,xcode,soap,.net,Ios,Xcode,Soap,我使用以下代码向.NET服务发送JSON以注册用户: NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: deviceID, @"DeviceId", model, @"DeviceModel", user, @"Us

我使用以下代码向.NET服务发送JSON以注册用户:

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”}

现在,当我尝试注册一个我知道已经存在的用户名时,我会返回以下HTML作为响应字符串:

<?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>

请求错误
正文{color:#000000;背景色:白色;字体系列:Verdana;左边距:0px;右边距:0px;}内容{35;左边距:30px;字体大小:70em;底部填充:2em;}A:链接{color:#336699;字体重量:粗体;文本装饰:下划线;}A:已访问{color:#6699cc;字体重量:粗体;文本装饰:下划线;}A:活动{颜色:#336699;字体大小:粗体;文本装饰:下划线;}。标题1{背景颜色:#003366;边框底部:#336699 6px实心;颜色:#FFFFFFFF;字体系列:Tahoma;字体大小:26px;字体大小:正常;边距:0em 0em 10px-20px;填充底部:8px;填充左侧:30px;填充顶部:16px;}预处理{字体大小:小;背景色:#e5e5cc;填充:5px;字体系列:Courier New;页边距顶端:0px;边框:1px#f0f0e0实心;空白:预换行;空白:-预换行;字换行:中断字;}表格{边框折叠:折叠;边框间距:0px;字体系列:Verdana;}表格th{右边框:2px白色实心;边框底部:2px白色实心;字体大小:粗体;背景色:#cecf9c;}表格td{右边框:2px白色实心;边框底部:2px白色实心;背景色:#e5e5cc;}
请求错误

服务器在处理请求时遇到错误。有关详细信息,请参阅服务器日志

很明显,这并没有返回实际的错误,只是一个网页告诉我有错误。 是否缺少从服务中检索实际错误代码的方法? 服务是否可能以某种方式将错误作为响应发送

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

首先,要检查状态代码是什么,以便您可以像这样处理>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];
这是使用异步连接(您应该!),然后您可以调用success块中的下一步

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

更新 也许您可以使用这段代码(我从中获取)来查看他们在标题中提供了什么样的数据(如果有的话),并查看它是否有用

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

我想我们需要查看您的asp.net代码。您如何处理服务器上的错误?很遗憾,我没有编写服务器代码。因此,响应代码会触发错误。有关NSURERROR或服务器如何处理错误(返回的内容)的详细信息,请参阅此文档是您案例中的
requestError
。requestError为零,这让我觉得请求似乎成功了,因为返回了HTML。谢谢。这确实给了我可以使用的状态码400。我认为该服务可能不会发送任何关于失败原因的有用信息,但我认为它可能会打开如果用户名已经存在,请发送400。谢谢。幸运的是,您在这里受服务器的支配。检查我的更新,看看他们是否在标题中传递对您有用的更多信息。