Created
February 20, 2014 14:23
-
-
Save atifaziz/9114697 to your computer and use it in GitHub Desktop.
WebClient fixes
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 | |
open System.Net | |
open System.Net.Mime | |
open System.Text | |
let opt x = if x = null then None else Some(x) | |
type WebClient() = | |
inherit System.Net.WebClient() | |
let mutable mthd = null | |
member private this.Decode bytes = | |
let encoding = | |
this.ResponseHeaders.["Content-Type"] |> opt | |
|> Option.map (fun h -> ContentType(h)) | |
|> Option.map (fun ct -> ct.CharSet) | |
|> function | |
| None -> this.Encoding | |
| Some(charset) -> Encoding.GetEncoding(charset) | |
encoding.GetString(bytes) | |
member this.Method | |
with get() = mthd | |
and set value = mthd <- value | |
override this.GetWebRequest(address) = | |
let req = base.GetWebRequest(address) | |
if not (mthd = null) then | |
req.Method <- mthd | |
mthd <- null | |
req | |
member this.DownloadString(address : Uri) = | |
base.DownloadData(address) |> this.Decode | |
member this.UploadString(address : Uri, data : string) = | |
base.UploadData(address, base.Encoding.GetBytes(data)) |> this.Decode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment