2010-10-14 73 views
1

我一直在使用openFrameworks,在論壇上發佈的問題上: www.openframeworks.cc/forum/viewtopic.php?f = 8 & t = 4765在通過HTTP登錄後在iPhone上保存cookie Cookie混合C++和Objective C

本質上,我使用了一組文件,ofxHttpUtils,它使用poco發佈到Web表單。 我用這個例子的代碼是: github.com/arturoc/ofxHttpUtils/blob/gh-pages/example/src/testApp.cpp

我想發佈到登錄頁面,輸入用戶名和密碼,然後我的目標是通過iPhone應用程序刮掉文字。這就是目標。

我遇到的問題是cookies。 ofxHttpUtils插件沒有任何記住POST的cookie的方法,所以我得到的響應只是登錄頁面。我已經尋找方法,試圖捕捉到的cookie,而且似乎是在目標C的東西在這裏,從另一個帖子堆棧溢出:

NSHTTPURLResponse * response; 
NSError    * error; 
NSMutableURLRequest * request; 
request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://temp/gomh/authenticate.py?setCookie=1"] 
             cachePolicy:NSURLRequestReloadIgnoringCacheData 
            timeoutInterval:60] autorelease]; 

[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];  
NSLog(@"RESPONSE HEADERS: \n%@", [response allHeaderFields]); 

// If you want to get all of the cookies: 
NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://temp"]]; 
NSLog(@"How many Cookies: %d", all.count); 
// Store the cookies: 
// NSHTTPCookieStorage is a Singleton. 
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:all forURL:[NSURL URLWithString:@"http://temp"] mainDocumentURL:nil]; 

// Now we can print all of the cookies we have: 
for (NSHTTPCookie *cookie in all) 
    NSLog(@"Name: %@ : Value: %@, Expires: %@", cookie.name, cookie.value, cookie.expiresDate); 


// Now lets go back the other way. We want the server to know we have some cookies available: 
// this availableCookies array is going to be the same as the 'all' array above. We could 
// have just used the 'all' array, but this shows you how to get the cookies back from the singleton. 
NSArray * availableCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"http://temp"]]; 
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:availableCookies]; 

// we are just recycling the original request 
[request setAllHTTPHeaderFields:headers]; 

request.URL = [NSURL URLWithString:@"http://temp/gomh/authenticate.py"]; 
error  = nil; 
response = nil; 

NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
NSLog(@"The server saw:\n%@", [[[NSString alloc] initWithData:data encoding: NSASCIIStringEncoding] autorelease]); 

我不知道如何/在哪裏實現這一點,使我可以與我的ofxHttpUtils代碼進行整合,以便記住cookie並在調用密碼保護網站時提供服務。誰能幫忙?我知道這個請求是有點不確定的...我希望你能看到我想要做什麼...

回答

0

由於你已經使用OF,它可能會更簡單,堅持下去。您將不得不擴展xHttpUtils來處理cookie,或者通過更智能的方式來處理cookie,以便它可以智能地處理cookie,或者在讓您根據需要抓取和設置cookie時將它們留下笨蛋。 Poco :: Net,xHttpUtils基於哪個版本,對Cookie沒有問題 - 它包含的功能如HTTPResponse::getCookies()

最直接的方法就是啞巴一個:

  • 添加ofxHttpForm::setCookies()這樣你就可以通過餅乾到模塊和getCookies()因此模塊可以訪問它們
  • 修改ofxHttpUtils::doPostForm()ofxHttpForm和集拉餅乾他們在Poco HTTPRequest
  • 修改ofxHttpRequest構造函數從Poco HTTPResponse中提取cookie併爲您的代碼提供一種方法來獲取它們

然後,您的代碼將獲取登錄POST後發回的cookie,並將它們設置爲所有將來的請求。

+0

嗨傑里米感謝您的答案。這是我的想法 - 但我不知道該怎麼去做。我認爲它會涉及在你所說的ofxHttpUtils中寫一個函數。但我不確定我有這樣的知識!但我確實認爲混合上述兩種代碼可能不是正確的做法。你有什麼指示讓我開始?再次感謝 – sacculi 2010-10-14 21:32:06

+0

@sacculi我編輯了我的原始答案,提供了一個簡單方法的概要,它基本上只是通過ofxHttpUtils層來回傳遞cookie。 – 2010-10-15 00:22:18

+0

謝謝傑里米。這些步驟非常有用,非常感謝。我嘗試過各種實施方式,但都很努力。就像將cookie傳遞給新函數(步驟1)一樣,我想我需要從postForm函數傳入HTTPResponse,但是我無法做到。在我看來,這裏存在着一些概念上的差距。我也很感興趣的是http://stackoverflow.com/questions/1499086/poco-c-net-ssl-how-to-post-https-request,它似乎與我一直在使用的ofxHttpUtils有相同的代碼。我想知道如何使用,如: – sacculi 2010-10-15 12:56:17