nsurlconnection服务器需要特殊设置吗?

时间:2011-05-10 10:51:54

标签: iphone ios nsurl

我想知道服务器是否应该使用特殊设置来使用nsurlconnections?当我尝试创建连接和身份验证时。我没有收到任何错误或警告,但没有连接。

请有人在此澄清我。

由于

编辑:这是我目前使用的代码

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"loginURl"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
    [webView1 scalesPageToFit]; 
    if (theConnection) { 
        receivedData = [[NSMutableData data] retain]; 
    } else { 
        NSLog(@"Connection Failed"); 
    } 
}

3 个答案:

答案 0 :(得分:1)

不,您的服务器不需要任何特殊配置。 NSURLConnection只使用简单的HTTP请求。

对于身份验证:您需要自己实现所需的委托方法以进行身份​​验证。另外,检查您是否通过SSL连接,并为此实现所需的委托方法。

编辑:来自我的一个使用HTTP身份验证的应用中的一部分。我发现你还需要在canAuthenticateAgainstProtectionSpace:方法中为NSURLAuthenticationMethodHTTPBasic返回YES,否则它将不起作用。

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust] || [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic];
}

答案 1 :(得分:0)

实施NSURLConnection委托方法:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
           NSLog("Connection successful");
   }

这将使用连接成功字符串登录到控制台。查看响应数据,您可以在其中找到有关连接的一些信息。

答案 2 :(得分:0)

这是项目的代码

- (void)viewDidLoad {
    [super viewDidLoad];
    parserObject = [NSXML_Parser alloc];// parser initialization
    composerObject = [URLComposer alloc];// composer creation
    [composerObject init_URLComposer];//composer initialization

    [self.view addSubview:logIn_View];
    [webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"loginURl"]]];
    NSMutableData * receivedData = [NSMutableData alloc];
    NSLog(@"Starting the url conn");
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"loginURl"]                   
                                                cachePolicy:NSURLRequestUseProtocolCachePolicy                             
                                            timeoutInterval:60.0];
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    [webView1 scalesPageToFit];
    if (theConnection) {

        receivedData = [[NSMutableData data] retain];

    } else {
        NSLog(@"Connection Failed");
        // Inform the user that the connection failed.
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSURLRequest *urlreq =  [webView request];
    NSURL *req = [urlreq URL];
    NSURL *connectingUrl = [NSURL URLWithString: @"loginURl"];
    Boolean error1 = [req isEqual:errorUrl];// use string size to identify the error in url
    Boolean error2 = [req isEqual:connectingUrl];
    if (error1) {
        //[self.view addSubview:displayObj];
    }
    else if(error2) {

    }
    else {
        NSString *session_ID = [str_Url substringFromIndex:strLen];
        XMLDataFromServer = [NSData dataWithContentsOfURL:[NSURL  URLWithString:testing_Url_String]]; 
        [self logIn_Button_Click];
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {

}


- (void)dealloc {
    [super dealloc];
}

#pragma mark -
#pragma mark NSURLConnection Methods
#pragma mark -

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    NSLog(@"canAuthenticateAgainstProtectionSpace");
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if([challenge previousFailureCount] == 0){
        NSLog(@"Auth tried");
        NSURLCredential *credentials = [NSURLCredential credentialWithUser:@"userName" password:@"password" persistence:NSURLCredentialPersistenceNone];

        [[challenge sender] useCredential:credentials forAuthenticationChallenge:challenge];
    }
    else {
        NSLog(@"Auth failed");
    }    
}

- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"didReceiveResponse: %@\n",[[response URL] absoluteString]);
}
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data
{
    //NSLog(@"Did receive data: %@",data);
}

- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error
{

    NSLog(@"Error is:%@",error);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)conn
{

    NSLog(@"connectionDidFinishLoading");
}