0
我試圖從我的objective-c應用程序向我的服務器發送HTTP發佈請求。我嘗試了很多不同的東西,但我無法獲得請求主體上的內容請求。使用url上的參數它工作得很好,但我需要發送保留字符的字符串。帶有正文內容的HTTP發佈請求失敗
這是我的課負責請求:
#import "CLPUtilRequest.h"
static NSString *baseURL = @"http://localhost:8080/cl-mobile";
@implementation CLPUtilRequest
//Parameteres on the URL (working!!)
+ (NSData *) makeHttpRequest:(NSString *)url {
//Set info for webservice
NSString *urlString = baseURL;
urlString = [urlString stringByAppendingString:url];
NSURL *resquestUrl = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:resquestUrl];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/http" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//Call the webservice
NSError *errorReturned = nil;
NSHTTPURLResponse *theResponse =[[NSHTTPURLResponse alloc]init];
return [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
}
//Parameteres on the bodt (not working!!)
+ (NSData *) makeHttpRequestContentOnBody:(NSString *)url withContent:(NSString *)content {
NSData *postData = [content dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *errorReturned = nil;
NSHTTPURLResponse *theResponse =[[NSHTTPURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
return data;
}
@end
而這正是我所說的請求。
NSString *contentString = @"chaveUsuarioEmpresa=";
contentString = [contentString stringByAppendingString:appDelegate.clinica.chave];
contentString = [contentString stringByAppendingString:@"&chaveAgendamento="];
contentString = [contentString stringByAppendingString:appDelegate.agendamento.chave];
contentString = [contentString stringByAppendingString:@"&anamnese="];
contentString = [contentString stringByAppendingString:self.texto.text];
NSData *dataResponse = [CLPUtilRequest makeHttpRequestContentOnBody:@"/agendamento/anamnese/save" withContent:contentString];
調用請求後,我errorReturned給我下面的(調試):NSURLError *域名:@ 「NSURLErrorDomain」 - 代碼:-1002
我試圖做的是在這裏的鏈接中(但沒有做到):Sending an HTTP POST request on iOS
在此先感謝!
非常感謝,真的是我的一個愚蠢的錯誤:( 現在它的工作很好! – phellipeperin 2014-09-29 19:25:47