You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
7.2 KiB

//
// MOBFHttpService.h
// MOBFoundation
//
// Created by vimfung on 15-1-20.
// Copyright (c) 2015年 MOB. All rights reserved.
//
#import <Foundation/Foundation.h>
@class MOBFHttpService;
/**
* GET方式
*/
extern NSString *const kMOBFHttpMethodGet;
/**
* POST方式
*/
extern NSString *const kMOBFHttpMethodPost;
/**
* DELETE方式
*/
extern NSString *const kMOBFHttpMethodDelete;
/**
* HEAD方式
*/
extern NSString *const kMOBFHttpMethodHead;
/**
* HTTP返回事件
*
* @param response
* @param responseData
*/
typedef void(^MOBFHttpResultEvent) (NSHTTPURLResponse *response, NSData *responseData);
/**
* HTTP错误事件
*
* @param error
*/
typedef void(^MOBFHttpFaultEvent) (NSError *error);
/**
* HTTP上传数据事件
*
* @param totalBytes
* @param loadedBytes
*/
typedef void(^MOBFHttpUploadProgressEvent) (int64_t totalBytes, int64_t loadedBytes);
/**
* HTTP下载数据事件
*
* @param totalBytes
* @param loadedBytes
*/
typedef void(^MOBFHttpDownloadProgressEvent) (int64_t totalBytes, int64_t loadedBytes);
/**
* HTTP服务类
*/
@interface MOBFHttpService : NSObject
/**
* @brief GET
*/
@property (nonatomic, copy) NSString *method;
/**
*/
@property (nonatomic) NSTimeInterval timeout;
/**
* @brief YES
*/
@property (nonatomic) BOOL isCacheResponse;
/**
* YESNO则表示使用自定义的方式发起请求
*/
@property (nonatomic) BOOL autoFillRequestForm;
/**
* HTTP服务
*
* @param urlString URL地址字符串
*
* @return HTTP服务对象
*/
- (id)initWithURLString:(NSString *)urlString;
/**
* HTTP服务
*
* @param URL URL地址对象
*
* @return HTTP服务对象
*/
- (id)initWithURL:(NSURL *)URL;
/**
* HTTP服务
*
* @param request
*
* @return HTTP服务对象
*/
- (id)initWithRequest:(NSURLRequest *)request;
/**
* HTTP头
*
* @param header
* @param value
*/
- (void)addHeader:(NSString *)header value:(NSString *)value;
/**
* HTTP头集合
*
* @param headers
*/
- (void)addHeaders:(NSDictionary *)headers;
/**
*
*
* @param value
* @param key
*/
- (void)addParameter:(id)value forKey:(NSString *)key;
/**
*
*
* @param parameters
*/
- (void)addParameters:(NSDictionary *)parameters;
/**
*
*
* @param fileData
* @param fileName
* @param mimeType MIME类型
* @param transferEncoding
* @param key
*/
- (void)addFileParameter:(NSData *)fileData
fileName:(NSString *)fileName
mimeType:(NSString *)mimeType
transferEncoding:(NSString *)transferEncoding
forKey:(NSString *)key;
/**
* NSData或者NSInputStream
*
* @param body
*/
- (void)setBody:(id)body;
/**
*
*
* @param resultHandler
* @param faultHandler
* @param uploadProgressHandler
*/
- (void)sendRequestOnResult:(MOBFHttpResultEvent)resultHandler
onFault:(MOBFHttpFaultEvent)faultHandler
onUploadProgress:(MOBFHttpUploadProgressEvent)uploadProgressHandler;
/**
@param resultHandler
@param faultHandler
@param uploadProgressHandler
@param downloadProgressHandler
*/
- (void)sendRequestOnResult:(MOBFHttpResultEvent)resultHandler
onFault:(MOBFHttpFaultEvent)faultHandler
onUploadProgress:(MOBFHttpUploadProgressEvent)uploadProgressHandler
onDownloadProgress:(MOBFHttpDownloadProgressEvent)downloadProgressHandler;
/**
*
*/
- (void)cancelRequest;
/**
* HTTP请求
*
* @param urlString
* @param method
* @param parameters
* @param headers
* @param resultHandler
* @param faultHandler
* @param uploadProgressHandler
*
* @return HTTP服务对象
*/
+ (MOBFHttpService *)sendHttpRequestByURLString:(NSString *)urlString
method:(NSString *)method
parameters:(NSDictionary *)parameters
headers:(NSDictionary *)headers
onResult:(MOBFHttpResultEvent)resultHandler
onFault:(MOBFHttpFaultEvent)faultHandler
onUploadProgress:(MOBFHttpUploadProgressEvent)uploadProgressHandler;
/**
* HTTP请求
*
* @param urlString
* @param method
* @param parameters
* @param headers
* @param timeout
* @param resultHandler
* @param faultHandler
* @param uploadProgressHandler
*
* @return HTTP服务对象
*/
+ (MOBFHttpService *)sendHttpRequestByURLString:(NSString *)urlString
method:(NSString *)method
parameters:(NSDictionary *)parameters
headers:(NSDictionary *)headers
timeout:(NSTimeInterval)timeout
onResult:(MOBFHttpResultEvent)resultHandler
onFault:(MOBFHttpFaultEvent)faultHandler
onUploadProgress:(MOBFHttpUploadProgressEvent)uploadProgressHandler;
/**
* HTTP请求
*
* @param urlString
* @param method
* @param parameters
* @param headers
* @param timeout
* @param resultHandler
* @param faultHandler
* @param uploadProgressHandler
* @param downloadProgressHandler
*
* @return HTTP服务对象
*/
+ (MOBFHttpService *)sendHttpRequestByURLString:(NSString *)urlString
method:(NSString *)method
parameters:(NSDictionary *)parameters
headers:(NSDictionary *)headers
timeout:(NSTimeInterval)timeout
onResult:(MOBFHttpResultEvent)resultHandler
onFault:(MOBFHttpFaultEvent)faultHandler
onUploadProgress:(MOBFHttpUploadProgressEvent)uploadProgressHandler
onDownloadProgress:(MOBFHttpDownloadProgressEvent)downloadProgressHandler;
@end