我想使用的用戶和密碼錶單POST請求,訪問web應用程序在其下方,使用戶不必滿山遍野或按下登錄鍵來訪問他們的webapps。據我NSLogs,連接DidReceiveResponse,ConnectionDidReceiveData和ConnectionDidFinishLoading,但我看到在模擬器沒有結果; 我去web視圖,它顯示了空單,等待我輸入信息...不是我想要的。我想繞過這種形式。什麼是/可能會丟失或在這裏公然錯誤?:iOS HTTP Post:請求成功連接;什麼也沒有發生
viewDidLoad中: ... USR,私服等之前*消息體中產生,等等
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的貢獻,它接近一點,但還有其他要求得到t o表單下的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/
的鍵和值是什麼,我需要爲我的形式。問題出在窗體/網站/服務器上。不知道該從哪裏出發。任何關於從哪裏開始尋找的建議都會很棒。
現在我有同樣的問題!你有沒有想過解決你的問題的正確方法?謝謝你的時間! – wayway 2014-05-02 01:08:56
它不久前,但我認爲事實證明,Web應用程序是問題,正如sangony下面提到的。 – Morkrom 2014-05-02 19:05:37