繁体   English   中英

Objective-C HTTP POST请求

[英]Objective-C HTTP POST Request

我正在使用使用HTTP的SMS网关。 我使用有关stackoverflow等的教程和问题创建了一个请求,但是出了点问题。 当我手动输入URL时,我可以使用网关没有任何问题,但我不能以编程方式进行,所以我想我的代码可能不正确。 感谢任何帮助!

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]);
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];
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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM