I’m new with Objective-C, i’m working in an application and I need to connect to a Java’s servlet using HTTP Post.
I searched over internet and found a lot of information related with NSMutableURLRequest an NSURLConnection.
I made some test and now I can send and receiveStrings but I need to make some changes and I don´t know how can I do.
I want to send a message to my Servlet using a String (done) and I want to Stop the excecution until receive a response from the Servlet (This is my problem). I can´t stop the excecution, i don´t know how to do that.
This is my code:
- (NSString *)sendPostRequest: (NSString*)stringToSend {
//URL en formato String
NSString *urlString = @"http://192.168.1.1:8080/Servlet/ListenerServlet";
//Armo el Body con los Datos a enviar
NSMutableString* requestBody = [[NSMutableString alloc] init];
[requestBody appendString:stringToSend];
//Creo el Request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
//Creo el Objecto con la URL dentro
NSURL *url = [NSURL URLWithString: [NSString stringWithString:urlString]];
//Seteo la URL al Request
[request setURL:url];
//Armo los Datos a Enviar
NSString* requestBodyString = [NSString stringWithString:requestBody];
NSData *requestData = [NSData dataWithBytes: [requestBodyString UTF8String] length: [requestBodyString length]];
//Seteo el Metodo de Envio
[request setHTTPMethod: @"POST"];
//Seteo los Datos a Enviar
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody: requestData];
//Creo la Conexion y automaticamente se envia
[[NSURLConnection alloc] initWithRequest:request delegate:self];
[requestBody release];
[request release];
return @"";
}
Can somebody help me?
Thanks and sorry for my poor english
Finally I make it work using NSMutableURLRequest and NSURLConnection with sendSynchronousRequest.
This is the code used:
}