gpt4 book ai didi

iphone - AFNetworking POST 到 REST Web 服务

转载 作者:行者123 更新时间:2023-12-03 18:34:00 29 4
gpt4 key购买 nike

简单的背景故事,我们以前的开发人员使用 ASIHTTPRequest 发出 POST 请求并从我们的 Web 服务检索数据。由于未知的原因,我们应用程序的这一部分停止工作。似乎有足够的时间来证明 future 并使用 AFNetworking。 REST Web 服务在 CakePHP 框架上运行。

简而言之,我没有收到使用 AFNetworking 的请求响应字符串。

我知道网络服务有效,因为我能够使用curl成功发布数据并接收正确的响应: curl -d“数据[模型][字段0]=字段0值&数据[模型][字段1]=字段1值”https://example.com/api/class/function.plist

根据之前的开发人员的指示,我想出了以下内容。

#import "AFHTTPRequestOperation.h"    



- (IBAction)loginButtonPressed {

NSURL *url = [NSURL URLWithString:@"https://example.com/api/class/function.plist"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[request setValue:[usernameTextField text] forHTTPHeaderField:@"data[User][email]"];

[request setValue:[passwordTextField text] forHTTPHeaderField:@"data[User][password]"];

AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@"operation hasAcceptableStatusCode: %d", [operation.response statusCode]);

NSLog(@"response string: %@ ", operation.responseString);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"error: %@", operation.responseString);

}];

[operation start];

}

输出:操作 hasAcceptableStatusCode: 200响应字符串:一个空白的 plist 文件

尝试的解决方案1: AFNetworking Post Request所提出的解决方案使用 AFHTTPRequestOperation 的一个名为 operationWithRequest 的函数。但是,当我尝试使用上述解决方案时,我收到警告“未找到类方法 '+operationWithRequest:completion:'(返回类型默认为 'id'”

尝试解决方案2:NSURLConnection。输出:我可以打印成功日志消息,但不能打印响应字符串。*更新 - 返回空白 plist。

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *httpBodyData = @"data[User][email]=username@example.com&data[User][password]=awesomepassword";
[httpBodyData dataUsingEncoding:NSUTF8StringEncoding];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[NSData dataWithContentsOfFile:httpBodyData]];
NSHTTPURLResponse __autoreleasing *response;
NSError __autoreleasing *error;
[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];

// *update - returns blank plist
NSData *responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"responseData %@",str);

if (error == nil && response.statusCode == 200) {
// Process response
NSLog(@"success");//returns success code of 200 but blank
NSLog(@"resp %@", response );
} else {
// Process error
NSLog(@"error");
}

最佳答案

这些是最终满足我对网络服务请求的基本行(剔除我为自己使用而制定的条件)。感谢@8vius 和@mattt 的建议!

- (IBAction)loginButtonPressed {        
NSURL *baseURL = [NSURL URLWithString:@"https://www.example.com/api/class"];

//build normal NSMutableURLRequest objects
//make sure to setHTTPMethod to "POST".
//from https://github.com/AFNetworking/AFNetworking
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[httpClient defaultValueForHeader:@"Accept"];

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
[usernameTextField text], kUsernameField,
[passwordTextField text], kPasswordField,
nil];

NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
path:@"https://www.example.com/api/class/function" parameters:params];

//Add your request object to an AFHTTPRequestOperation
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc]
initWithRequest:request] autorelease];

//"Why don't I get JSON / XML / Property List in my HTTP client callbacks?"
//see: https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ
//mattt's suggestion http://stackoverflow.com/a/9931815/1004227 -
//-still didn't prevent me from receiving plist data
//[httpClient registerHTTPOperationClass:
// [AFPropertyListParameterEncoding class]];

[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

[operation setCompletionBlockWithSuccess:
^(AFHTTPRequestOperation *operation,
id responseObject) {
NSString *response = [operation responseString];
NSLog(@"response: [%@]",response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", [operation error]);
}];

//call start on your request operation
[operation start];
[httpClient release];
}

关于iphone - AFNetworking POST 到 REST Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9927945/

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