2

我正在尝试通过objective-c上的NSURLConnection通过POST方法发送我的用户名和密码来登录服务器,但我从我的NSLog中得到了这个

NS日志:


NSLog:
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",string);

它写入控制台的内容:



    
    
    404 Not Found
    
    

Not Found

Sorry!The page requested was not found.

为什么会发生这种情况?为什么我得到未找到页面而不是索引页面?我做错了什么?顺便说一句,我正在尝试连接 https 而不是 http 我不知道这是源代码:



    @interface ProjectViewController ()
    @property NSURLConnection * urlConnection;
    @property NSMutableData * responseData;

    @end

    @implementation ProjectViewController



    @synthesize responseData =_responseData;

    - (IBAction)LoginButton:(UIButton *)sender {

        NSMutableURLRequest *request = nil;
        request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://loginpage.com"]];

        NSString *post = [NSString stringWithFormat:@"sid=%@&PIN=%@", @"username", @"password"];
        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        [request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
        [request setTimeoutInterval: 15];
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:postData];

        _urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [_urlConnection start];



        //[self performSegueWithIdentifier:@"LoginComplete" sender:self];
    }




    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        [self.responseData appendData:data];
        NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",string);
    }

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        //Oops! handle failure here
    }
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    }

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
            //NSDictionary *headerFields = [(NSHTTPURLResponse*)response allHeaderFields]; //This would give you all the header fields;
            //NSLog(@"%@",headerFields);
        }
    }
    @end

我刚刚认识到的是,如果我将 POST 请求的一部分放入评论中,它会将页面源打印到控制台,但当然我不能将我的用户名和密码发送到服务器。我只是获取登录页面的源代码,但我想在登录后获取源代码。实际上我想通过使用这个会话重定向到另一个页面。

这部分:



    NSString *post = [NSString stringWithFormat:@"sid=%@&PIN=%@", @"username", @"password"];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    [request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
    [request setTimeoutInterval: 15];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];

4

1 回答 1

0

网站上使用的证书是自签名的吗?您是否尝试在 webview 中发出请求以查看它是否在那里工作?不在 nsurlrequest 中?

试试这个回复,也许它会给你一些提示。

于 2012-12-03T09:51:50.233 回答