http編程綜述:亦可稱爲soap編程。一般狀況下,http編程要比socket編程相對要簡單易用得多。因此用的最廣普遍。web
1、http編程其實就是http請求。http請求最長用的方法是 get 和 post 方法。編程
==》get方法和post方法相比理解起來比較簡單,get方法能夠直接請求一個url,也能夠url後面拼接上參數做爲一個新的url地址進行請求。get方法後面的value要通過unicode編碼。form的enctype屬性默認爲application/x-www-form-urlencoded。不能發送二進制文件。
==》post方法相對要複雜一些。首先post方法要設置key和value ,全部的key和value都會拼接成 key1=value1&key2=value2的樣式的字符串,而後這個字符串轉化爲二進制放到 http請求的body中。當請求發送的時候,也就跟隨body一塊兒傳給服務器。http請求Content-Type設置爲:application/x-www-form-urlencoded。這裏講的只是簡單的post請求,通常發送文件不會選擇這種方式(從技術方面考慮也能夠發送文件,就是把文件以 key 和 value的方式放入)。下面咱們再討論一下post發送二進制文件更加廣泛的方法。緩存
2、HTTP協議是什麼?
簡單來講,就是一個基於應用層的通訊規範:雙方要進行通訊,你們都要遵照一個規範,這個規範就是HTTP協議。
HTTP協議能作什麼?
不少人首先必定會想到:瀏覽網頁。沒錯,瀏覽網頁是HTTP的主要應用,可是這並不表明HTTP就只能應用於網頁的瀏覽。HTTP是一種協議,只要通訊的雙方都遵照這個協議,HTTP就能有用武之地。好比我們經常使用的QQ,迅雷這些軟件,都會使用HTTP協議(還包括其餘的協議)。
HTTP協議如何工做?
你們都知道通常的通訊流程:首先客戶端發送一個請求(request)給服務器,服務器在接收到這個請求後將生成一個響應(response)返回給客戶端。
在這個通訊的過程當中HTTP協議在如下4個方面作了規定:
服務器
1. Request和Response的格式()
2. 創建鏈接的方式(一、非持久鏈接 二、持久鏈接)
3. 緩存的機制
4. 響應受權激發機制網絡
(應用場合)
5. 基於HTTP的應用(一、 HTTP代理 二、多線程下載 三、 HTTPS傳輸協議原理 四、開發web程序時經常使用的Request Methods 五、用戶與服務器的交互)多線程
經常使用cocoa內部類: app
1,Reachability.h 蘋果demo支持網絡鏈接診斷less
2,NSConnection鏈接、NSMutableURLRequest URL網址的請求封裝包socket
3,NSXMLParser XML解析ide
經常使用第三方庫:
1,ASIHttprequest 庫
操做步驟:
1:檢查網絡環境(3G/WIFI)
2:發起NSConnection請求
3:處理返回xml數據包(NSXMLParser解析xml文件),或者返回文件png、pdf之類
4:若返回數據包中含有待下載的圖片下載地址。則重複二、3步驟下載下來圖片。只不過3中返回的是文件。
socket編程綜述:
經常使用cocoa內部類:
經常使用第三方庫:1,Asyncsocket庫
操做步驟:
一:確認網絡環境3G/WIFI
1. 添加源文件和framework
開發Web等網絡應用程序的時候,須要確認網絡環境,鏈接狀況等信息。若是沒有處理它們,是不會經過Apple的審查的。
Apple 的 例程 Reachability 中介紹了取得/檢測網絡狀態的方法。要在應用程序程序中使用Reachability,首先要完成以下兩部:
1.1. 添加源文件:
在你的程序中使用 Reachability 只須將該例程中的 Reachability.h 和 Reachability.m 拷貝到你的工程中。以下圖:
1.2.添加framework:
將SystemConfiguration.framework 添加進工程。以下圖:
2. 網絡狀態
Reachability.h中定義了三種網絡狀態:
typedef enum {
NotReachable = 0, //無鏈接
ReachableViaWiFi, //使用3G/GPRS網絡
ReachableViaWWAN //使用WiFi網絡
} NetworkStatus;
所以能夠這樣檢查網絡狀態:
Reachability *r = [Reachability reachabilityWithHostName:@「www.apple.com」];
switch ([r currentReachabilityStatus]) {
case NotReachable:
// 沒有網絡鏈接
break;
case ReachableViaWWAN:
// 使用3G網絡
break;
case ReachableViaWiFi:
// 使用WiFi網絡
break;
}
3.檢查當前網絡環境
程序啓動時,若是想檢測可用的網絡環境,能夠像這樣
// 是否wifi
+ (BOOL) IsEnableWIFI {
return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}
// 是否3G
+ (BOOL) IsEnable3G {
return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
}
例子:
- (void)viewWillAppear:(BOOL)animated {
if (([Reachability reachabilityForInternetConnection].currentReachabilityStatus == NotReachable) &&
([Reachability reachabilityForLocalWiFi].currentReachabilityStatus == NotReachable)) {
self.navigationItem.hidesBackButton = YES;
[self.navigationItem setLeftBarButtonItem:nil animated:NO];
}
}
4. 連接狀態的實時通知
網絡鏈接狀態的實時檢查,通知在網絡應用中也是十分必要的。接續狀態發生變化時,須要及時地通知用戶:
Reachability 1.5版本
// My.AppDelegate.h
#import "Reachability.h"
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
NetworkStatus remoteHostStatus;
}
@property NetworkStatus remoteHostStatus;
@end
// My.AppDelegate.m
#import "MyAppDelegate.h"
@implementation MyAppDelegate
@synthesize remoteHostStatus;
// 更新網絡狀態
- (void)updateStatus {
self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];
}
// 通知網絡狀態
- (void)reachabilityChanged:(NSNotification *)note {
[self updateStatus];
if (self.remoteHostStatus == NotReachable) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName", nil)
message:NSLocalizedString (@"NotReachable", nil)
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
// 程序啓動器,啓動網絡監視
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// 設置網絡檢測的站點
[[Reachability sharedReachability] setHostName:@"www.apple.com"];
[[Reachability sharedReachability] setNetworkStatusNotificati*****Enabled:YES];
// 設置網絡狀態變化時的通知函數
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)
name:@"kNetworkReachabilityChangedNotification" object:nil];
[self updateStatus];
}
- (void)dealloc {
// 刪除通知對象
[[NSNotificationCenter defaultCenter] removeObserver:self];
[window release];
[super dealloc];
}
Reachability 2.0版本
// MyAppDelegate.h
@class Reachability;
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
Reachability *hostReach;
}
@end
// MyAppDelegate.m
- (void)reachabilityChanged:(NSNotification *)note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus status = [curReach currentReachabilityStatus];
if (status == NotReachable) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName""
message:@"NotReachable"
delegate:nil
cancelButtonTitle:@"YES" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// ...
// 監測網絡狀況
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name: kReachabilityChangedNotification
object: nil];
hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
hostReach startNotifer];
// ...
}
二:使用NSConnection下載數據
1.建立NSConnection對象,設置委託對象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[self urlString]]];
[NSURLConnection connectionWithRequest:request delegate:self];
2. NSURLConnection delegate委託方法
- (void)connection:(NSURLConnection *)connection didReceiveResp*****e:(NSURLResp*****e *)resp*****e;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
3. 實現委託方法
- (void)connection:(NSURLConnection *)connection didReceiveResp*****e:(NSURLResp*****e *)resp*****e {
// store data
[self.receivedData setLength:0]; //一般在這裏先清空接受數據的緩存
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
/* appends the new data to the received data */
[self.receivedData appendData:data]; //可能屢次收到數據,把新的數據添加在現有數據最後
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// 錯誤處理
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// disconnect
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSString *returnString = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
NSLog(returnString);
[self urlLoaded:[self urlString] data:self.receivedData];
firstTimeDownloaded = YES;
}
三:使用NSXMLParser解析xml文件
1. 設置委託對象,開始解析
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; //或者也可使用initWithContentsOfURL直接下載文件,可是有一個緣由不這麼作:
// It's also possible to have NSXMLParser download the data, by passing it a URL, but this is not desirable
// because it gives less control over the network, particularly in responding to connection errors.
[parser setDelegate:self];
[parser parse];
2. 經常使用的委託方法
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError;
static NSString *feedURLString = @"http://www.yifeiyang.net/test/test.xml";
3. 應用舉例
- (void)parseXMLFileAtURL:(NSURL *)URL parseError:(NSError **)error
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
NSError *parseError = [parser parserError];
if (parseError && error) {
*error = parseError;
}
[parser release];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString*)qName attributes:(NSDictionary *)attributeDict{
// 元素開始句柄
if (qName) {
elementName = qName;
}
if ([elementName isEqualToString:@"user"]) {
// 輸出屬性值
NSLog(@"Name is %@ , Age is %@", [attributeDict objectForKey:@"name"], [attributeDict objectForKey:@"age"]);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
// 元素終了句柄
if (qName) {
elementName = qName;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
// 取得元素的text
}
NSError *parseError = nil;
[self parseXMLFileAtURL:[NSURL URLWithString:feedURLString] parseError:&parseError];
//實例
//
// NLViewController.m
// NetWorkTest
//
// Created by Nono on 12-5-16.
// Copyright (c) 2012年 NonoWithLilith. All rights reserved.
//
#import "NLViewController.h"
@interface NLViewController ()
@implementation NLViewController
@synthesize label = _label;
@synthesize data = _data;
@synthesize connection = _connection;
- (void)dealloc{
[self.label release];
[self.data release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 10.0, 300.0, 400)];
self.label = label;
label.textAlignment = UITextAlignmentCenter;
[label setNumberOfLines:0];
label.lineBreakMode = UILineBreakModeWordWrap;
self.label.text = @"正在在請求數據";
[self.view addSubview:label];
[label release];
//step 1:請求地址
NSString *urlString = @"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlString];
//step 2:實例化一個request
NSURLRequest *requrst = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
//step 3:建立連接
self.connection = [[NSURLConnection alloc] initWithRequest:requrst delegate:self];
if ( self.connection) {
NSLog(@"連接成功");
}else {
NSLog(@"連接失敗");
}
[url release];
[urlString release];
[requrst release];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
self.label = nil;
self.data = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark-
#pragma NSUrlConnectionDelegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//接受一個服務端回話,再次通常初始化接受數據的對象
NSLog(@"返回數據類型:%@",[response textEncodingName]);
NSMutableData *d = [[NSMutableData alloc] init];
self.data = d;
[d release];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//接受返回數據,這個方法可能會被調用屢次,所以將屢次返回數據加起來
NSUInteger datalength = [data length];
NSLog(@"返回數據量:%d",datalength);
[self.data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//鏈接結束
NSLog(@"%d:",[self.data length]);
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *mystr = [[NSString alloc] initWithData:_data encoding:enc];
// string i
NSLog(@"最後的結果:%@",mystr);
self.label.text = mystr;
[mystr release];
[self.connection release];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//連接錯誤
}