我试图使用用户和密码表单的POST请求,访问它下面的web应用程序,以便用户不必填写字段或按登录键访问他们的web应用程序。根据我的nlog,连接didreceiverresponse, ConnectionDidReceiveData和ConnectionDidFinishLoading,但我在模拟器中看不到结果;我去webview,它显示空表单,等待我输入我的信息…不是我想要的。我想绕过这个表单。这里有什么遗漏或明显的错误?:
ViewDidLoad: …userr, 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的贡献,它更接近了一点,但是要进入表单下面的web应用程序还需要一些其他的东西。
- (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/
键和值是我的表单所需要的。问题出在表单/网站/服务器上。不知道该往哪走。任何关于从哪里开始寻找的建议将是非常棒的。
看一下这段代码:
- (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");
}