gpt4 book ai didi

.net - 从 html POST 检索错误响应

转载 作者:行者123 更新时间:2023-11-29 04:16:34 24 4
gpt4 key购买 nike

我使用此代码将 JSON 发送到 .NET 服务以注册用户:

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 作为响应字符串:

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

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

编辑--我忘了提及,实际的 errorResponse 为零,我假设因为返回了 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,执行此操作非常简单,只需添加库(在此处找到它: http://afnetworking.com ),然后执行以下操作:

//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];

这是使用异步连接(您应该!),然后您可以在成功 block 中调用下一步。

我没有对此进行测试,但它应该可以(或非常接近)满足您需要做的事情。

更新也许您可以使用此代码(我从 HERE 获取)来查看它们在 header 中提供的数据类型(如果有),并查看它是否有用。

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

关于.net - 从 html POST 检索错误响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13616018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com