I want to get a linked .csv file, that is available after posting to a http form. The URL (in code it's kURL) is http://www2.htw-dresden.de/~rawa/cgi-bin/auf/raiplan_kal.php. I try to get the result with:
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);
}
}];
As result I get:
...
<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>
...
I need the file that is created after the javascript-function AbGehts(). But how can I get this?
I hope you can help me without the password that is needed in this form, I can't publish this..
That button just makes a POST request to http://www2.htw-dresden.de/~rawa/cgi-bin/plan/t21.csv
Testing in a browser, it also works with a GET. If you use that URL, you will be able to get the CSV data directly.
EDIT:
To get the filename dynamically a regex like this would work:
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);
}