No Description

WBHttpRequest.h 5.6KB

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