Mark Comix
Mark Comix

Reputation: 1898

Send and receive Strings using HTTP Post

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

Upvotes: 1

Views: 2747

Answers (2)

Mark Comix
Mark Comix

Reputation: 1898

Finally I make it work using NSMutableURLRequest and NSURLConnection with sendSynchronousRequest.

This is the code used:

- (NSString*) sendPostRequest:(NSString *)stringToSend{

NSString *urlString = [self serverURL];

NSURL *url = [NSURL URLWithString: urlString];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];

//set headers

[request addValue:@"Content-Type" forHTTPHeaderField:@"application/x-www-form-urlencoded"];

[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:[stringToSend dataUsingEncoding:NSASCIIStringEncoding]];

[request setTimeoutInterval:90]; // set timeout for 90 seconds

NSURLResponse **response = NULL;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:response error:nil];

NSString *responseDataString = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];

NSLog(@"Response %@", responseDataString);

return responseDataString;

}

Upvotes: 1

Kenny Winker
Kenny Winker

Reputation: 12097

You're trying to use an asynchronous api (NSURLRequest) in a synchronous way. NSURLRequest is made to work in the background using the delegate pattern. You start a request, and the request calls delegate methods on it's delegate when data is available or the connection is finished.

You should re-tool your app so that it can work this way. Network operations can take a long time, and you shouldn't halt things for the user unless you have to.

If you must stop your app until the network operation finishes, you should set some kind of a halt / resume function that disables your UI, rather than occupying the main thread.

jbat100 suggested ASIHTTPRequest. It's probably overkill, works asynchronously as well, and has been abandoned by it's author. I would say if you need to use a network library you should look into AFNetworking. Again, probably overkill, definitely asynchronous, but actively maintained and uses a blocks-based interface.

Upvotes: 1

Related Questions