2013年10月4日 星期五

iOS-Web(1)

最近覺得web develop蠻有趣的,之前在iOS上開發相關的App,總是用別人的framework來parse json或其他的data,其實並不用這麼麻煩,只要搞懂HTTP協定中的各種欄位,以及會時寫Async的上下載方式,就可以完成一個簡單的iOS web lib惹!(個人認為)

之前在寫相關的App看到別人用NSMutableURLRequest這個物件時,就自動認定我GG惹!只好趕快求助,真是不及格的實習工程師,最近因為工作上沒人求助,只好自己認真google了一下才終於有些概念(如果會寫PHP就好惹QQ),首先從HTTP header來看:

request line or status line
General header
request header or response header
Entity header
body message

例如常常會看到(用chrome的web develop工具)如下圖,為一個HTTP header的範例(chrome把它自動變成開發者比較好閱讀的格式)可以看到對應的欄位,大致上HTTP header可以分成兩種,一種是request一種是response,就像是對web server request一個網頁,而web server response 需要的網頁。
HTTP header個人認為有可以分成兩個部分,一個是request or response的訊息(EX: POST /path/image HTTP/1.1),另外是一些需要讓瀏覽器知道的資訊,也就是request header or response header,其中所有的欄位都沒有強制要填入header中,所以視需求將欄位填入。(上網找惹一下好像HOST欄位在HTTP/1.1的版本中有強制要填入的樣子)

回到正題,如果我們要用NSMutableURLRequest,透過HTTP來對server請求訊息的話,可以分解成以下步驟:
1. 宣告NSMutableURLRequest一個物件
2. 設定HTTP method(ex: POST, GET, PUT)
3. 設定適當的header欄位,讓server知道該做怎樣的response
4. 把data用NSData包起來然後設定body
5. 用NSURLConnection來傳送request

POST example:

+ (NSData *)HttpPostMethod:(NSString *)url postStr:(NSString *)postString{

    //1
    NSMutableURLRequest *myMutableRequest =[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
    //2
    [myMutableRequest setHTTPMethod:@"POST"];
    //3
    [myMutableRequest setValue:@"application/jsonrequest" forHTTPHeaderField:@"Accept"];
    [myMutableRequest setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[postString length]] forHTTPHeaderField:@"Content-Length"];
    [myMutableRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    //4
    [myMutableRequest setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSLog(@"request = %@",[myMutableRequest allHTTPHeaderFields]);
    NSLog(@"body = %@",[[NSString alloc] initWithData:myMutableRequest.HTTPBody encoding:NSUTF8StringEncoding]);
    
    //5
    NSError *error;
    NSURLResponse *response;
    NSData *retData = [NSURLConnection sendSynchronousRequest:myMutableRequest returningResponse:&response error:&error];
    return retData;
}

PUT example:

+ (NSData *)HttpPutMethod:(NSString *)url putStr:(NSString *)putString{
    
    //1
    NSMutableURLRequest *myMutableRequest =[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
    //2
    [myMutableRequest setHTTPMethod:@"PUT"];
    //3
    [myMutableRequest setValue:@"application/jsonrequest" forHTTPHeaderField:@"Accept"];
    [myMutableRequest setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[putString length]] forHTTPHeaderField:@"Content-Length"];
    [myMutableRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    //4
    [myMutableRequest setHTTPBody:[putString dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSLog(@"request = %@",[myMutableRequest allHTTPHeaderFields]);
    NSLog(@"body = %@",[[NSString alloc] initWithData:myMutableRequest.HTTPBody encoding:NSUTF8StringEncoding]);
    //5
    NSError *error;
    NSURLResponse *response;
    NSData *retData = [NSURLConnection sendSynchronousRequest:myMutableRequest returningResponse:&response error:&error];
    
    return retData;
}

DELETE example:

+ (NSData *)HttpDeleteMethod:(NSString *)url deleteStr:(NSString *)deleteString{
    
    //1
    NSMutableURLRequest *myMutableRequest =[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
    //2
    [myMutableRequest setHTTPMethod:@"DELETE"];
    //3
    [myMutableRequest setValue:@"application/jsonrequest" forHTTPHeaderField:@"Accept"];
    [myMutableRequest setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[deleteString length]] forHTTPHeaderField:@"Content-Length"];
    [myMutableRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    //4
    [myMutableRequest setHTTPBody:[deleteString dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSLog(@"request = %@",[myMutableRequest allHTTPHeaderFields]);
    NSLog(@"body = %@",[[NSString alloc] initWithData:myMutableRequest.HTTPBody encoding:NSUTF8StringEncoding]);
    //5
    NSError *error;
    NSURLResponse *response;
    NSData *retData = [NSURLConnection sendSynchronousRequest:myMutableRequest returningResponse:&response error:&error];
    
    return retData;
}

沒有留言:

張貼留言