Ios 如何通过NSURLConnection发送大字符串
这是我的密码Ios 如何通过NSURLConnection发送大字符串,ios,nsurlconnection,nsurlrequest,Ios,Nsurlconnection,Nsurlrequest,这是我的密码 - (void)loadData:(NSString *)url { NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; } - (void)connection:(NSURLConnection *)connection di
- (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];
}
如果url变得更大,并在日志中给出连接失败,则它不起作用
像
有关获取类型请求长度,请参阅本文
要发送大字符串,请使用POST类型请求而不是GET类型。我们有两种发送数据的方法。 1.GET方法:仅用于固定长度或有限长度的字符串。 2.POST方法:用于在比较get方法时发送更多字符串 我已经给出了使用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){ }
如果出现任何错误,errStr将显示错误。过去,我在iOS中使用了一些长度约为2000个字符的URL,没有问题。NSURL、NSURLRequest和NSURLConnection都管理得很好。如果您的URL短于此,则问题可能不是由于其长度,而是与URL的构造方式有关。什么不起作用,在尝试创建URL之前您是否对任何字符进行了编码。是的,我在创建URL之前使用base64编码。请您将此信息放入:
NSLog(@“%@,[error localizedDescription]);代码>在连接中:didFailWithError:
并告诉我们输出是什么?问题可能不是url的大小,而是转义字符。@SimonM上述日志的输出是错误的url
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){ }