1,HTTP常見的方法
GET 擷取指定資源
POST 2M 向指定資源提交資料進行處理請求,在RESTful風格中用於新增資源 HEAD 擷取指定資源頭部資訊
PUT 替換指定資源(不支援瀏覽器操作)
DELETE 刪除指定資源
2,設定管理員的put請求方式:
複製代碼 代碼如下:
1>
n 開啟終端
p cd /etc/apache2
p sudo vim httpd.conf
n 在vim中輸入
p /httpd-dav.conf
• 尋找httpd-dav.conf
p 按0將游標移動至行首 p 按x將行首的#刪除
p 輸入:wq,儲存並退出
2>
在終端繼續輸入
cd /etc/apache2/extra
sudo vim httpd-dav.conf
在vim中將右圖中第一處標紅位置 的Digest修改為Basic
輸入:wq,儲存並退出
提示:
修改的是使用者授權的方式
第二處標紅位置是儲存使用者密碼 的檔案(/user/user.passwd)
第三處標紅位置是能夠使 用PUT請求的使用者名稱(admin)
4>
在終端輸入 p cd /usr
sudo htpasswd -c /usr/user.passwd admin
ls-l
sudo chgrp www /usr/user.passwd
ls-l
5>
建立var檔案夾,儲存DavLockDB相關檔案 n sudo mkdir -p /usr/var
sudo chown -R www:www /usr/var
建立上傳檔案夾:uploads
sudo mkdir -p /usr/uploads
sudo chown -R www:www /usr/uploads
重新啟動Apache
sudo apachectl -k restart
6>當看到這個時就表示配置正確
修改後用ls -l查看的示意圖如下
如果能看到這三個就表示配置正確
uploads
user.passwd
var
執行個體:
複製代碼 代碼如下:
#import "KUViewController.h"
#import "KUProgress.h"
@interfaceKUViewController ()<NSURLSessionTaskDelegate>
//下載進度的類,繼承UIview
@property (weak, nonatomic) IBOutlet KUProgress *progressView;
@end
@implementation KUViewController
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self putFile];
}
/**
* 用PUT方法上傳檔案,不經過瀏覽器傳遞
*/
-(void)putFile
{
//1,url(協議+主機名稱+路徑+儲存到伺服器的檔案名稱)
// post:url (協議+主機名稱+上傳的伺服器的程式)
NSString *urlStr = @"http://localhost/uploads/046.Post提交使用者隱私資料&MD5加密.mp4";
//1.1編碼格式
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
//2,request 請求(預設是get)
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
//1>httpMethod
request.HTTPMethod = @"PUT";
//2>網路請求授權
/**
BASE64目前在網路上最流行的一種編碼方式,可以將二進位的資料轉換成字串,對方接受到之後,可以再講字串轉換成二進位檔案
BASE64可以編碼,也可以解碼
授權格式:
(1)授權字串格式:使用者名稱:口令
(2)授權模式:Basic Base64編碼的授權字串
(3)位HTTPHEADERField的Authorization賦值
*/
NSString *authStr = @"admin:admin";
//將字串轉換成 Base64
authStr = [self authBase64:authStr];
//轉換成第二部的
NSString *authBase64 = [NSString stringWithFormat:@"Basic %@",authStr];
//轉換成第三部
[request setValue:authBase64 forHTTPHeaderField:@"Authorization"];
//3,session
//1>.建立會話機制
NSURLSessionConfiguration *config = [NSURLSessionConfigurationdefaultSessionConfiguration];
NSURLSession *session = [NSURLSessionsessionWithConfiguration:config delegate:selfdelegateQueue:[[NSOperationQueuealloc] init]];
//2> 上傳任務
//上傳的檔案的路徑
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"01.Post提交使用者隱私資料&MD5加密.mp4" withExtension:nil];
[[session uploadTaskWithRequest:request fromFile:fileUrl] resume];
// 這是不用下載進度條的方法。
// NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromFile:fileUrl completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//
// //把位元據轉換成字串
// NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSLog(@"str = %@",str);
// }];
//
}
#pragma mark -- 代理方法
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
{
CGFloat value = (CGFloat)totalBytesSent / totalBytesExpectedToSend;
// [NSThread sleepForTimeInterval:0.2];
[[NSOperationQueuemainQueue] addOperationWithBlock:^{
self.progressView.progress = value;
}];
NSLog(@"下載進度;value = %.03lf",value);
}
-(void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error
{
NSLog(@"上傳失敗");
}
//轉換成Base64編碼授權字串
-(NSString *)authBase64:(NSString *)authStr
{
//將字串轉換成位元局
NSData *data = [authStr dataUsingEncoding:NSUTF8StringEncoding];
return [data base64EncodedStringWithOptions:0];
}