志愿者匹配API Objective C
我正在使用AFNetworking在VolunteerMatch API上执行Web请求。 当我执行请求时,我收到一个200代码,但是我没有收到答复。 这是一个典型的VolunteerMatch请求的样子:
GET /api/call?action=helloWorld&query=... HTTP/1.1 Host: www.volunteermatch.org Accept-Charset: UTF-8 Content-Type: application/json Authorization: WSSE profile="UsernameToken" X-WSSE: UsernameToken Username="acme", PasswordDigest="quR/EWLAV4xLf9Zqyw4pDmfV9OY=", Nonce="d36e316282959a9ed4c89851497a717f", Created="2003-12-15T14:43:07-0700"
这里是扩展文档。
这是我用来提出请求的代码:
// Create parameters NSDictionary* param = @{ @"action":@"helloWorld", @"query":@"{\"name\":\"john\"}" }; // Create Manager AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; // Add Headers [manager.requestSerializer setValue:@"WWSE profile=\"UsernameToken\"" forHTTPHeaderField:@"Authorization"]; [manager.requestSerializer setValue:[CocoaWSSE headerWithUsername:@"XXXXX" password:@"XXXXXXXXXXX"] forHTTPHeaderField:@"X-WSSE"]; // Perform Request [manager GET:@"http://www.volunteermatch.org/api/call" parameters:param success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) { NSLog(@"HTTP Request URL: %@", [operation.request URL]); NSLog(@"HTTP Response Status Code: %ld", [operation.response statusCode]); NSLog(@"HTTP Response Body: %@", responseObject); } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) { NSLog(@"HTTP Request failed: %@", error); }];
这是我得到的回应:
2015-11-17 16:53:36.476 XXXXXX[85280:5969811] HTTP Response Status Code: 200 2015-11-17 16:53:36.476 XXXXXX[85280:5969811] HTTP Response Body: (null)
我正在使用AFNetworking WS调用一个常用的方法。 用途:
呼叫WS:
NSDictionary* param = @{ @"action":@"helloWorld", @"query":@"{\"name\":\"john\"}" }; [self requestWithUrlString:@"URL" parmeters:paramDictionary success:^(NSDictionary *response) { //code For Success } failure:^(NSError *error) { // code for WS Responce failure }];
添加两种方法:这两种方法是常见的,你可以在整个项目中使用这些常用的方法使用NSObject类。 还添加/ /定义错误代码,如…
定义kDefaultErrorCode 12345
- (void)requestWithUrlString:(NSString *)stUrl parmeters:(NSDictionary *)parameters success:(void (^)(NSDictionary *response))success failure:(void (^)(NSError *error))failure { [self requestWithUrl:stUrl parmeters:parameters success:^(NSDictionary *response) { if([[response objectForKey:@"success"] boolValue]) { if(success) { success(response); } } else { NSError *error = [NSError errorWithDomain:@"Error" code:kDefaultErrorCode userInfo:@{NSLocalizedDescriptionKey:[response objectForKey:@"message"]}]; if(failure) { failure(error); } } } failure:^(NSError *error) { if(failure) { failure(error); } }];}
和/ /在下面的方法设置标题(如果需要,否则删除)
- (void)requestWithUrl:(NSString *)stUrl parmeters:(NSDictionary *)parameters success:(void (^)(NSDictionary *response))success failure:(void (^)(NSError *))failure { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]]; [manager.requestSerializer setValue:@"WWSE profile=\"UsernameToken\"" forHTTPHeaderField:@"Authorization"]; [manager GET:stUrl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { if([responseObject isKindOfClass:[NSDictionary class]]) { if(success) { success(responseObject); } } else { NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil]; if(success) { success(response); } } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); if(failure) { failure(error); } }];}
对于任何问题和更多详情请访问AFNetworking
UPDATE
———-迁移与AFNetworking 3.0 ———迁移你的AFN 2.0到3.0有一些小的变化请chaeck在这里AFN 3.0
尝试按照这里的指导来整合AFNetworking
最新版本2.0,不要忘记添加AFSecurityPolicy.h
, AFSecurityPolicy.m
文件。 这将支持https:
请求。
您不应该先序列化请求,然后添加新的标头。 头文件不会以这种方式(请求序列化)结束。 如果你反过来,你可能没问题。