gpt4 book ai didi

ios - 志愿者匹配 API Objective-C

转载 作者:可可西里 更新时间:2023-11-01 03:37:42 25 4
gpt4 key购买 nike

我正在使用 AFNetworking 在 VolunteerMatch API 上执行网络请求。当我执行请求时,我收到一个 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"

Here是扩展文档。

这是我用来发出请求的代码:

 // 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);
}
}];}

对于任何问题和更多详细信息 please visit AFNetworking


更新
---------- 使用 AFNetworking 3.0 迁移 ----------请将您的 AFN-2.0 迁移到 3.0,并做一些小改动 chaeck here AFN 3.0

关于ios - 志愿者匹配 API Objective-C ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33767908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com