Currently, the code below is what I have to make a synchronous HTTP connection with a server. The server basically displays the required UI dialog depending upon the query. Now, the server is changed to a secure server which requires basic authentication (username & password). Could you please help me with an example to change it to a HTTPS request with basic auth?
NSString *url = localhosturl;
NSString *path=[items[0] path];
path = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLUserAllowedCharacterSet]];
NSArray *myStrings = [[NSArray alloc] initWithObjects:url, syncportnum, query, path, nil];
NSString *targetUrl = [myStrings componentsJoinedByString:@""];
NSURL *url_1 = [NSURL URLWithString:targetUrl];
NSURLRequest *request1 = [NSURLRequest requestWithURL:url_1];
NSURLResponse *response;
NSError *error;
//send it synchronous
NSData *responseData = [NSURLConnection sendSynchronousRequest:request1 returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
// check for an error. If there is a network error, you should handle it here.
if(!error)
{
//log response
NSLog(@"Response from server = %@", responseString);
}
Thank you!
That would typically mean changing the URL to
https://username:password@hostname/...... with a long list of caveats, such as the fact that the username and password are going to be unavoidably in the cache, and a strong recommendation to rewrite this to do the same work asynchronously using a supported API. :-)