2014-05-02 48 views
0

我想獲得鏈接的.csv文件,該文件在發佈到http表單後可用。 URL(代碼爲kURL)爲http://www2.htw-dresden.de/~rawa/cgi-bin/auf/raiplan_kal.php。 我試圖用得到的結果:連續執行HTTP帖子Obj C

NSString *myRequestString = [NSString stringWithFormat:@"unix=%@&pressme=%@",_password,@"S+T+A+R+T"]; 

    NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:kURL 
                  cachePolicy:NSURLRequestUseProtocolCachePolicy 
                 timeoutInterval:10]; 

    [request setHTTPMethod: @"POST"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 
    [request setHTTPBody: myRequestData]; 
    [request setValue:@"4" forHTTPHeaderField:@"w1"]; 


    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
     if (connectionError) NSLog(@"ERROR: %@", [connectionError localizedDescription]); 
     else { 
      NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
     NSLog(@"%@", string); 
     } 
    }]; 

至於結果,我得到:

... 
<form name=Testform method=post action=../plan/t21.csv><INPUT TYPE=hidden NAME=w1 value=4></form><script type="text/javascript"> 
function AbGehts() {document.Testform.submit();} 
window.setTimeout("AbGehts()",1); 
</script> 
... 

我需要的是JavaScript功能AbGehts後創建的文件()。但我怎麼能得到這個?

我希望你能幫助我不認爲需要這種形式的密碼,我不能發佈這個..

+0

所以你沒有合適的Web服務調用?你不能添加一個?你不能編碼JavaScript的任何重定向到應用程序? – Wain

+0

我不知道我明白你的意思。我所稱的網絡服務是在我的大學運行的一項服務。我希望在第一次請求後隱藏表單後面的數據。 – Ben

回答

1

那個按鈕只是讓一個POST請求http://www2.htw-dresden.de/~rawa/cgi-bin/plan/t21.csv

測試在瀏覽器中,它也適用於GET。如果您使用該網址,則可以直接獲取CSV數據。

編輯:

要獲取文件名動態像這樣的正則表達式將工作:

NSString *html = @"<form name=Testform method=post action=../plan/t21.csv>"; 
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<form name=Testform method=post action=\\.\\.\\/plan\\/([^>]*)>" options:0 error:nil]; 
NSTextCheckingResult *result = [regex firstMatchInString:html options:0 range:NSMakeRange(0, html.length)]; 
if(result) { 
    NSRange range = [result rangeAtIndex:1]; 
    NSString *filename = [html substringWithRange:range]; 
    NSLog(@"filename: %@", filename); 
} 
+0

是的,對不起,我編輯了代碼。第一個是一個測試,如果這個工程。但是死亡文件並不是每次都被命名爲t21.csv。它也可能是t20.csv或其他東西。 – Ben

+0

在這種情況下,您可以使用正則表達式來獲取文件名並動態構建URL。編輯答案... –

+0

謝謝!這並不是我期待的,但它完美地工作,謝謝! – Ben