iOS HTTP Post:请求成功连接;什么都没发生

时间:2013-05-10 20:47:01

标签: ios http post webview nsdata

我正在尝试对用户和密码表单使用POST请求,以访问其下方的Web应用程序,以便用户无需填写字段或按登录密钥即可访问其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/

键和值是我表单所需要的。问题出在表单/ site / server上。不知道从哪里开始。从这里开始寻找的任何建议都会很棒。

1 个答案:

答案 0 :(得分: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");
}