【问题标题】:Objective-C HTTP POST RequestObjective-C HTTP POST 请求
【发布时间】:2017-02-11 22:55:37
【问题描述】:

我正在使用一个使用 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]);

【问题讨论】:

  • 你检查请求头了吗?确保您不必设置与身份验证或应用程序/json 相关的参数。
  • 使用浏览器时,只需将我在(NSString post)中写的参数添加到URL就足够了。
  • 试试 NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];。并关心 URL Encode stackoverflow.com/questions/8088473/…
  • 很遗憾没有用。但我注意到一个警告,它告诉我们“sendSynchronousRequest”已被弃用。
  • Cameloper 检查我的以下答案。

标签: ios objective-c http post


【解决方案1】:
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];

【讨论】:

    【解决方案2】:
    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-25
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多