我们有一个应用程序需要调用rest webservice(用于登录屏幕)。在调用此Web服务时,我们偶尔会收到结果,但很多时候,我们通过以下消息获取空数据:
<HTML><HEAD><TITLE>Invalid Host Specified</TITLE></HEAD>
<BODY>The request did not specify a valid virtual host.</BODY></HTML>
我们收到此错误后,我们无法收到任何其他结果,并且应用程序卡在登录屏幕中。我们必须关闭应用程序并打开它,然后再次尝试查看web服务是否有效。
这个错误背后的原因是什么?我们尝试了NSURLConnection
和NSURLSession
来接收结果,但两者都有相同的问题。如果有人为此提供解决方案或者至少指出我正确的方向来解决问题,那将会非常有用。
这是我用来调用网络服务的功能:
-(void)startWebServiceForURL:(NSString *)URL withInputJson:(NSString *)inputJson forView:(UIView *)parentView completionBlock:(DownloadCompletionBlock)completionBlock errorHandlingBlock:(DownloadErrorBlock)ErrorBlock
{
if (_showIndicator)
{
UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
UIViewController *viewC = [window rootViewController];
_progressIndicator = [MBProgressHUD showHUDAddedTo:viewC.view animated:YES];
}
_completion = completionBlock;
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[inputJson length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[inputJson dataUsingEncoding:NSUTF8StringEncoding]];
[request setTimeoutInterval:60.0f];
NSURLSession * session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(),
^{
if (_showIndicator)
{
[_progressIndicator hideAnimated:YES];
}
if (!error && data != nil)
{
NSLog(@"result received!!!");
NSError * error1;
{
NSLog(@"print something else");
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"new string: %@", newStr);
NSDictionary * jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error1];
NSLog(@"received data: %@", jsonDictionary);
if (!error1)
_completion(YES,jsonDictionary);
else
{
_error = ErrorBlock;
_error(error1);
}
}
}
else
{
NSLog(@"error receiving result");
_error = ErrorBlock;
_error(error);
if ( error.domain == NSURLErrorDomain && error.code == NSURLErrorNotConnectedToInternet)
{
NSLog(@"not connected");
}
}
});
}] resume];
}
答案 0 :(得分:0)
很有可能,以下所有情况都属实:
我能想到的唯一其他可能性是您尝试通过其IP地址访问主机,或者您正在发出HTTP / 1.0请求(缺少主机标头)。