-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurl.ps1
52 lines (44 loc) · 1.41 KB
/
curl.ps1
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
# Function CURL
# Used for requesting a web resource.
#
###########################################################
# PARAMETERS
# Url : Url of the request resource
# Method : Http verb used (GET,POST,PUT,DELETE,HEAD)
# Auth : If you need to use Http Authentication.
# -auth "your-username"
# The password is requested by the script latter
# so it cannot be read.
#
function Curl()
{
param
(
[string]$url = $null,
[string]$method = "GET",
[string]$auth = $null
);
trap [Exception] {
write $("ERREUR : " + $_.Exception.Message);
continue;
}
if($url -ne $null) {
# Create a new request
$webrequest = [system.Net.HttpWebRequest]::create($url);
$webrequest.set_Method($method);
$shouldAuth = ! ([boolean] [String]::IsNullOrEmpty($auth));
if($shouldAuth) {
$pass = Read-Host -assecurestring "Please enter your password"
$credentials = new-object System.Net.NetworkCredential ($auth, $pass);
$webrequest.set_Credentials($credentials);
}
# Get the response
$response = [system.Net.HttpWebResponse] $webrequest.GetResponse();
# Get the response stream and read it completely
$stream = $response.GetResponseStream();
$reader = new-object io.StreamReader($stream);
$resource = $reader.ReadToEnd();
# Write response
Write $resource;
}
}