2012-05-04 5 views
1

일부 데이터를 가져 오기 위해 내 애플리케이션에서 HTTPS 요청을해야합니다. 나는 여기에 내 코드, 여전히 작동이 코드를 가지고 내 이전 응용 프로그램을 업그레이드하기 때문에이 작동하지 않습니다Cocoa/objective-C로 HTTPS 요청

-(void)Authenticate 
{ 
    self.responseData = [NSMutableData data]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://10.84.4.2:8444/fish-mobile/GetPIN.jsp?MSISDN=12345"]]; 
    //NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    [responseData setLength:0]; 

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

    - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:  (NSURLAuthenticationChallenge *)challenge { 

    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; 

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; 
} 

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [responseData appendData:data]; 
} 

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    self.responseData = nil; 
} 


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

     NSString *responseString = [[NSString alloc] initWithData:responseData  encoding:NSUTF8StringEncoding]; 


    NSLog(@"%@", responseString); 

    } 

이 방법을 사용하여 할 수있을하는 데 사용하지만, 나는 새로운 응용 프로그램과 포스 테이를 만들 경우 나는 코드 그것은있는 NSString에 대한 가시적 인 @interface이 SetLength를 또는 대한 appendData를 선언 없다고 말합니다

[responseData setLength:0]; 

[responseData appendData:data]; 

주위에 오류가 발생합니다.

누군가가 왜 이것이 작동하지 않는지 말할 수 있습니까?

답변

0

이 드 responseData 인스턴스 변수처럼 나에게 소리 미리

덕분에 어떻게 든있는 NSString에 typecasted되고있다. responseData ivar는 어떻게 정의되었고 방금 게시 한 위임자를 제외하고 다른 작업을 수행하고 있습니까?

+0

가 관리 잘 작동됩니다 : D – AdamM

2

-(void) testme: (NSString *) link{ 
NSURL * url = [NSURL URLWithString:link]; 
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url]; 
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPMethod:@"GET"]; 
[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES]; 

}

에 누전 그리고 그 방법은

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

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 

    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; 

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 

    NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@", s); 

} 

가 정렬 얻을