8

I'm using an SMS gateway which uses HTTP. I created a request using tutorials and questions on stackoverflow etc. but there's something wrong. When I enter the URL manually I can use the gateway without any problems but I cannot do it programmatically so I thought probably my code is not right. Appreciate any help!

NSString *post = [NSString stringWithFormat:@"user=%@&password=%@&api_id=%@&to=%@&text=%@",_username,_password,_apiID,[_numbers componentsJoinedByString:@","],_txtMsg.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://api.mySmsGateWay.com/http/sendmsg"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
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", [responseCode statusCode]);
}

NSLog (@"%@", [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding]);
rmaddy
  • 314,917
  • 42
  • 532
  • 579
cameloper
  • 297
  • 1
  • 3
  • 16
  • Did you check the request header? To make sure you don't have to set a params related to authentication or maybe the application/json. – arthurfonseca Oct 03 '16 at 22:14
  • Just adding the parameters I wrote in (NSString post) to the URL is enough when using a browser. – cameloper Oct 03 '16 at 22:17
  • 2
    try NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];. And care about URL Encode http://stackoverflow.com/questions/8088473/how-do-i-url-encode-a-string – larva Oct 04 '16 at 00:12
  • It sadly didn't work. But i noticed a warning which tells that "sendSynchronousRequest" is deprecated. – cameloper Oct 04 '16 at 04:59
  • 1
    Cameloper check my below answer. – user3182143 Oct 04 '16 at 13:02

2 Answers2

19
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.mySmsGateWay.com/http/sendmsg"]];

NSString *userUpdate =[NSString stringWithFormat:@"user=%@&password=%@&api_id=%@&to=%@&text=%@",_username,_password,_apiID,[_numbers componentsJoinedByString:@","],_txtMsg.text, nil];

//create the Method "GET" or "POST"
[urlRequest setHTTPMethod:@"POST"];

//Convert the String to Data
NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding];

//Apply the data to the body
[urlRequest setHTTPBody:data1];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    if(httpResponse.statusCode == 200)
    {
        NSError *parseError = nil;
        NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
        NSLog(@"The response is - %@",responseDictionary);
        NSInteger success = [[responseDictionary objectForKey:@"success"] integerValue];
        if(success == 1)
        {
            NSLog(@"Login SUCCESS");
        }
        else
        {
            NSLog(@"Login FAILURE");
        }
    }
    else
    {
        NSLog(@"Error");     
    }
}];
[dataTask resume];
user3182143
  • 9,459
  • 3
  • 32
  • 39
4
NSError *error;
NSString *urlString = @"http://api.mySmsGateWay.com/http/sendmsg";
NSURL *url = [NSURL URLWithString:urlString];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

NSString * parameterString = [NSString stringWithFormat:@"user=%@&password=%@&api_id=%@&to=%@&text=%@",_username,_password,_apiID,[_numbers componentsJoinedByString:@","],_txtMsg.text];


NSLog(@"%@",parameterString);

[request setHTTPMethod:@"POST"];

[request setURL:url];

[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];


NSData *postData = [parameterString dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPBody:postData];

NSData *finalDataToDisplay = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

NSMutableDictionary *abc = [NSJSONSerialization JSONObjectWithData: finalDataToDisplay
                                                           options: NSJSONReadingMutableContainers

                                                            error: &error];
NSLog(@"%@",abc);
Siva Sankar
  • 543
  • 1
  • 4
  • 18