WBHttpRequest.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // WBHttpRequest.h
  3. // WeiboSDK
  4. //
  5. // Created by DannionQiu on 14-9-18.
  6. // Copyright (c) 2014年 SINA iOS Team. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import <UIKit/UIKit.h>
  10. #pragma mark - WBHttpRequest and WBHttpRequestDelegate
  11. @class WBHttpRequest;
  12. /**
  13. 接收并处理来自微博sdk对于网络请求接口的调用响应 以及logOutWithToken的请求
  14. */
  15. @protocol WBHttpRequestDelegate <NSObject>
  16. /**
  17. 收到一个来自微博Http请求的响应
  18. @param response 具体的响应对象
  19. */
  20. @optional
  21. - (void)request:(WBHttpRequest *)request didReceiveResponse:(NSURLResponse *)response;
  22. /**
  23. 收到一个来自微博Http请求失败的响应
  24. @param error 错误信息
  25. */
  26. @optional
  27. - (void)request:(WBHttpRequest *)request didFailWithError:(NSError *)error;
  28. /**
  29. 收到一个来自微博Http请求的网络返回
  30. @param result 请求返回结果
  31. */
  32. @optional
  33. - (void)request:(WBHttpRequest *)request didFinishLoadingWithResult:(NSString *)result;
  34. /**
  35. 收到一个来自微博Http请求的网络返回
  36. @param data 请求返回结果
  37. */
  38. @optional
  39. - (void)request:(WBHttpRequest *)request didFinishLoadingWithDataResult:(NSData *)data;
  40. /**
  41. 收到快速SSO授权的重定向
  42. @param URI
  43. */
  44. @optional
  45. - (void)request:(WBHttpRequest *)request didReciveRedirectResponseWithURI:(NSURL *)redirectUrl;
  46. @end
  47. /**
  48. 微博封装Http请求的消息结构
  49. */
  50. @interface WBHttpRequest : NSObject
  51. {
  52. NSURLConnection *connection;
  53. NSMutableData *responseData;
  54. }
  55. /**
  56. 用户自定义请求地址URL
  57. */
  58. @property (nonatomic, strong) NSString *url;
  59. /**
  60. 用户自定义请求方式
  61. 支持"GET" "POST"
  62. */
  63. @property (nonatomic, strong) NSString *httpMethod;
  64. /**
  65. 用户自定义请求参数字典
  66. */
  67. @property (nonatomic, strong) NSDictionary *params;
  68. /**
  69. WBHttpRequestDelegate对象,用于接收微博SDK对于发起的接口请求的请求的响应
  70. */
  71. @property (nonatomic, weak) id<WBHttpRequestDelegate> delegate;
  72. /**
  73. 用户自定义TAG
  74. 用于区分回调Request
  75. */
  76. @property (nonatomic, strong) NSString* tag;
  77. /**
  78. 统一HTTP请求接口
  79. 调用此接口后,将发送一个HTTP网络请求
  80. @param url 请求url地址
  81. @param httpMethod 支持"GET" "POST"
  82. @param params 向接口传递的参数结构
  83. @param delegate WBHttpRequestDelegate对象,用于接收微博SDK对于发起的接口请求的请求的响应
  84. @param tag 用户自定义TAG,将通过回调WBHttpRequest实例的tag属性返回
  85. */
  86. + (WBHttpRequest *)requestWithURL:(NSString *)url
  87. httpMethod:(NSString *)httpMethod
  88. params:(NSDictionary *)params
  89. delegate:(id<WBHttpRequestDelegate>)delegate
  90. withTag:(NSString *)tag;
  91. /**
  92. 统一微博Open API HTTP请求接口
  93. 调用此接口后,将发送一个HTTP网络请求(用于访问微博open api)
  94. @param accessToken 应用获取到的accessToken,用于身份验证
  95. @param url 请求url地址
  96. @param httpMethod 支持"GET" "POST"
  97. @param params 向接口传递的参数结构
  98. @param delegate WBHttpRequestDelegate对象,用于接收微博SDK对于发起的接口请求的请求的响应
  99. @param tag 用户自定义TAG,将通过回调WBHttpRequest实例的tag属性返回
  100. */
  101. + (WBHttpRequest *)requestWithAccessToken:(NSString *)accessToken
  102. url:(NSString *)url
  103. httpMethod:(NSString *)httpMethod
  104. params:(NSDictionary *)params
  105. delegate:(id<WBHttpRequestDelegate>)delegate
  106. withTag:(NSString *)tag;
  107. /**
  108. 取消网络请求接口
  109. 调用此接口后,将取消当前网络请求,建议同时[WBHttpRequest setDelegate:nil];
  110. 注意:该方法只对使用delegate的request方法有效。无法取消任何使用block的request的网络请求接口。
  111. */
  112. - (void)disconnect;
  113. #pragma mark - block extension
  114. typedef void (^WBRequestHandler)(WBHttpRequest *httpRequest,
  115. id result,
  116. NSError *error);
  117. /**
  118. 统一微博Open API HTTP请求接口
  119. 调用此接口后,将发送一个HTTP网络请求(用于访问微博open api)
  120. @param url 请求url地址
  121. @param httpMethod 支持"GET" "POST"
  122. @param params 向接口传递的参数结构
  123. @param queue 发起请求的NSOperationQueue对象,如queue为nil,则在主线程([NSOperationQueue mainQueue])发起请求。
  124. @param handler 接口请求返回调用的block方法
  125. */
  126. + (WBHttpRequest *)requestWithURL:(NSString *)url
  127. httpMethod:(NSString *)httpMethod
  128. params:(NSDictionary *)params
  129. queue:(NSOperationQueue*)queue
  130. withCompletionHandler:(WBRequestHandler)handler;
  131. /**
  132. 统一HTTP请求接口
  133. 调用此接口后,将发送一个HTTP网络请求
  134. @param url 请求url地址
  135. @param httpMethod 支持"GET" "POST"
  136. @param params 向接口传递的参数结构
  137. @param queue 发起请求的NSOperationQueue对象,如queue为nil,则在主线程([NSOperationQueue mainQueue])发起请求。
  138. @param handler 接口请求返回调用的block方法
  139. */
  140. + (WBHttpRequest *)requestWithAccessToken:(NSString *)accessToken
  141. url:(NSString *)url
  142. httpMethod:(NSString *)httpMethod
  143. params:(NSDictionary *)params
  144. queue:(NSOperationQueue*)queue
  145. withCompletionHandler:(WBRequestHandler)handler;
  146. @end