gpt4 book ai didi

objective-c - 对 WCF REST 服务的 AFNetworking JSON 请求

转载 作者:可可西里 更新时间:2023-11-01 05:46:04 25 4
gpt4 key购买 nike

我正在为我的 REST 服务 (WCF) 使用 AFNetworking。这是代码:

  NSDictionary *userName = [NSDictionary dictionaryWithObject:@"user" forKey:@"UserNameOrEmail"];
NSDictionary *pass = [NSDictionary dictionaryWithObject:@"123" forKey:@"Password"];
NSArray *credentials = [NSArray arrayWithObjects:userName,pass,nil];
NSDictionary *params = [NSDictionary dictionaryWithObject:credentials forKey:@"request"];


AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:
[NSURL URLWithString:@"http://server"]];

[client postPath:@"/ProfileWebService.svc/login" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *text = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Response: %@", text);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", [error localizedDescription]);
}];

但我收到 400 HTTP 错误。

使用 HTML 和 AJAX 看起来像:

        $(document).ready(function () {

$("#login_call").click(function () {
$.ajax({
type: 'POST',
url: 'http://server/ProfileWebService.svc/login',
contentType : 'application/json',
dataType: 'json',
data: JSON.stringify({request: {UserNameOrEmail: $('#login_username').val(), Password: $('#login_password').val()}}),
success: function (data) {
$('#login_result').val('Code: ' + data.LoginResult.Code + '\nFault String: ' + data.LoginResult.FaultString);
}
});
});

});

并且工作正常。

参数有什么问题。

最佳答案

在 Stackoverflow 上学习和搜索更详细的 AFNetworking 文档给了我一个解决方案:

1.请求的参数应该是这样的:

  NSDictionary *params =
[NSDictionary dictionaryWithObjectsAndKeys:
[NSDictionary dictionaryWithObjectsAndKeys:
username.text, @"UserNameOrEmail",
password.text, @"Password",
nil],
@"request",
nil];

2.创建AFHTTPClient

  AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:
[NSURL URLWithString:@"http://server"]];

3.当我发送 JSON 作为参数时,我必须添加:

  client.parameterEncoding = AFJSONParameterEncoding;

在我这样做之后,我摆脱了 404 错误,但我无法用 JSON 响应做任何事情,我不知道为什么。但解决方案是:

4.创建请求:

  NSMutableURLRequest *request = 
[client requestWithMethod:@"POST" path:@"/ProfileWebService.svc/login" parameters:params];

5.创建一个操作:

  AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
NSLog(@"JSON: %@", [JSON valueForKeyPath:@"LoginResult"]);
NSLog(@"Code: %@", [[[JSON valueForKeyPath:@"LoginResult"] valueForKeyPath:@"Code"] stringValue]);
NSLog(@"FaultString: %@", [[JSON valueForKeyPath:@"LoginResult"] valueForKeyPath:@"FaultString"]);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
{
NSLog(@"error opening connection");
}];

6.开始操作:

  [operation start];

希望这篇文章对一些 AFNetworking 初学者有所帮助。

关于objective-c - 对 WCF REST 服务的 AFNetworking JSON 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13274689/

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