1

我需要从我的应用程序发出 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);

    }

但是,自从我升级了代码后,这不起作用,具有此代码的旧应用程序仍然可以工作,但是如果我创建一个新应用程序并将此代码发布到其中,则会出现错误

  [responseData setLength:0];

    [responseData appendData:data];

它说 NSString 没有可见的@interface 声明 setLength 或 appenddata。

有人可以告诉我为什么这不起作用吗?

提前致谢

4

2 回答 2

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

}

工作正常

于 2012-05-04T15:04:46.220 回答
0

在我看来,您的 responseData 实例变量以某种方式被强制转换为 NSString。responseData ivar 是如何定义的,除了您刚刚发布的委托之外,您是否对其进行了任何其他操作?

于 2012-05-04T14:22:00.533 回答