Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Created February 20, 2014 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atifaziz/9114697 to your computer and use it in GitHub Desktop.
Save atifaziz/9114697 to your computer and use it in GitHub Desktop.
WebClient fixes
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