使用powershell制作http请求界面工具

说明

  1. 把下面的代码放到后缀名为.ps1的文件中
  2. 右键打开,打开方式为Windows PowerShell
  3. 打开之后界面如下图:使用powershell制作http请求界面工具
  4. 参数格式说明:
    • header: 请求头,以换行符分割每一条
    • cookie: 以;分割,如a=aaa;b=bbb
    • content: 请求内容,如果contentType选的为json,则为json格式,如{"a":"aaa","b":"bbb"}, 如果contentType选的为x-www-form-urlencoded,则为form结构,如a=aaa&b=bbb
  5. 请求示例如下图:使用powershell制作http请求界面工具

完整代码

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
# 使用powershell运行程序,并隐藏窗口
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'
$consolePtr = [Console.Window]::GetConsoleWindow()
[Console.Window]::ShowWindow($consolePtr, 0)

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Web

# 发送请求
function ajax ($options) {
$agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36 Edg/85.0.564.44'
$url = $options[0]
$type = $options[1]
$contentType = $options[2]
$cookie = $options[3]
$body = $options[4]
$headers = $options[5]
function getCookie ($cookie) {
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
if($cookie -eq ""){
return $session
}
foreach ($a in $cookie.split(";")){
$cookie = New-Object System.Net.Cookie
$arr = $a.split("=")
$cookie.Name = $arr[0].Replace(" ", "")
$cookie.Value = $arr[1]
$cookie.Domain = ([uri]$url).Host
$session.Cookies.Add($cookie)
}
return $session
}
$session = getCookie($cookie)

if($type -eq 'GET'){
$res = Invoke-WebRequest -Uri $url -Method 'GET' -WebSession $session -UserAgent $agent -Headers $headers
}else{
$res = Invoke-WebRequest -Uri $url -ContentType $contentType -Method $type -Body $body -WebSession $session -UserAgent $agent -Headers $headers
}
return $res
}
# 换行形式的格式转哈希表
function convertHashTable ($json) {
$k = @{}
if($json -ne ""){
$json = $json.split("`n")
for($i=0;$i -lt $json.count;$i++){
$arr = $json[$i].split(":")
$val = $json[$i].ToString().Replace($arr[0]+":", "")
$k.Add($arr[0], $val)
}
}
return $k
}
# 格式化json缩进
function Format-Json([Parameter(Mandatory, ValueFromPipeline)][String] $json) {
$indent = 0;
($json -Split "`n" | ForEach-Object {
if ($_ -match '[\}\]]\s*,?\s*$') {
$indent--
}
$line = (' ' * $indent) + $($_.TrimStart() -replace '": (["{[])', '": $1' -replace ': ', ': ')
if ($_ -match '[\{\[]\s*$') {
$indent++
}
$line
}) -Join "`n"
}

function drawLabel ($options) {
$label = New-Object System.Windows.Forms.Label
$label.Location = $options[0]
$label.Size = $options[1]
$label.Text = $options[2]
return $label
}
function drawRichText ($options) {
$RichTextBox = New-Object System.Windows.Forms.RichTextBox
$RichTextBox.Location = $options[0]
$RichTextBox.Size = $options[1]
return $RichTextBox
}
function drawRadioButton ($options) {
$RadioButton = New-Object System.Windows.Forms.RadioButton
$RadioButton.Location = $options[0]
$RadioButton.size = $options[1]
$RadioButton.Text = $options[2]
$RadioButton.Checked = $options[3]
$RadioButton.Name = $options[4]
return $RadioButton
}
function drawGroupBox ($options) {
$GroupBox = New-Object System.Windows.Forms.GroupBox
$GroupBox.Location = $options[0]
$GroupBox.size = $options[1]
$GroupBox.text = $options[2]
return $GroupBox
}

$form = New-Object System.Windows.Forms.Form
$form.Text = 'http request'
$form.Size = '1000,600'
$form.StartPosition = 'CenterScreen'

$req = drawGroupBox('30,15', '450,500', "request")

