2011-05-10 61 views
0

我想知道服務器是否應該有特殊的設置來使用nsurlconnections?當我嘗試創建連接和身份驗證。我沒有收到任何錯誤或警告,但沒有連接。nsurlconnection服務器需要特殊設置?

請問任何人請在此澄清我。

感謝

編輯:這是我的代碼目前使用

- (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"); 
    } 
} 
+0

您可能需要提供更多的信息和接受更多的答案,得到的答覆 – 2011-05-10 11:05:04

+0

你能告訴我你想這樣我可以告訴ü – pa12 2011-05-10 11:06:44

回答

0

實現NSURLConnection的委託方法:

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

這將記錄與連接字符串成功的控制檯。查看響應數據,您可能會發現有關其中的連接的一些信息。

+0

我沒有實現這個方法。我成功了。在didReceiveResponse :(NSURLResponse *)響應中,服務器始終使用我用來連接到服務器的相同url進行響應。 – pa12 2011-05-10 21:32:13

+0

那很好,在服務器上你有沒有實現任何東西來回應時,URL加載的地方? – Sandeep 2011-05-11 03:02:37

+0

我工作的網址是一個網頁。它在瀏覽器中正常工作,現在我試圖使用相同的URL並使用nsurlconnection。 – pa12 2011-05-13 13:21:51

1

不,你的服務器並不需要任何特殊的配置。 A NSURLConnection只使用簡單的HTTP請求。

對於身份驗證:您需要自己實現所需的委託方法進行身份驗證。此外,請檢查您是否通過SSL連接,併爲此執行所需的委託方法。

編輯:繼承自我的一個使用HTTP認證的應用程序。我發現你還需要在canAuthenticateAgainstProtectionSpace:方法中爲NSURLAuthenticationMethodHTTPBasic返回YES,否則它將無法工作。

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

我連接哪些細節使用https。服務器有證書的問題,所以我正在實施canAuthenticateAgainstProtectionSpace,還有didReceiveAuthenticationChallenge和其他相關方法。但我無法通過服務器進行身份驗證或顯示任何錯誤。你能告訴我可能是什麼問題嗎? – pa12 2011-05-10 21:29:24

+0

請張貼造成問題的代碼 – 2011-05-11 07:11:47

+0

我已添加下面的代碼。請看看它。 – pa12 2011-05-12 18:20:21

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"); 
} 
+0

請正確格式化您的代碼... – 2011-05-12 18:26:58

+0

我不知道如何格式化代碼在stackoverflow。我試過但我不能。對不起,麻煩你了。 – pa12 2011-05-13 13:17:39