網絡請求的步驟:
get請求:
#pragma mark - 這是私有方法,盡量不要再方法中直接使用屬性,因為一般來說屬性都是和界面關聯的,我們可以通過參數的方式來使用屬性
#pragma mark Get登錄方法
- (void)loginWithGet:(NSString *)name pwd:(NSString *)pwd
{
//1確定地址NSURL
NSString *urlString = [NSString stringWithFormat:@"www.baidu.com?username=%@&password=%@", name, pwd];
NSLog(@"%@",urlString);
//url中,如果包含中文字符需要轉換成帶百分號的格式,提供給服務器解碼(如果服務器用的是utf-8)。
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",urlString);
NSURL *url = [NSURL URLWithString:urlString];
//2建立請求NSURLRequest
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3建立並啟動連接NSRULConnection
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
[conn start]; //啟動連接,這是網絡請求已經發生了。這是一個異步連接請求,,請求發送出去以後,就交由代理處理。
//服務器通知准備,准備中轉數據
self.serverData = [NSMutableData data];
}
- (void)login
{
NSLog(@"come here");
NSString *userName = self.nameTextField.text;
NSString *pwd = self.passwordTextField.text;
//[self loginWithGet:userName pwd:pwd]; //用get的方式調用
//上面一行是get方式,下面是post方式。
//1確定地址NSURL
NSString *urlString = [NSString stringWithFormat:@"www.baidu.com"];
NSURL *url = [NSURL URLWithString:urlString];
//2建立請求NSMutableURLRequest(post需要用這個)
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//1)post請求方式,網絡請求默認是get方法,所以如果我們用post請求,必須聲明請求方式。
[request setHTTPMethod:@"POST"];
//2)post請求的數據體,post請求中數據體時,如果有中文,不需要轉換。因為ataUsingEncoding方法已經實現了轉碼。
NSString *bodyStr = [NSString stringWithFormat:@"username=%@&password=%@", userName, pwd];
//將nstring轉換成nsdata
NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"body data %@", body);
[request setHTTPBody:body];
//3建立並啟動連接NSRULConnection
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
[conn start]; //啟動連接,這是網絡請求已經發生了。這是一個異步連接請求,,請求發送出去以後,就交由代理處理。
//服務器通知准備,准備中轉數據
self.serverData = [NSMutableData data];
}
//4通過代理方法處理網絡請求,遵守協議
#pragma mark 網絡數據處理代理,總共有五個代理方法
#pragma mark 代理方法1 接受到服務器的響應,服務器要傳數據了,客戶端做好接收准備
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
}
#pragma mark 代理方法2 接收服務器傳輸的數據,可能會多次執行
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//對每次傳輸的數據進行拼接,需要中轉數據
[self.serverData appendData:data];
}
#pragma mark 代理方法3 接收數據完成,做後續處理
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//對方法2拼接的數據,做後續處理。
NSString *str = [[NSString alloc] initWithData:self.serverData encoding:NSUTF8StringEncoding];
//對服務器返回的字符串進行處理。
//1 從str中找出的用戶名所在的位置
NSRange range = [str rangeOfString:@"用戶名"]; //nsrange存放查找到的字符串(用戶名)的位置和長度
NSLog(@"%@", NSStringFromRange(range));
NSString *msg = nil;
if (range.location > 0) {
//2曲用戶名後面的字符串,一直到末尾
NSString *name = [str substringFromIndex:(range.location +range.length)];
NSLog(@"%@",name);
//3歡迎歸來
msg= [NSString stringWithFormat:@"歡迎歡迎:%@", name];
}else
{
msg = @"用戶名或者密碼錯誤,請重試!";
}
NSLog(@"%@", str);
//提示用戶登錄成功
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alertView show];
//清空數據
self.serverData = nil;
}
#pragma mark 代理方法4 f服務器請求失敗,原因很多(w網絡環境等等);
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"網絡連接請求錯誤%@",error.localizedDescription); //本地化的錯誤信息描述。
}
#pragma mark d代理方法5 向服務器發送數據,次方法僅適用於post,尤其上傳文件。
/*
第一個參數是連接,第二個參數是發送的數據體,第三個表示整體要寫的數據,第四個是表示預期要寫的數據。服務器通過這些值知道這次傳了多少,已經傳了多少,預期總共要穿多少
*/
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
NSLog(@"發送數據給服務器");
}
@end