How to send a large large string through NSURLConnection

This is my code.

- (void)loadData:(NSString *)url { NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"connection found---------"); } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSLog(@"reciving data---------"); } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"connection fail---------"); [self.pddelegate connectionError]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"data posting done---------"); [self.pddelegate dataPosted]; } 

It does not work if url gets larger and connection failure in logs.

how

 url=@ ".......order_details&admin=29&tableid=89&waiter_id=18&items=MzQ6MSwxMToxLDMzOjEsNjc6MSwzOToxLDY5OjEsNTY6MSw2ODoxLDg6MSw1NToxLDYyOjEsNzY6MSw0MToxLDIwOjEsNjE6MQ==" 
+4
source share
3 answers

see this SO post for a request length of type What is the maximum length of a URL in different browsers?

to send a large string, use a POST request instead of a GET type.

0
source

We have two methods for sending data. 1. GET method: used only for fixed length or limited string length. 2. POST method: used to send more lines when comparing the get method.

I gave an example of using PostMethod.

 NSString *post =[[NSString alloc] initWithFormat:@"%@",YourString]; NSURL *url=[NSURL URLWithString:*@"YourURL like www.google.com"*]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:url]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue: @"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; request.timeoutInterval = 60; NSError *error = nil; NSURLResponse *response; NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSString *errStr = _stringEmpty; @try { errStr = [error localizedDescription]; }@catch (NSException * exception){ } 

If an error occurs, errStr will display an error.

0
source

In the past, I used multiple URLs with a length of about 2000 characters in iOS without any problems. NSURL, NSURLRequest, and NSURLConnection did a great job. If your URL is shorter than this, the problem is probably not due to its length, but to the binding to the method of creating the URL.

0
source

Source: https://habr.com/ru/post/1482312/


All Articles