1

我正在尝试对 User 和 Password 表单使用 POST 请求来访问其下方的 webapp,这样用户就不必填写字段或按登录键来访问他们的 webApps。根据我的 NSLogs,连接 DidReceiveResponse、ConnectionDidReceiveData 和 ConnectionDidFinishLoading,但我在模拟器中看不到任何结果;我转到 webview,它显示空表单,等待我输入我的信息......不是我想要的。我想绕过这个表格。这里有什么/可能遗漏或明显错误?

ViewDidLoad: ...usr、pW 等在 *messageBody 等之前生成...

NSString *messageBody = [NSString stringWithFormat:@"UsernameFieldName=%@&PasswordFieldName=%@", usr, pW];

NSData *postData = [messageBody dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

 NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:appURL];
[theRequest setHTTPMethod:@"Post"];

[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSLog(@"%@", messageBody);

[theRequest setHTTPBody:[messageBody dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];

[theRequest addValue: messageBody forHTTPHeaderField:@"Content-Length"];
NSLog(@"%@", theRequest);

[NSURLConnection connectionWithRequest: theRequest delegate:self];

连接方法:

- (void)connection:(NSURLConnection*)connection
didReceiveResponse:(NSURLResponse*)response
{
    NSLog(@"Connection DidReceiveResponse");

    webData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    NSLog(@"ConnectionDidReceiveData");

    webData = [NSMutableData dataWithData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
    NSLog(@"Connection Did Finish Loading");

    [HelpWebView loadData:webData 
                 MIMEType:@"text/html"
         textEncodingName:@"UTF-8"
                  baseURL:nil];
}

以下是 Sangony 的贡献,它更接近一点,但还需要其他一些东西才能访问表单下方的 webapp。

 - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
   //KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"key" accessGroup:nil];
   //NSString *usr = [keychainItem objectForKey:(__bridge id)(kSecAttrAccount)];
   //NSString *pW = [keychainItem objectForKey:(__bridge id)(kSecValueData)];

    if ([challenge previousFailureCount] == 0) {
    NSLog(@"received authentication challenge");
    NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"?username"
                                                                password:@"?password"
                                                             persistence:NSURLCredentialPersistenceForSession];
//        NSURLCredential *newCredential = [NSURLCredential credentialWithUser:[NSString stringWithFormat:@"%@", usr]
//                                                                    password:[NSString stringWithFormat:@"%@", pW]
//                                                                persistence:NSURLCredentialPersistenceForSession];
    NSLog(@"Credential created");
    [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
    NSLog(@"Responded to authentication challenge");
}
    else {
    NSLog(@"Authentication failure");
}}

更新 我在这里测试了上述方法: http ://www.posttestserver.com/

键和值是我的表单所需要的。问题在于表单/站点/服务器。不知道从这里去哪里。任何关于从哪里开始寻找的建议都会很棒。

4

1 回答 1

1

看看这段代码:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge previousFailureCount] == 0) {
NSLog(@"received authentication challenge");
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"?username"
                                                            password:@"?password"
                                                         persistence:NSURLCredentialPersistenceForSession];
NSLog(@"Credential created");
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
NSLog(@"Responded to authentication challenge");    
}
else {
NSLog(@"Authentication failure");
}
于 2013-05-10T22:14:10.640 回答