0

Currently I have:

NSArray* array = [NSArray arrayWithObjects:@"auth.login",@"username",@"password", nil];
NSData* packed_array = [array messagePack];

NSURL* url = [NSURL URLWithString:@"https://192.168.1.149:3790/api/1.0"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];

//Previously the below had changed by hostname.
//[request setValue:@"RPC Server" forHTTPHeaderField:@"Host"];

[request setValue:@"binary/message-pack" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d",[packed_array length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:packed_array];

NSURLResponse *response;
NSError *error;
responseData = [NSMutableData dataWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]];

NSLog(@"response data: %@",[responseData messagePackParse]);
NSLog(@"error: %@",error);
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
NSLog(@"called canAuthenticateAgainstProtectionSpace");
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];  
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSLog(@"called didReceiveAuthenticationChallenge");
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];  
}

Which returns "Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid"…"

How should I be implementing the answer from this question and getting data back?

Community
  • 1
  • 1
erran
  • 1,300
  • 2
  • 15
  • 36
  • 1
    dup http://stackoverflow.com/questions/933331/how-to-use-nsurlconnection-to-connect-with-ssl-for-an-untrusted-cert – hamstergene Jun 20 '12 at 01:48
  • Did you neglect to read the question? :^\ I was unable to implement the code in that answer. – erran Jun 20 '12 at 02:31
  • You haven't posted the code that instantiates the NSURLConnection. Are you properly setting it's delegate to an object that implements those two methods? – Taum Jun 20 '12 at 03:36
  • I used `[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]` to receive the data. I never instantiated a NSURLConnection object. How would I go about doing so and setting the delegate to self? I tried creating a NSURLConnection object before but I couldn't figure out how to set the delegate. While on the other hand I couldn't figure out how to get an asynchronous connection (which is recommended correct?) instead of the synchronous method I've used to spit out NSData. – erran Jun 20 '12 at 03:55
  • How do I set the delegate for NSURLConnection? – erran Jun 20 '12 at 18:33

2 Answers2

0

I figured out the problem(s). First I fixed the way I was using the delegate and secondly I changed the host. I edited the code to remove the changing of the host and commented it. I still don't think my question was a duplicate…

Community
  • 1
  • 1
erran
  • 1,300
  • 2
  • 15
  • 36
0

If your application flow needs the syncronous request, i answer something similar here Objective-C SSL Synchronous Connection

Community
  • 1
  • 1
jgorozco
  • 593
  • 10
  • 11