我想获得一个链接的.csv文件,该文件在发布到http表单后可用。URL(在代码中是kURL(是http://www2.htw-dresden.de/~rawa/cgi-bin/auf/raiplan_kal.php。我试着用得到结果
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((之后创建的文件。但我怎么能得到这个?
我希望你能在没有此表单中所需密码的情况下帮助我,我无法发布此。。
该按钮只是向http://www2.htw-dresden.de/~rawa/cgi-bin/plan/t21.csv
在浏览器中进行测试时,它也可以使用GET。如果您使用该URL,您将能够直接获得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);
}