iphone - 我的应用使用 URLRequest 登录两次

标签 iphone objective-c ios ipad

当我在我的应用程序中按下登录时,它在 API 中登录两次而不是一次..,这有问题但我找不到什么,因为它只执行此代码一次。

   NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];     
    NSHTTPURLResponse   * response;
                NSError             * error;
                NSMutableURLRequest * request;
                NSString            * params;
                NSString *urlAddress = [NSString stringWithFormat:@"%@/?action=request&api=json&module=ManagementModule&function=startSession&instance=0",[ConnectServer returnserverip]];
                NSLog(@"UPX %@",[ConnectServer returnserverip]);
                NSLog(@"IP %@",[ConnectServer returnclientip]);
                if([defaults boolForKey:@"enablePincode"]){
                    NSString *account = [defaults stringForKey:@"myAccount"];
                    NSString *username =[defaults stringForKey:@"myUsername"];
                    NSString *password = [defaults stringForKey:@"myPassword"];
                    NSString *clientip = [ConnectServer returnclientip];
                    NSString *clientname = [ConnectServer returnclientname];
                    params = [[[NSString alloc] initWithFormat:@"params=&auth[password]=%@&auth[mode]=%@&auth[account]=%@&auth[user]=%@&auth[rights]=%@&auth[user_ip]=%@&auth[client_name]=%@",password,@"password",account,username,@"user",clientip,clientname] autorelease];
                }
                else {
                    NSString *clientip = [ConnectServer returnclientip];
                    NSString *clientname = [ConnectServer returnclientname];
                    params = [[[NSString alloc] initWithFormat:@"params=&auth[password]=%@&auth[mode]=%@&auth[account]=%@&auth[user]=%@&auth[rights]=%@&auth[user_ip]=%@&auth[client_name]=%@",[myPassword text],@"password",[myAccount text],[myUsername text],@"user",clientip,clientname] autorelease];
                }
                request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlAddress] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60] autorelease];
                NSData *myRequestData = [params dataUsingEncoding:NSUTF8StringEncoding];
                [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[[NSURL URLWithString: urlAddress] host]];
                [request setHTTPMethod:@"POST"];
                [request setHTTPBody:myRequestData];
                [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Accept"];
                [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
                [request setValue:[NSString stringWithFormat:@"%d", [myRequestData length]] forHTTPHeaderField:@"Content-Length"];
                [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
     NSLog(@"RESPONSE HEADERS: \n%@", [response allHeaderFields]);
    request.URL = [NSURL URLWithString:urlAddress];
    error       = nil;
    response    = nil;
    NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSLog(@"The server saw:\n%@", [[[NSString alloc] initWithData:data encoding: NSASCIIStringEncoding] autorelease]);
    NSLog(@"Parameters: %@", params);
    NSLog(@"Actual sended parameters to the server: %@", myRequestData);
    NSString *Sresponse;
    Sresponse = [[[NSString alloc] initWithData:data encoding: NSASCIIStringEncoding] autorelease];

最佳答案

代码中有两个请求:

[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

往下五行

NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

提示:在这种情况下,使用 wireshark 或我最喜欢的 Charles 来解码 SSL 连接。

关于iphone - 我的应用使用 URLRequest 登录两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7371509/

相关文章:

iphone - 为什么我会从 Xcode 收到此警告? iOS代码看起来不错

objective-c - 如何避免 ASIHTTPRequest 的 404 错误?

iphone - 如何在另一个 View Controller 中调用 NSNotificationCenter 函数?

objective-c - Mac应用程序安全相关

ios - 如何确定搜索栏的位置

iphone - 使用 iOS Addressbook api 搜索交换联系人

iphone - UICollectionView 动画(插入/删除项目)

c++ - 为什么 QML 会删除我的 C++ ListView 模型?

objective-c - 条件导入头文件只有iOS5

javascript - 使用 JS 检测 iPhone SE 设备型号