2014-05-02 45 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