1,HTTP常见的方法
GET 获取指定资源
POST 2M 向指定资源提交数据进行处理请求,在RESTful风格中用于新增资源 HEAD 获取指定资源头部信息
PUT 替换指定资源(不支持浏览器操作)
DELETE 删除指定资源
2,配置服务器的put请求方式:
1>n 打开终端 p cd /etc/apache2 p sudo vim httpd.confn 在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];
}
© 版权声明
本文刊载的所有内容,包括文字、图片、音频、视频、软件、程序、以及网页版式设计等部门来源于互联网,版权均归原作者所有!本网站提供的内容服务于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯本网站及相关权利人的合法权利。
联系信息:邮箱aoxolcom@163.com或见网站底部。
联系信息:邮箱aoxolcom@163.com或见网站底部。
THE END


















请登录后发表评论
注册
社交帐号登录