2016-10-03 160 views
1

我正在使用使用HTTP的SMS網關。我使用教程和問題在stackoverflow等創建了一個請求,但有什麼問題。當我手動輸入網址時,我可以毫無問題地使用網關,但是我無法通過編程來完成,因此我認爲我的代碼可能不正確。感謝任何幫助!Objective-C HTTP POST請求

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

你檢查請求頭?爲了確保您不必設置與身份驗證相關的參數或應用程序/ json。 – arthurfonseca

+0

使用瀏覽器時,只需將(NSString post)中寫入的參數添加到URL就足夠了。 – cameloper

+2

試試NSData * postData = [post dataUsingEncoding:NSUTF8StringEncoding] ;.並關心網址編碼http://stackoverflow.com/questions/8088473/how-do-i-url-encode-a-string – larva

回答

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