Reputation: 2446
After referring some questions in Stack Overflow I used below method to get response and response code as well.
NSMutableURLRequest *NSRequest;
NSRequest = [[NSMutableURLRequest alloc] init];
[NSRequest setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@://%@:%@/auth",[[NSUserDefaults standardUserDefaults] stringForKey:@"mongoScheme"],[[NSUserDefaults standardUserDefaults] stringForKey:@"mongoHost"],[[NSUserDefaults standardUserDefaults] stringForKey:@"mongoPort"]]]];
[NSRequest setHTTPMethod:@"GET"];
[NSRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[NSRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSHTTPURLResponse *response = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:NSRequest returningResponse:&response error:nil];
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
int code = [httpResponse statusCode];
NSLog(@"%i",code);
NSLog(@"%@",response);
But I'm not able to get any one of them.
I check with logs and found response
as null
and statusCode
as 0
how can I get this?
Upvotes: 0
Views: 124
Reputation: 32
The options for performing HTTP requests in Objective-C can be a little intimidating. One solution that has worked well for me is to use NSMutableURLRequest
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:url]];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *responseCode = nil;
NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
if([responseCode statusCode] != 200){
NSLog(@"Error getting %@, HTTP status code %i", url, [responseCode statusCode]);
return nil;
}
return [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];
Upvotes: 0
Reputation: 805
Warning:'sendSynchronousRequest(_:returningResponse:)' was deprecated in iOS 9.0 So Please use this:
For Objective-C
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:"Your_URL"]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle response
}] resume];
For Swift,
var request = NSMutableURLRequest(URL: NSURL(string: "YOUR URL"))
var session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
var params = ["username":"username", "password":"password"] as Dictionary<String, String>
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
println("Response: \(response)")})
task.resume()
Upvotes: 1
Reputation: 4815
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSString *string = BaseURLString1;
NSString* encodedUrl = [string stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:encodedUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:encodedUrl parameters:nil error:nil];
NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSArray *dictValue=[[NSArray alloc] init];
dictValue=[responseObject valueForKey:@"KEY"];
}];
[dataTask resume];
Upvotes: 0