2012-05-04 52 views
1

我需要從我的應用程序發出一個HTTPS請求來獲取一些數據。我曾經能在這裏使用這種方法HTTPS請求可可/ objective-C

-(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); 

    } 

但是自從我升級我的代碼,我有這個代碼仍然有效舊的應用程序,這是不工作做,但如果我創建一個新的應用程序和郵政這在我的代碼繞過

[responseData setLength:0]; 

[responseData appendData:data]; 

錯誤報告說,爲的NSString沒有明顯的@interface聲明setLength或appenddata。

有人能告訴我爲什麼這不起作用嗎?

在此先感謝

回答

0

的聲音,我喜歡你的responseData實例變量被莫名其妙類型強制轉換爲NSString的。 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); 

} 

做工精細