4

I am trying for an HTTP call on https. Here is my code snippet.

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                initWithURL:[NSURL
                                             URLWithString:@"https://testservice.fiamm.com/token"]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];

NSString *postString = @"username=TestIphone&Password=T3st1ph$n&Grant_type=password";

[request setValue:[NSString stringWithFormat:@"%d", [postString length]] forHTTPHeaderField:@"Content-length"];

[request setHTTPBody:[postString
                      dataUsingEncoding:NSUTF8StringEncoding]];

// Fetch the JSON response
NSData *urlData;
NSURLResponse *response;
NSError *error;

// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:request
                                returningResponse:&response
                                            error:&error];

// Construct a String around the Data from the response
NSString *strFiamm = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];

When I try in hurl or postman I get response proper but when I try in my code I get this error.

NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)

Any help or suggestions appreciated.!

Ruchir Shah
  • 898
  • 3
  • 13
  • 31
  • 1
    possible duplicate of [NSURLSession/NSURLConnection HTTP load failed](http://stackoverflow.com/questions/30739473/nsurlsession-nsurlconnection-http-load-failed) – Tariq Jun 15 '15 at 11:17

3 Answers3

0

The link that you mentioned here is not https certificate in it so use simple http not https

Edit #1

I tried these values

http://testservice.fiamm.com/token
username=TestIphone
Password=T3st1ph$n
Grant_type=password

and output is

403 - Forbidden: Access is denied.
You do not have permission to view this directory or page using the credentials that you supplied.

that means your username password or key values are not correct

Varun Naharia
  • 5,318
  • 10
  • 50
  • 84
0

You didn't correctly setup the Content-Length header. The correct length of the content is the postString encoded in UTF-8 in bytes. [postString length] returns the number of UTF-16 code points (which is neither always the number of bytes in UTF-8 nor always the number of characters in the string).

You should also ensure that the post string is properly URL-encoded for use with application/x-www-form-urlencoded. You'll find a number of posts regarding this on SO. The definitive guide, though, is this: URL-encoded form data. You should especially take note of the warning emphasised in the first paragraph about application/x-www-form-urlencoded form data:

"This form data set encoding is in many ways an aberrant monstrosity, the result of many years of implementation accidents and compromises leading to a set of requirements necessary for interoperability, but in no way representing good design practices. In particular, readers are cautioned to pay close attention to the twisted details involving repeated (and in some cases nested) conversions between character encodings and byte sequences."

Having said this, I would strongly recommend to use another Content-Type to pass data to a server, for example JSON.

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
0

add this methode in your class

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

    if ([challenge.protectionSpace.authenticationMethod isEqualToString: NSURLAuthenticationMethodServerTrust])
    {

        NSURL* baseURL = [NSURL URLWithString:@"yourURL”];

        if ([challenge.protectionSpace.host isEqualToString:baseURL.host])
        {
            SecTrustRef trust = challenge.protectionSpace.serverTrust;

            NSURLCredential *cred = [NSURLCredential credentialForTrust:trust];

            [challenge.sender useCredential:cred forAuthenticationChallenge:challenge];

        }
        else
        NSLog(@"Not trusting connection to host %@", challenge.protectionSpace.host);

    }
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86
Mohamad Chami
  • 1,226
  • 14
  • 10