DWHTTPRequest.h 3.81 KB
Newer Older
Han Ruisong's avatar
Han Ruisong committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#import <Foundation/Foundation.h>

typedef void (^DWHTTPRequestDrmProgressBlock)(NSData *data, NSInteger totalBytesReceived, NSInteger totalBytesExpectedToReceive);
typedef void (^DWErrorBlock)(NSError *error);
typedef void (^DWHTTPRequestReceiveResponseBlock)(NSHTTPURLResponse *response);
typedef void (^DWHTTPRequestDownloadProgressBlock)(NSInteger bytesWritten, NSInteger totalBytesReceived, NSInteger totalBytesExpectedToReceive);
typedef void (^DWHTTPRequestUploadProgressBlock)(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite);
typedef void (^DWHTTPRequestFinishLoadingBlock)(NSHTTPURLResponse *response, NSData *responseBody);
typedef void (^DWHTTPRequestFinishLoadingStringBlock)(NSHTTPURLResponse *response, NSString *responseBodyString);

@interface DWHTTPRequest : NSObject <NSURLConnectionDataDelegate,NSURLSessionTaskDelegate,NSURLSessionDelegate>{
    NSURLSession *inProcessSession;
}

# pragma mark - http request
Hanruisong's avatar
Hanruisong committed
16
17
18
19
/**
 *  @brief url
 */
@property(strong, nonatomic)NSURL *url;
Han Ruisong's avatar
Han Ruisong committed
20

Hanruisong's avatar
Hanruisong committed
21
22
23
24
/**
 *  @brief 请求方式
 */
@property(copy, nonatomic)NSString *HTTPMethod;
Han Ruisong's avatar
Han Ruisong committed
25

Hanruisong's avatar
Hanruisong committed
26
27
28
29
/**
 *  @brief 请求头
 */
@property(strong, nonatomic, readonly)NSDictionary *requestHeaders;
Han Ruisong's avatar
Han Ruisong committed
30

Hanruisong's avatar
Hanruisong committed
31
32
33
34
/**
 *  @brief 请求body
 */
@property(strong, nonatomic)NSData *requestBody;
Han Ruisong's avatar
Han Ruisong committed
35
36

/**
Hanruisong's avatar
Hanruisong committed
37
 *  @brief 请求超时时间,默认10秒
Han Ruisong's avatar
Han Ruisong committed
38
 */
Hanruisong's avatar
Hanruisong committed
39
@property(assign, nonatomic)NSTimeInterval timeoutSeconds;
Han Ruisong's avatar
Han Ruisong committed
40

Hanruisong's avatar
Hanruisong committed
41
# pragma mark - http response
Han Ruisong's avatar
Han Ruisong committed
42
/**
Hanruisong's avatar
Hanruisong committed
43
 *  @brief 响应头
Han Ruisong's avatar
Han Ruisong committed
44
 */
Hanruisong's avatar
Hanruisong committed
45
@property(strong, nonatomic, readonly)NSHTTPURLResponse *response;
Han Ruisong's avatar
Han Ruisong committed
46
47

/**
Hanruisong's avatar
Hanruisong committed
48
 *  @brief 响应头字符串
Han Ruisong's avatar
Han Ruisong committed
49
 */
Hanruisong's avatar
Hanruisong committed
50
@property(assign, nonatomic)NSStringEncoding forcedResponseEncoding;
Han Ruisong's avatar
Han Ruisong committed
51
52

/**
Hanruisong's avatar
Hanruisong committed
53
 *  @brief 响应体body
Han Ruisong's avatar
Han Ruisong committed
54
 */
Hanruisong's avatar
Hanruisong committed
55
@property(strong, nonatomic, readonly)NSData *responseBody;
Han Ruisong's avatar
Han Ruisong committed
56
57

/**
Hanruisong's avatar
Hanruisong committed
58
 *  @brief 响应体body字符串
Han Ruisong's avatar
Han Ruisong committed
59
 */
Hanruisong's avatar
Hanruisong committed
60
61
@property(copy, nonatomic, readonly)NSString *responseBodyString;

Han Ruisong's avatar
Han Ruisong committed
62
63

/**
Hanruisong's avatar
Hanruisong committed
64
65
 *  若设置为YES,则在 -connection:didReceiveData: 中不会copy接受的数据。
 *  默认为NO,若使用 -startAsynchronousWithDestinationFilePath:,则接收到的数据会被写入文件,否则追加到buffer。
Han Ruisong's avatar
Han Ruisong committed
66
 */
Hanruisong's avatar
Hanruisong committed
67
68
69
70
71
@property(assign, nonatomic)BOOL unCopyResponseBody;
@property(copy)DWHTTPRequestDrmProgressBlock drmProgressBlock;
@property(copy)DWHTTPRequestReceiveResponseBlock receiveReponseBlock;
@property(copy)DWHTTPRequestDownloadProgressBlock downoadProgressBlock;
@property(copy)DWHTTPRequestUploadProgressBlock uploadProgressBlock;
Han Ruisong's avatar
Han Ruisong committed
72

Hanruisong's avatar
Hanruisong committed
73
74
75
76
77
78
79
/**
 非点播产品线,只需关心这3个回调即可!!!
 */
/**
 *  @brief 网络请求完成回调
 */
@property(copy)DWHTTPRequestFinishLoadingBlock finishLoadingBlock;
Han Ruisong's avatar
Han Ruisong committed
80

Hanruisong's avatar
Hanruisong committed
81
82
83
84
/**
 *  @brief 网络请求完成回调
 */
@property(copy)DWHTTPRequestFinishLoadingStringBlock finishLoadingStringBlock;
Han Ruisong's avatar
Han Ruisong committed
85
86

/**
Hanruisong's avatar
Hanruisong committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
 *  @brief 网络请求失败回调
 */
@property(copy)DWErrorBlock errorBlock;

/*!
 * @method
 * @abstract 通过NSURL初始化
 * @discussion 设置默认请求超时时间:10秒
 * @param url 网络请求URL
 * @result DWHTTPRequest对象
 */
+(DWHTTPRequest *)requestWithURL:(NSURL *)url;

/*!
 * @method
 * @abstract 通过字符串url初始化
 * @discussion 设置默认请求超时时间:10秒
 * @param urlString 字符串url
 * @result DWHTTPRequest对象
Han Ruisong's avatar
Han Ruisong committed
106
 */
Hanruisong's avatar
Hanruisong committed
107
108
109
110
111
112
113
114
115
116
+(DWHTTPRequest *)requestWithURLString:(NSString *)urlString;

/*!
 * @method
 * @abstract 发起同步HTTP请求
 * @discussion 发起同步HTTP请求
 * @param error 错误信息
 * @result HTTP响应体
 */
-(NSData *)startSynchronousWithError:(NSError **)error;
Han Ruisong's avatar
Han Ruisong committed
117

Hanruisong's avatar
Hanruisong committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*!
 * @method
 * @abstract 发起异步HTTP请求
 * @discussion 发起异步HTTP请求
 */
-(void)startAsynchronous;

/*!
* @method
* @abstract 结束本次HTTP请求
* @discussion 结束本次HTTP请求
*/
-(void)finish;

/*!
 * @method
 * @abstract 设置HTTPHeaderField
 * @discussion 设置HTTPHeaderField
 * @param value 值
 * @param name key
 */
-(void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)name;
Han Ruisong's avatar
Han Ruisong committed
140
141
142


@end