简体   繁体   English

iOS Rest Web服务返回无效的主机错误

[英]iOS Rest Webservice Returning Invalid Host Error

We have an application which requires a rest webservice to be called (for the login screen). 我们有一个需要调用Rest Web服务的应用程序(用于登录屏幕)。 While calling this webservice, we are able to receive the result occasionally, but many times, we're getting null data with the following message: 在调用此Web服务时,我们偶尔会收到结果,但是很多时候,我们会通过以下消息获取空数据:

<HTML><HEAD><TITLE>Invalid Host Specified</TITLE></HEAD>
<BODY>The request did not specify a valid virtual host.</BODY></HTML>

After we get this error, we are not able to receive any other result and the app gets stuck in the login screen. 收到此错误后,我们将无法再收到其他任何结果,并且该应用会卡在登录屏幕中。 We have to close the app and open it and try again to see if the webservice works. 我们必须关闭该应用程序并打开它,然后重试以查看该Web服务是否正常运行。

What could be reason behind this error? 错误背后的原因可能是什么? We tried both NSURLConnection and NSURLSession to receive the result but both are having the same issue. 我们尝试了NSURLConnectionNSURLSession来接收结果,但是两者都存在相同的问题。 It would be really helpful if someone gives a solution for this or at least point me in the right direction in solving the issue. 如果有人为此提供解决方案,或者至少为我指出解决问题的正确方向,那将非常有帮助。

This is the function which I'm using to call the web service: 这是我用来调用Web服务的函数:

-(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];
}

In all likelihood, all of the following are true: 完全符合以下所有条件:

  • You're contacting an Oracle web server. 您正在联系Oracle Web服务器。
  • Multiple domain names point to that server's IP address. 多个域名指向该服务器的IP地址。
  • One of them isn't listed in the web server's config file. Web服务器的配置文件中未列出其中之一。
  • That's the domain that your app is trying to access. 这就是您的应用尝试访问的域。

The only other possibilities I can think of are that either you're trying to access the host by its IP address or you're making HTTP/1.0 requests (which lack a Host header). 我能想到的唯一其他可能性是,您正在尝试通过其IP地址访问主机,或者正在发出HTTP / 1.0请求(缺少Host标头)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM