/
PsUrl.psm1
234 lines (204 loc) · 6.76 KB
/
PsUrl.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
##
## Inspired by curl, adds some commands to work with web.
##
function Get-Url {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, Position=0)]
[String]$Url,
[String]$ToFile,
[Management.Automation.PSCredential]$Credential
)
Write-Verbose "Get-Url is considered obsolete. Please use Get-WebContent instead"
$client = (New-Object Net.WebClient)
if ($Credential){
$ntwCred = $Credential.GetNetworkCredential()
$client.Credentials = $ntwCred
$auth = "Basic " + [Convert]::ToBase64String([Text.Encoding]::Default.GetBytes($ntwCred.UserName + ":" + $ntwCred.Password))
$client.Headers.Add("Authorization", $auth)
}
if ($ToFile -ne ""){
$client.DownloadFile($Url, $ToFile)
} else {
$client.DownloadString($Url)
}
<#
.Synopsis
Downloads from url as a string.
.Description
.Parameter Url
URL to download
.Parameter ToFile
Optional parameter to dowload stuff to the file.
.Example
Get-Url http://chaliy.name
Description
-----------
Downloads content of the http://chaliy.name
#>
}
function Get-WebContent {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, Position=0)]
[String]$Url,
[Management.Automation.PSCredential]$Credential
)
$client = (New-Object Net.WebClient)
if ($Credential){
$ntwCred = $Credential.GetNetworkCredential()
$client.Credentials = $ntwCred
$auth = "Basic " + [Convert]::ToBase64String([Text.Encoding]::Default.GetBytes($ntwCred.UserName + ":" + $ntwCred.Password))
$client.Headers.Add("Authorization", $auth)
}
try {
$client.DownloadString($Url)
} catch [System.Net.WebException] {
throw "Request failed: ""$($_.Exception.Message)"""
}
<#
.Synopsis
Downloads content from given url as a string.
.Description
.Parameter Url
URL to download
.Parameter ToFile
Optional parameter to dowload stuff to the file.
.Example
Get-WebContent http://chaliy.name
Description
-----------
Downloads content of the http://chaliy.name
.Link
Send-WebContent
#>
}
function Write-Url {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, Position=0)]
[String]$Url,
[HashTable]$Data,
[String]$Content,
[TimeSpan]$Timeout = [TimeSpan]::FromMinutes(1),
[Management.Automation.PSCredential]$Credential,
[String]$ContentType
)
Write-Verbose "Write-Url is considered obsolete. Please use Send-WebContent instead"
if ($Content -ne ""){
Send-WebContent -Url:$Url -Content:$Content -Timeout:$Timeout -Credential:$Credential -ContentType:$ContentType
} else {
Send-WebContent -Url:$Url -Data:$Data -Timeout:$Timeout -Credential:$Credential -ContentType:$ContentType
}
<#
.Synopsis
POST values to URL
.Description
.Parameter Url
URL to POST
.Parameter Data
Hashtable of the data to post.
.Parameter Timeout
Optional timeout value, by default timeout is 1 minute.
.Parameter ContentType
Adds Content-Type header to request
.Example
Write-Url http://chaliy.name -Data @{"Foo" = "Bar" }
Description
-----------
POST's to the http://chaliy.name as application/x-www-form-urlencoded
#>
}
function Send-WebContent {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, Position=0)]
[String]$Url,
[Parameter(ParameterSetName='Data')]
[HashTable]$Data,
[Parameter(ParameterSetName='Content')]
[String]$Content,
[TimeSpan]$Timeout = [TimeSpan]::FromMinutes(1),
[Management.Automation.PSCredential]$Credential,
[String]$ContentType
)
try{
$req = [Net.WebRequest]::Create($Url)
$req.Method = "POST"
$req.Timeout = $Timeout.TotalMilliseconds
if ($Credential){
$ntwCred = $Credential.GetNetworkCredential()
$auth = "Basic " + [Convert]::ToBase64String([Text.Encoding]::Default.GetBytes($ntwCred.UserName + ":" + $ntwCred.Password))
$req.Headers.Add("Authorization", $auth)
$req.Credentials = $ntwCred
$req.PreAuthenticate = $true
}
if ($ContentType -ne ""){
$req.ContentType = $ContentType
}
switch($PSCmdlet.ParameterSetName) {
Content {
$reqStream = $req.GetRequestStream()
$reqBody = [Text.Encoding]::Default.GetBytes($Content)
$reqStream.Write($reqBody, 0, $reqBody.Length)
}
Data {
Add-Type -AssemblyName System.Web
$formData = [Web.HttpUtility]::ParseQueryString("")
foreach($key in $Data.Keys){
$formData.Add($key, $Data[$key])
}
$reqBody = [Text.Encoding]::Default.GetBytes($formData.ToString())
$req.ContentType = "application/x-www-form-urlencoded"
$reqStream = $req.GetRequestStream()
$reqStream.Write($reqBody, 0, $reqBody.Length)
}
}
$reqStream.Close()
$Method = $req.Method
Write-Verbose "Execute $Method request"
foreach($header in $req.Headers.Keys){
Write-Verbose ("$header : " + $req.Headers[$header])
}
$resp = $req.GetResponse()
$respStream = $resp.GetResponseStream()
$respReader = (New-Object IO.StreamReader($respStream))
$respReader.ReadToEnd()
}
catch [Net.WebException]{
if ($_.Exception -ne $null -and $_.Exception.Response -ne $null) {
$errorResult = $_.Exception.Response.GetResponseStream()
$errorText = (New-Object IO.StreamReader($errorResult)).ReadToEnd()
Write-Error "The remote server response: $errorText"
}
throw $_
}
<#
.Synopsis
POST values to URL
.Description
.Parameter Url
URL to POST
.Parameter Data
Hashtable of the data to post.
.Parameter Timeout
Optional timeout value, by default timeout is 1 minute.
.Parameter ContentType
Adds Content-Type header to request
.Example
Send-WebContent http://chaliy.name -Data @{"Foo" = "Bar" }
Description
-----------
POST's to the http://chaliy.name as application/x-www-form-urlencoded
.Link
Get-WebContent
#>
}
Export-ModuleMember Get-Url #Obsolete
Export-ModuleMember Write-Url #Obsolete
Set-Alias gwc Get-WebContent
Set-Alias swc Send-WebContent
Export-ModuleMember Get-WebContent
Export-ModuleMember Send-WebContent
Export-ModuleMember -Alias gwc
Export-ModuleMember -Alias swc