我设置了ASP.NET REST API,并尝试通过iOS进行连接。同样的错误继续出现,我不知道连接在哪里被“破坏”。在NSLog中,链接显示为两个斜杠而不是NSString中的正斜杠。任何人都可以告诉我为什么这不连接?
完成阻止日志:
2015-02-08 23:19:00.795 b2bGatewayWebview[7266:548905] Loading... ["method","interface","parameters","UserAuthentication","http:\/\/website.com\/folder\/Handler1.ashx",{"userName":"DummyAcct","passsword":"DummyPwd"}]
2015-02-08 23:19:00.796 b2bGatewayWebview[7266:548905] Loading... {"method":"Getmembers","interface":"http:\/\/website.com\/folder\/Handler1.ashx","parameters":{"username":"DummyAcct"}}
2015-02-08 23:19:00.795 b2bGatewayWebview[7266:548861] View did load called.
2015-02-08 23:19:01.525 b2bGatewayWebview[7266:548937] done
2015-02-08 23:19:01.537 b2bGatewayWebview[7266:548937] RAW response = {
"Successful": false,
"ErrorMessage": "Internal server error"
}
RestAPI.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface RestAPI: UIViewController
@property (nonatomic, strong) NSURL *url;
@property (nonatomic, strong) NSString *interface;
-(void) CreateNewAccount:(NSString*)ausername password:(NSString*)apassword completionHandler:(void(^)(NSDictionary *dictionary, NSError *error))handler;
-(void) Getmembers:(NSString*)ausername completionHandler:(void(^)(NSDictionary *dictionary, NSError *error))handler;
-(void) UserAuthentication:(NSString*)auserName passsword:(NSString*)apasssword completionHandler:(void(^)(NSDictionary *dictionary, NSError *error))handler;
@end
RestAPI.m
#import "RestAPI.h"
@implementation RestAPI
@synthesize url;
@synthesize interface;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *initialLink = @"http://website.com/Handler1.ashx";
[self setInterface:initialLink];
NSString *username = @"DummyAcct";
NSString *password = @"DummyPswd";
//asynchronously call login method
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self UserAuthentication:username passsword:password completionHandler:^(NSDictionary *dictionary, NSError *error) {
NSLog(@"Login method called.");
}];
[self Getmembers:username completionHandler:^(NSDictionary *dictionary, NSError *error) {
nil;
}];
});
NSLog(@"View did load called.");
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (id) init
{
self = [super init];
if (self) {
[self setUrl: [NSURL URLWithString: @"http://website.com/Handler1.ashx"]];
[self setInterface:@"http://website.com/Handler1.ashx"];
}
return self;
}
- (void)load:(NSData*)data completionHandler:(void(^)(NSURLResponse *response, NSData *data, NSError *error))handler
{
NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Loading... %@", s);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue
completionHandler:handler];
}
- (void) Getmembers:(NSString*)ausername completionHandler:(void(^)(NSDictionary *dictionary, NSError *error))handler
{
NSMutableDictionary *d = [[NSMutableDictionary alloc] init];
[d setValue:interface forKey:@"interface"];
[d setValue:@"Getmembers" forKey:@"method"];
NSMutableDictionary *p = [[NSMutableDictionary alloc] init];
[p setValue:ausername forKey:@"username"];
[d setValue:p forKey:@"parameters"];
NSData *data = [NSJSONSerialization dataWithJSONObject:d options:0 error:nil];
[self load:data completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(@"done");
NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"RAW response = %@", s);
NSDictionary *d = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
handler(d, error);
}];
}
//login method
-(void) UserAuthentication:(NSString*)auserName passsword:(NSString*)apasssword completionHandler:(void(^)(NSDictionary *dictionary, NSError *error))handler
{
//NSMutableArray *a = [[NSMutableArray alloc] init];
NSMutableDictionary *d = [[NSMutableDictionary alloc] init];
[d setValue:interface forKey:@"interface"];
//[a setValue:interface forKey:@"interface"];
[d setValue:@"UserAuthentication" forKey:@"method"];
//[a setValue:@"UserAuthentication" forKey:@"method"];
NSMutableDictionary *p = [[NSMutableDictionary alloc] init];
[p setValue:auserName forKey:@"userName"];
[p setValue:apasssword forKey:@"passsword"];
[d setValue:p forKey:@"parameters"];
//[a setValue:p forKey:@"parameters"];
NSMutableArray *dictAllKeys=[NSMutableArray arrayWithArray:[d allKeys]];
NSMutableArray *dictAllValues=[NSMutableArray arrayWithArray:[d allValues]];
NSMutableArray *keysAndValues=[NSMutableArray arrayWithArray:[dictAllKeys arrayByAddingObjectsFromArray:dictAllValues]];
//NSJSONWritingOptions *writingOptions;
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:keysAndValues options:0 error:&error];
[self load:data completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(@"done");
NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"RAW response = %@", s);
//NSJSONReadingOptions *options;
NSError *error2;
NSDictionary *d = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error2];
handler(d, error);
}];
}
@end
答案 0 :(得分:1)
Rest API SQL设置不正确(Integrated Security为true)。同样在iOS方面,我不应该为用户身份验证传递数组,而是传递字典。