Created
October 23, 2016 08:08
-
-
Save fairjm/8509cb19a4a729d96b51f1f57c995013 to your computer and use it in GitHub Desktop.
F# get attachment filename by httpclient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System.Net.Http | |
open System.Net.Mime | |
open System.Text | |
///get fileName by ContentDisposition or random file name if null | |
let getFileName (msg: HttpResponseMessage) = | |
let value = Option.ofObj msg.Content.Headers.ContentDisposition | |
let result = | |
value | |
|> Option.map (fun e -> e.FileName) | |
|> Option.map (fun e -> Encoding.UTF8.GetString(Encoding.GetEncoding("ISO-8859-1").GetBytes(e))) | |
match result with | |
| Some(v) -> v | |
| None -> System.IO.Path.GetRandomFileName() | |
let fileName = using(new HttpClient()) (fun e -> | |
let msg = e.GetAsync("https://sukebei.nyaa.se/?page=download&tid=2154757").Result | |
Utils.getFileName(msg) | |
) | |
printfn "%A" fileName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment