Skip to content

Commit

Permalink
Fixed #2 Added support for encoding in get-webcontent
Browse files Browse the repository at this point in the history
  • Loading branch information
chaliy committed Aug 14, 2013
1 parent 2ce0b23 commit 5895b9a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
24 changes: 20 additions & 4 deletions PsUrl/PsUrl.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Param(
.Parameter Url
URL to download
.Parameter ToFile
Optional parameter to dowload stuff to the file.
Optional parameter to download stuff to the file.
.Example
Get-Url http://chaliy.name
Expand All @@ -48,7 +48,8 @@ function Get-WebContent {
Param(
[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, Position=0)]
[String]$Url,
[Management.Automation.PSCredential]$Credential
[Management.Automation.PSCredential]$Credential,
$Encoding
)
$client = (New-Object Net.WebClient)
if ($Credential){
Expand All @@ -57,6 +58,12 @@ Param(
$auth = "Basic " + [Convert]::ToBase64String([Text.Encoding]::Default.GetBytes($ntwCred.UserName + ":" + $ntwCred.Password))
$client.Headers.Add("Authorization", $auth)
}
if ($Encoding){
if ($Encoding -is [string]){
$Encoding = [Text.Encoding]::GetEncoding($Encoding)
}
$client.Encoding = $Encoding
}

try {
$client.DownloadString($Url)
Expand All @@ -70,15 +77,24 @@ Param(
.Description
.Parameter Url
URL to download
.Parameter ToFile
Optional parameter to dowload stuff to the file.
.Parameter Credential
Optional parameter to specified basic authorization credentials
.Parameter Encoding
Optional parameter to specified encoding of the content(e.g. Utf-8)
.Example
Get-WebContent http://chaliy.name
Description
-----------
Downloads content of the http://chaliy.name
.Example
Get-WebContent http://chaliy.name -Encoding Utf-8
Description
-----------
Downloads content of the http://chaliy.name with UTF-8 encoding
.Link
Send-WebContent
Expand Down
8 changes: 7 additions & 1 deletion Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import-module -name ($here + "\PsUrl\PsUrl.psm1") -force
write-host Should support downloading stuff
get-webcontent "http://example.com" | out-null


write-host Should support downloading encoded stuff
get-webcontent "http://example.com" -Encoding ([Text.Encoding]::Utf8) | out-null

write-host Should support downloading encoded stuff 2
get-webcontent "http://example.com" -Encoding utf-8 | out-null

write-host Should support 404
get-webcontent "http://us.blizzard.com/en-us/404/" -ErrorAction:SilentlyContinue

Expand All @@ -15,6 +22,5 @@ remove-item "$here\example.html"
write-host Should support posting stuff
send-webcontent "http://example.com" -Data @{"Foo" = "Bar"} | out-null


write-host Should support posting stuff with headers
send-webcontent "http://example.com" -Data @{"Foo" = "Bar"} -Headers @{"Something" = "Funny"} | out-null

0 comments on commit 5895b9a

Please sign in to comment.