iphone網路post串連的兩種處理方式(同步和非同步)(zz)

來源:互聯網
上載者:User

第一種: 直接返回方式。
-(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.
}


摘自 進階碼農的專欄

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.