iphone网络post连接的两种处理方式(同步和异步)(zz)
来源:互联网 发布:mac上怎么裁剪图片 编辑:程序博客网 时间:2024/05/23 11:20
第一种: 直接返回方式。
-(void)UpadaPost:(NSString *)strcontext URL:(NSString *)urlstr{
NSLog(urlstr);
NSLog(strcontext);
assert(strcontext != NULL);
assert(urlstr != NULL);
NSData *postData = [strcontext dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlstr]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *respone;
NSError *error;
NSData *myReturn =[NSURLConnection sendSynchronousRequest:request returningResponse:&respone
error:error];
NSLog(@"%@", [[NSString alloc] initWithData:myReturn encoding:NSUTF8StringEncoding]);
}
第二种,采用事件代理方式
-(void)UpadaPost:(NSString *)strcontext URL:(NSString *)urlstr{
NSLog(urlstr);
NSLog(strcontext);
assert(strcontext != NULL);
assert(urlstr != NULL);
NSData *postData = [strcontext dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlstr]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn)
{
NSLog(@"Connection success");
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[conn retain];
}
else
{
// inform the user that the download could not be made
}
}
// 收到响应时, 会触发
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// 注意这里将NSURLResponse对象转换成NSHTTPURLResponse对象才能去
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSDictionary *dictionary = [httpResponse allHeaderFields];
NSLog([dictionary description]);
NSLog(@"%d",[response statusCode]);
}
}
// Forward errors to the delegate.
//链接错误
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
//[self performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil waitUntilDone:NO];
NSLog(@"%@",[error localizedDescription]);
}
// Called when a chunk of data has been downloaded.
//接收数据 每收到一次数据, 会调用一次
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Process the downloaded chunk of data.
NSLog(@"%d", [data length]);
//NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
//[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO];
}
//接收结束
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"%@",connection);
//NSLog(@"%lld", received_);
//[self performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil waitUntilDone:NO];
// Set the condition which ends the run loop.
}
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlstr]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *respone;
NSError *error;
NSData *myReturn =[NSURLConnection sendSynchronousRequest:request returningResponse:&respone
error:error];
NSLog(@"%@", [[NSString alloc] initWithData:myReturn encoding:NSUTF8StringEncoding]);
}
第二种,采用事件代理方式
-(void)UpadaPost:(NSString *)strcontext URL:(NSString *)urlstr{
NSLog(urlstr);
NSLog(strcontext);
assert(strcontext != NULL);
assert(urlstr != NULL);
NSData *postData = [strcontext dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlstr]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn)
{
NSLog(@"Connection success");
[UIApplication sharedApplication].networkActivityIndicator
[conn retain];
}
else
{
// inform the user that the download could not be made
}
}
// 收到响应时, 会触发
- (void)connection:(NSURLConnection *)connection
// 注意这里将NSURLResponse对象转换成NSHTTPURLResponse对象才能去
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSDictionary *dictionary = [httpResponse allHeaderFields];
NSLog([dictionary description]);
NSLog(@"%d",[response statusCode]);
}
}
// Forward errors to the delegate.
//链接错误
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
//[self performSelectorOnMainThr
NSLog(@"%@",[error localizedDescription]);
}
// Called when a chunk of data has been downloaded.
//接收数据 每收到一次数据, 会调用一次
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Process the downloaded chunk of data.
NSLog(@"%d", [data length]);
//NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
//[self performSelectorOnMainThr
}
//接收结束
- (void)connectionDidFinishLoadi
NSLog(@"%@",connection);
//NSLog(@"%lld", received_);
//[self performSelectorOnMainThr
// Set the condition which ends the run loop.
}
- iphone网络post连接的两种处理方式(同步和异步)(zz)
- Iphone网络post连接的两种处理方式(同步和异步)
- 网络处理2-异步POST请求和同步请求
- 网络处理(2)-异步POST请求和同步请求
- 网络处理2-异步POST请求和同步请求
- iphone网络编程(同步/异步,get/post)
- 42.网络请求POST同步和异步
- boost::asio的同步方式和异步方式网络编程
- GCD实现异步任务同步的两种方式
- 网络数据请求(同步/异步连接下的GET/POST请求)
- 两种HTTP连接方式POST&GET的比较
- 两种HTTP连接方式POST&GET的比较
- 两种HTTP连接方式POST&GET的比较
- UI网络笔记(二):UI网络之Post同步和异步的密文请求
- Android-网络提交数据get和post的两种方式
- httpURLConnection-网络请求的两种方式-get请求和post请求
- httpURLConnection-网络请求的两种方式-get请求和post请求
- 网络访问GET,POST两种方式的基本步骤
- How To Load .NET Assemblies In A PowerShell Session
- 有关男女比例趋势的面试题
- 如何加载图片按钮
- 逻辑移位与算术移位的区别
- 在Windows下配置apache跑fcgi程序
- iphone网络post连接的两种处理方式(同步和异步)(zz)
- google地图的使用
- php中时间轴开发
- webkit内核源码分析一
- Adobe推出数字出版套装软件单机版
- 射频卡的印刷方法
- Objective-C 2.0 学习笔记三(数据类型和表达式)
- java多线程学习2-线程栈模型与线程的变量
- android 分辨率标题栏及状态栏高度