2012-05-04 3 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のに型変換されています。 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); 

} 

それはソート得ることができた細かい