我有执行查询的网址。
https://<BASE_URL>/<TENANT_URL>/?query=where UserName='abc'&companyId=&page=1&pageSize=25&filterResultByColumns=true
我在租客网址之后逃离剩下的部分,
NSString *requestUrl = [[NSString stringWithFormat:@"%@/?query=where UserName='%@'&companyId=&page=1&pageSize=25&filterResultByColumns=true",<TENANT_URL>,userCredential.userName]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
requestUrl = [NSString stringWithFormat:@"%@/%@",baseurl,requestUrl];
这是我的GET请求。
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPResponseSerializer *serializer = [AFHTTPResponseSerializer serializer];
serializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
manager.responseSerializer = serializer;
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSString *path = [NSString stringWithFormat:@"%@",URL];
[manager GET:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError* error = nil;
NSArray* json = [NSJSONSerialization
JSONObjectWithData:responseObject
options:kNilOptions
error:&error];
success(json);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error);
}];
但我总是收到400 Bad request错误。我认为问题在于“query = where ..”。但我不确定。我该如何解析URL。我在Chrome中使用“POSTMAN”进行了测试。它完美地运作。但是当我运行应用程序时,它会引发错误。
错误:
Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: bad request (400)" UserInfo=0xb7ac2b0 {NSErrorFailingURLKey=https://<BASE_URL>/<TENANT_URL>/?query=where UserName='abc'&companyId=&page=1&pageSize=25&filterResultByColumns=true&url=https%3A%2F%2F<BASE_URL>%2F%2F<TENANT_URL>%2F%3F?query=where%2DUserName%3D%27abc%27%26companyId%3D%26page%3D1%26pageSize%3D25%26filterResultByColumns%3Dtrue, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xb7e6910> { URL: https://<BASE_URL>/<TENANT_URL>/?query=where UserName='abc'&companyId=&page=1&pageSize=25&filterResultByColumns=true&url=https%3A%2F%2F<BASE_URL>%2F%2F<TENANT_URL>%2F%3F?query=where%2DUserName%3D%27abc%27%26companyId%3D%26page%3D1%26pageSize%3D25%26filterResultByColumns%3Dtrue } { status code: 400, headers {
"Cache-Control" = private;
"Content-Length" = 0;
"Content-Type" = "text/html";
Date = "Fri, 17 Jan 2014 05:29:56 GMT";
Server = "Microsoft-HTTPAPI/2.0";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} }, NSLocalizedDescription=Request failed: bad request (400)}
答案 0 :(得分:12)
看起来您正在构建的URL存在许多问题,以及您将参数传递(或未传递)到AFNetworking的方式。您不需要自己构建查询字符串,因为AFNetworking会为您执行此操作。正如我在上面的评论中所提到的,将query=where UserName='abc'
作为URL的一部分传递似乎是一个坏主意。不过,这里有一个简单示例,说明如果您的网址略有不同,您将如何调用AFNetworking的GET
方法:
// URL format: https://<BASE_URL>/<TENANT_URL>/?username=abc&companyId=&page=1&pageSize=25&filterResultByColumns=true
NSURL *baseURL = [NSURL URLWithScheme:@"https" host:BASE_URL path:TENANT_URL];
[manager GET:[baseURL absoluteString]
parameters:@{ @"username": @"abc",
@"companyId": @"example",
@"page": @1,
@"pageSize": @25,
@"filterResultByColumns": @YES }
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// handle success
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// handle failure
}];
如果您将参数传递给GET方法,AFNetworking将为您构建查询字符串。