title: login view using NSURLSession post request
body: <p>I am trying to have a login view for an ios app,
  this is the page I am trying to access <a href="http://demo.redmine.org/login" rel="nofollow">http://demo.redmine.org/login</a>
  I tried to use NSURLConnection but it is not available with ios 9.
  Let me know If there is something wrong with my code here.</p>
  
  <pre><code>- (IBAction)login:(id)sender {
      NSString *username = @"admin";
      NSString *password = @"admin";
      NSString *post = [NSString stringWithFormat:@"%@%@",username, password];
      NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
      NSString *postLength = [NSString stringWithFormat:@"%lu" , (unsigned long)[postData length]];
      NSURL *url = [NSURL URLWithString:@"http://172.16.231.19/redmine23/login"];
      NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
      [request setURL:url];
      [request setHTTPMethod:@"POST"];
      [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
      [request setValue:@"application/JSON" forHTTPHeaderField:@"Content-Type"];
      [request setHTTPBody:postData];
  
      NSURLSession *session = [NSURLSession sharedSession];
  
      NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                              completionHandler:
                                    ^(NSData *data, NSURLResponse *response, NSError *error) {
  
                                        if (error) {
                                            // Handle error...
                                            return;
                                        }
  
                                        if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
                                            NSLog(@"Response HTTP Status code: %ld\n", (long)[(NSHTTPURLResponse *)response statusCode]);
                                            NSLog(@"Response HTTP Headers:\n%@\n", [(NSHTTPURLResponse *)response allHeaderFields]);
                                        }
  
                                        NSString* body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                        NSLog(@"Response Body:\n%@\n", body);
                                    }];
      [task resume];
  </code></pre>
  
  <p>This is what I got :</p>
  
  <pre><code>    2016-02-09 04:49:19.336 testLoginApp[9574:1247112] Response HTTP Headers:
  {
      "Content-Length" = 1208;
      "Content-Type" = "text/html";
      Date = "Tue, 09 Feb 2016 09:48:44 GMT";
      Server = "Microsoft-IIS/8.5";
      "X-Powered-By" = "ASP.NET";
      "X-Request-Id" = c24707e081ab5a8fbd5d3e0ff701b854;
      "X-Runtime" = "0.126006";
  }
  2016-02-09 04:49:23.312 testLoginApp[9574:1247112] Response Body:
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/&gt;
  &lt;title&gt;500 - Internal server error.&lt;/title&gt;
  &lt;style type="text/css"&gt;
  &lt;!--
  body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
  fieldset{padding:0 15px 10px 15px;} 
  h1{font-size:2.4em;margin:0;color:#FFF;}
  h2{font-size:1.7em;margin:0;color:#CC0000;} 
  h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
  #header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
  background-color:#555555;}
  #content{margin:0 0 0 2%;position:relative;}
  .content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
  --&gt;
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="header"&gt;&lt;h1&gt;Server Error&lt;/h1&gt;&lt;/div&gt;
  &lt;div id="content"&gt;
   &lt;div class="content-container"&gt;&lt;fieldset&gt;
    &lt;h2&gt;500 - Internal server error.&lt;/h2&gt;
    &lt;h3&gt;There is a problem with the resource you are looking for, and it cannot be displayed.&lt;/h3&gt;
   &lt;/fieldset&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>