내 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
그리고 내가 요청을 부르는 곳이다. 나는이 링크에 설명 된대로하려고 노력 -1002
(그러나 : 코드 - @ "NSURLErrorDomain"NSURLError * 도메인 :
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 나에게 다음과 같은 (디버깅) 제공 하지 못했습니다.) : Sending an HTTP POST request on iOS
미리 감사드립니다.
고마움 많은 남자, 정말 내 벙어리 실수했다 : ( 지금은 잘 작동! – phellipeperin