传递参数和URL - AFNetworking iOS

时间:2014-01-16 10:41:53

标签: ios objective-c afnetworking

我有这样的网址。

https://<BASE_URL>/<TENANT_URL>/?query=where UserName = 'abc'&companyId=&page=1&pageSize=25&
filterResultByColumns=true.

我需要在此网址中传递参数。我将参数作为URL的字典传递

https://<BASE_URL>/<TENANT_URL>/
[dict setObject:@"abc" forKey:@"UserName"];
    [dict setObject:@"" forKey:@"companyId"];
    [dict setObject:[NSNumber numberWithInt:1] forKey:@"page"];
    [dict setObject:[NSNumber numberWithInt:25] forKey:@"pageSize"];
    [dict setObject:[NSNumber numberWithBool:true] forKey:@"filterResultByColumns"];
 //calling webservice
[manager GET:path parameters:dict 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);
                 }];

但是我收到了一个错误。在上面的url中传递参数的正确方法是什么。

错误:

 { status code: 401, headers {
    "Cache-Control" = private;
    "Content-Length" = 0;
    Date = "Thu, 16 Jan 2014 10:45:11 GMT";
    Server = "Microsoft-HTTPAPI/2.0";
    "Set-Cookie" = "FedAuth=; expires=Wed, 15-Jan-2014 10:45:11 GMT; path=/path/, FedAuth1=; expires=Wed, 15-Jan-2014 10:45:11 GMT; path=/path/";
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET";
} }, NSLocalizedDescription=Request failed: unauthorized (401)}

我认为这是我在使用webservice失败时遇到的默认错误。当我和邮递员一起尝试时,我收到了回复。我的输入请求可能不正确。我需要像上面指定的网址一样发送它。

由于

2 个答案:

答案 0 :(得分:0)

我认为,这与授权问题有关。试试这个。

[manager setAuthorizationHeaderWithUsername:SERVER_USER_NAME password:SERVER_PASSWORD];

答案 1 :(得分:0)

虽然以下代码不使用AfNetworking,但您可以使用它:

- (void) callWebService
    {
        NSString *urlAsString = [NSString stringWithFormat:@"Your URL here"];
        NSURL *url = [NSURL URLWithString:urlAsString];

        NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
        [urlRequest setTimeoutInterval:30.0f];
        [urlRequest setHTTPMethod:@"POST"];

        NSString *queryStr = [NSString stringWithFormat:@"parameter1=%@&parameter2=%@",parameter1,parameter2];
        NSData *bodyData = [queryStr dataUsingEncoding:NSUTF8StringEncoding];

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

        NSURLResponse *response = nil;
        NSError *error = nil;

        NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest
                                                     returningResponse:&response
                                                                 error:&error];


        if ([responseData length] > 0  &&
            error == nil) {

            NSDictionary *responseDict = [NSJSONSerialization
                                      JSONObjectWithData:responseData
                                      options:kNilOptions
                                      error:&error];
            NSLog(@"%@",responseDict)
        }
    }