# url
$urlLabel = drawLabel("15,30", "80,30", 'url')
$urlBox = New-Object System.Windows.Forms.TextBox
$urlBox.Location = "120,30"
$urlBox.Size = "300,30"

# methods
$methodLabel = drawLabel("15,80", "80,30", 'methods')

$groupMethods = drawGroupBox('120,60', '300,40', "")
$methodRadio1 = drawRadioButton('15,10', '50,20', "get", $true, "methods")
$methodRadio2 = drawRadioButton('70,10', '50,20', "post", $false, "methods")
$methodRadio3 = drawRadioButton('135,10', '50,20', "put", $false, "methods")
$methodRadio4 = drawRadioButton('200,10', '70,20', "delete", $false, "methods")
$groupMethods.Controls.AddRange(@($methodRadio1, $methodRadio2, $methodRadio3, $methodRadio4))

# contentType
$contentTypeLabel = drawLabel("15,120", "80,30", 'contentType')

$groupContentType = drawGroupBox('120,110', '300,40', "")
$contentTypeRadio2 = drawRadioButton('15,10', '50,20', "json", $true, "contentType")
$contentTypeRadio1 = drawRadioButton('70,10', '150,20', "x-www-form-urlencoded", $false, "contentType")
$groupContentType.Controls.AddRange(@($contentTypeRadio1, $contentTypeRadio2))

# header
$headerLabel = drawLabel("15,160", "80,30", 'header')
$headerBox = drawRichText("120,160", "300,60")

# cookie
$cookieLabel = drawLabel("15,240", "80,30", 'cookie')
$cookieBox = drawRichText("120,240", "300,60")

# content
$contentLabel = drawLabel("15,320", "80,30", 'content')
$contentBox = drawRichText("120,320", "300,120")

# button
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = "120,460"
$okButton.Size = "75,23"
$okButton.Text = 'submit'
$req.Controls.AddRange(@($urlLabel,$urlBox,$methodLabel, $groupMethods, $contentTypeLabel, $groupContentType ,$cookieLabel,$cookieBox,$contentLabel,$contentBox,$okButton, $headerLabel,$headerBox))
$form.Controls.Add($req)

$okButton.Add_Click({
try {
$url = $urlBox.Text
$headers = $headerBox.Text
$cookie = $cookieBox.Text
$body = $contentBox.Text
if($methodRadio1.Checked -eq $true){$type = "GET"}
elseif($methodRadio2.Checked -eq $true){$type = "POST"}
elseif($methodRadio3.Checked -eq $true){$type = "PUT"}
elseif($methodRadio4.Checked -eq $true){$type = "DELETE"}
if($contentTypeRadio2.Checked -eq $true){$contentType = "application/json; charset=UTF-8"}
else{$contentType = "application/x-www-form-urlencoded; charset=UTF-8"}
$headers = convertHashTable($headers)

$res = ajax($url, $type, $contentType, $cookie, $body, $headers)
$resHBox.text = ConvertTo-Json($res.Headers)
}
catch {
(New-Object -ComObject WScript.Shell).popup($PSItem.ToString(),0,"msg",0 + 48)
return
}
$resContentBox.text = ""
start-sleep -Milliseconds 500
try {
$resContentBox.text = ConvertFrom-Json($res.Content) | ConvertTo-Json -Depth 10 | Format-Json
}
catch {
$resContentBox.text = $res.Content
}
})

# response
$res = drawGroupBox('500,15', '450,500', "response")

# header
$resHLabel = drawLabel("15,30", "80,30", 'header')
$resHBox = drawRichText("120,30", "300,120")

# data
$resContentLabel = drawLabel("15,180", "80,30", 'data')
$resContentBox = drawRichText("120,180", "300,300")

$res.Controls.AddRange(@($resHLabel,$resHBox,$resContentLabel,$resContentBox))
$form.Controls.Add($res)

# $form.Topmost = $true
# $form.Add_Shown({$textBox.Select()})
$form.ShowDialog()
0%