| 1 |
#Region "Microsoft.VisualBasic::8c08a1c92ed7eb17a232b54032172d09, Microsoft.VisualBasic.Core\Extensions\WebServices\WebServiceUtils.vb"
|
| 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 |
#End Region
|
| 51 |
|
| 52 |
Imports System.Collections.Specialized
|
| 53 |
Imports System.IO
|
| 54 |
Imports System.Net
|
| 55 |
Imports System.Net.Security
|
| 56 |
Imports System.Runtime.CompilerServices
|
| 57 |
Imports System.Security.Cryptography.X509Certificates
|
| 58 |
Imports System.Text
|
| 59 |
Imports System.Text.RegularExpressions
|
| 60 |
Imports System.Web
|
| 61 |
Imports Microsoft.VisualBasic.CommandLine.Reflection
|
| 62 |
Imports Microsoft.VisualBasic.Language
|
| 63 |
Imports Microsoft.VisualBasic.Language.Default
|
| 64 |
Imports Microsoft.VisualBasic.Linq.Extensions
|
| 65 |
Imports Microsoft.VisualBasic.Net
|
| 66 |
Imports Microsoft.VisualBasic.Net.Http
|
| 67 |
Imports Microsoft.VisualBasic.Net.Tcp
|
| 68 |
Imports Microsoft.VisualBasic.Scripting.MetaData
|
| 69 |
Imports Microsoft.VisualBasic.Text
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
<Package("Utils.WebServices",
|
| 76 |
Description:="The extension module for web services programming in your scripting.",
|
| 77 |
Category:=APICategories.UtilityTools,
|
| 78 |
Publisher:="<a href=""mailto://xie.guigang@gmail.com"">xie.guigang@gmail.com</a>")>
|
| 79 |
Public Module WebServiceUtils
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
Public ReadOnly Property Protocols As String() = {"http://", "https://", "ftp://", "sftp://"}
|
| 86 |
|
| 87 |
Public Const URLPattern$ = "http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\\?\.\:\;\'\,]*)?"
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 97 |
<Extension> Public Function isURL(url As String) As Boolean
|
| 98 |
Return url.InStrAny(Protocols) > -1
|
| 99 |
End Function
|
| 100 |
|
| 101 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 102 |
<Extension> Public Function IsURLPattern(str As String) As Boolean
|
| 103 |
Return str.isURL OrElse str.IsPattern(URLPattern)
|
| 104 |
End Function
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
<ExportAPI("Build.Reqparm",
|
| 112 |
Info:="Build the request parameters for the HTTP POST")>
|
| 113 |
<Extension> Public Function BuildReqparm(dict As Dictionary(Of String, String)) As NameValueCollection
|
| 114 |
Dim reqparm As New NameValueCollection
|
| 115 |
|
| 116 |
For Each Value As KeyValuePair(Of String, String) In dict
|
| 117 |
Call reqparm.Add(Value.Key, Value.Value)
|
| 118 |
Next
|
| 119 |
|
| 120 |
Return reqparm
|
| 121 |
End Function
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
<ExportAPI("Build.Reqparm", Info:="Build the request parameters for the HTTP POST")>
|
| 129 |
<Extension>
|
| 130 |
Public Function BuildReqparm(data As IEnumerable(Of KeyValuePair(Of String, String))) As Specialized.NameValueCollection
|
| 131 |
Dim reqparm As New Specialized.NameValueCollection
|
| 132 |
For Each Value As KeyValuePair(Of String, String) In data
|
| 133 |
Call reqparm.Add(Value.Key, Value.Value)
|
| 134 |
Next
|
| 135 |
Return reqparm
|
| 136 |
End Function
|
| 137 |
|
| 138 |
Const PortOccupied As String = "Only one usage of each socket address (protocol/network address/port) Is normally permitted"
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
<Extension> Public Function IsSocketPortOccupied(ex As Exception) As Boolean
|
| 146 |
If TypeOf ex Is System.Net.Sockets.SocketException AndAlso
|
| 147 |
InStr(ex.ToString, PortOccupied, CompareMethod.Text) Then
|
| 148 |
Return True
|
| 149 |
Else
|
| 150 |
Return False
|
| 151 |
End If
|
| 152 |
End Function
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
<ExportAPI("CreateDirectory", Info:="Create a parameter dictionary from the request parameter tokens.")>
|
| 166 |
<Extension>
|
| 167 |
Public Function GenerateDictionary(tokens As String(), Optional lowercase As Boolean = True) As NameValueCollection
|
| 168 |
Dim out As New NameValueCollection
|
| 169 |
|
| 170 |
If tokens.IsNullOrEmpty Then
|
| 171 |
Return out
|
| 172 |
End If
|
| 173 |
If tokens.Length = 1 Then
|
| 174 |
If InStr(tokens(Scan0), "=") = 0 Then
|
| 175 |
Return out
|
| 176 |
End If
|
| 177 |
End If
|
| 178 |
|
| 179 |
Dim LQuery = (From s As String
|
| 180 |
In tokens
|
| 181 |
Let p As Integer = InStr(s, "="c)
|
| 182 |
Let Key As String = Mid(s, 1, p - 1)
|
| 183 |
Let value = Mid(s, p + 1)
|
| 184 |
Select Key,
|
| 185 |
value).ToArray
|
| 186 |
|
| 187 |
For Each x In LQuery
|
| 188 |
Dim name As String = If(lowercase,
|
| 189 |
x.Key.ToLower,
|
| 190 |
x.Key)
|
| 191 |
Call out.Add(name, x.value)
|
| 192 |
Next
|
| 193 |
|
| 194 |
Return out
|
| 195 |
End Function
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
<ExportAPI("Request.Parser")>
|
| 203 |
<Extension> Public Function QueryStringParameters(url$, Optional transLower As Boolean = True) As NameValueCollection
|
| 204 |
Dim tokens$()
|
| 205 |
|
| 206 |
With InStr(url, "://")
|
| 207 |
If .ByRef < 10 AndAlso .ByRef > 0 Then
|
| 208 |
url = url.GetTagValue("?").Value
|
| 209 |
End If
|
| 210 |
|
| 211 |
tokens = url.Split("&"c)
|
| 212 |
End With
|
| 213 |
|
| 214 |
Return GenerateDictionary(tokens, transLower)
|
| 215 |
End Function
|
| 216 |
|
| 217 |
ReadOnly urlEscaping As DefaultValue(Of Func(Of String, String)) = New Func(Of String, String)(AddressOf UrlEncode)
|
| 218 |
Friend ReadOnly noEscaping As DefaultValue(Of Func(Of String, String)) = New Func(Of String, String)(Function(s) s)
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 228 |
<Extension> Public Function BuildUrlData(data As IEnumerable(Of KeyValuePair(Of String, String)), Optional escaping As Boolean = False) As String
|
| 229 |
Return data.Select(Function(x) $"{x.Key}={(noEscaping Or urlEscaping.When(escaping))(x.Value) }").JoinBy("&")
|
| 230 |
End Function
|
| 231 |
|
| 232 |
<ExportAPI("Build.Args")>
|
| 233 |
Public Function BuildArgs(ParamArray params As String()()) As String
|
| 234 |
If params.IsNullOrEmpty Then
|
| 235 |
Return ""
|
| 236 |
Else
|
| 237 |
Dim values = params.Select(Function(arg) $"{arg(Scan0)}={arg(1)}").ToArray
|
| 238 |
Return String.Join("&", values)
|
| 239 |
End If
|
| 240 |
End Function
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
<Extension> <ExportAPI("URL.Decode")>
|
| 249 |
Public Function UrlDecode(s$, Optional encoding As Encoding = Nothing) As String
|
| 250 |
If s.StringEmpty Then
|
| 251 |
Return ""
|
| 252 |
End If
|
| 253 |
If encoding IsNot Nothing Then
|
| 254 |
Return HttpUtility.UrlDecode(s, encoding)
|
| 255 |
Else
|
| 256 |
Return HttpUtility.UrlDecode(s)
|
| 257 |
End If
|
| 258 |
End Function
|
| 259 |
|
| 260 |
<ExportAPI("URL.Decode")>
|
| 261 |
Public Sub UrlDecode(s As String, ByRef output As TextWriter)
|
| 262 |
If s IsNot Nothing Then
|
| 263 |
output.Write(UrlDecode(s))
|
| 264 |
End If
|
| 265 |
End Sub
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 275 |
<ExportAPI("URL.Encode")>
|
| 276 |
<Extension>
|
| 277 |
Public Function UrlEncode(s As String, Optional encoding As Encoding = Nothing) As String
|
| 278 |
If encoding IsNot Nothing Then
|
| 279 |
Return HttpUtility.UrlEncode(s, encoding)
|
| 280 |
Else
|
| 281 |
Return HttpUtility.UrlEncode(s)
|
| 282 |
End If
|
| 283 |
End Function
|
| 284 |
|
| 285 |
<ExportAPI("URL.Encode")>
|
| 286 |
Public Sub UrlEncode(s As String, ByRef output As TextWriter)
|
| 287 |
If s IsNot Nothing Then
|
| 288 |
output.Write(UrlEncode(s))
|
| 289 |
End If
|
| 290 |
End Sub
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
<ExportAPI("URL.PathEncode")>
|
| 298 |
<Extension>
|
| 299 |
Public Function UrlPathEncode(s As String) As String
|
| 300 |
If s Is Nothing Then
|
| 301 |
Return Nothing
|
| 302 |
End If
|
| 303 |
|
| 304 |
Dim idx As Integer = s.IndexOf("?"c)
|
| 305 |
Dim s2 As String = Nothing
|
| 306 |
|
| 307 |
If idx <> -1 Then
|
| 308 |
s2 = s.Substring(0, idx)
|
| 309 |
s2 = HttpUtility.UrlEncode(s2) & s.Substring(idx)
|
| 310 |
Else
|
| 311 |
s2 = HttpUtility.UrlEncode(s)
|
| 312 |
End If
|
| 313 |
|
| 314 |
Return s2
|
| 315 |
End Function
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
<ExportAPI("PostRequest.Parsing")>
|
| 324 |
<Extension> Public Function PostUrlDataParser(data$, Optional toLower As Boolean = True) As NameValueCollection
|
| 325 |
If String.IsNullOrEmpty(data) Then
|
| 326 |
Return New NameValueCollection
|
| 327 |
End If
|
| 328 |
|
| 329 |
Dim params$() = data.UrlDecode.Split("&"c)
|
| 330 |
Dim table = GenerateDictionary(params, toLower)
|
| 331 |
Return table
|
| 332 |
End Function
|
| 333 |
|
| 334 |
<ExportAPI("GET", Info:="GET http request")>
|
| 335 |
<Extension> Public Function GetRequest(strUrl$, ParamArray args As String()()) As String
|
| 336 |
If args.IsNullOrEmpty Then
|
| 337 |
Return GetRequest(strUrl)
|
| 338 |
Else
|
| 339 |
Dim params As String = BuildArgs(args)
|
| 340 |
|
| 341 |
If String.IsNullOrEmpty(params) Then
|
| 342 |
Return GetRequest(strUrl)
|
| 343 |
Else
|
| 344 |
Return GetRequest($"{strUrl}?{params}")
|
| 345 |
End If
|
| 346 |
End If
|
| 347 |
End Function
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
<ExportAPI("GET", Info:="GET http request")>
|
| 355 |
<Extension> Public Function GetRequest(url$, Optional https As Boolean = False, Optional userAgent As String = "Microsoft.VisualBasic.[HTTP/GET]") As String
|
| 356 |
Dim strData As String = ""
|
| 357 |
Dim strValue As New List(Of String)
|
| 358 |
Dim reader As New StreamReader(GetRequestRaw(url, https, userAgent), Encoding.UTF8)
|
| 359 |
|
| 360 |
Do While True
|
| 361 |
strData = reader.ReadLine()
|
| 362 |
If strData Is Nothing Then
|
| 363 |
Exit Do
|
| 364 |
Else
|
| 365 |
strValue += strData
|
| 366 |
End If
|
| 367 |
Loop
|
| 368 |
|
| 369 |
strData = String.Join(vbCrLf, strValue.ToArray)
|
| 370 |
Return strData
|
| 371 |
End Function
|
| 372 |
|
| 373 |
Sub New()
|
| 374 |
ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf CheckValidationResult)
|
| 375 |
End Sub
|
| 376 |
|
| 377 |
Private Function CheckValidationResult(sender As Object,
|
| 378 |
certificate As X509Certificate,
|
| 379 |
chain As X509Chain,
|
| 380 |
errors As SslPolicyErrors) As Boolean
|
| 381 |
Return True
|
| 382 |
End Function
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
Public Property Proxy As String
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
<ExportAPI("GET.Raw", Info:="GET http request")>
|
| 411 |
<Extension> Public Function GetRequestRaw(url As String,
|
| 412 |
Optional https As Boolean = False,
|
| 413 |
Optional userAgent As String = "Microsoft.VisualBasic.[HTTP/GET]") As Stream
|
| 414 |
Dim request As HttpWebRequest
|
| 415 |
If https Then
|
| 416 |
request = WebRequest.CreateDefault(New Uri(url))
|
| 417 |
Else
|
| 418 |
request = DirectCast(WebRequest.Create(url), HttpWebRequest)
|
| 419 |
End If
|
| 420 |
|
| 421 |
request.Method = "GET"
|
| 422 |
request.KeepAlive = False
|
| 423 |
request.ServicePoint.Expect100Continue = False
|
| 424 |
request.UserAgent = userAgent
|
| 425 |
|
| 426 |
Dim response As HttpWebResponse = DirectCast(request.GetResponse, HttpWebResponse)
|
| 427 |
Dim s As Stream = response.GetResponseStream()
|
| 428 |
Return s
|
| 429 |
End Function
|
| 430 |
|
| 431 |
<ExportAPI("POST", Info:="POST http request")>
|
| 432 |
Public Function PostRequest(url As String, Optional params As IEnumerable(Of KeyValuePair(Of String, String)) = Nothing) As String
|
| 433 |
Return url.POST(params.BuildReqparm)
|
| 434 |
End Function
|
| 435 |
|
| 436 |
<ExportAPI("POST", Info:="POST http request")>
|
| 437 |
Public Function PostRequest(url As String, ParamArray params As String()()) As String
|
| 438 |
Dim post As KeyValuePair(Of String, String)()
|
| 439 |
If params Is Nothing Then
|
| 440 |
post = Nothing
|
| 441 |
Else
|
| 442 |
post = params.Select(Function(value) New KeyValuePair(Of String, String)(value(0), value(1))).ToArray
|
| 443 |
End If
|
| 444 |
Return PostRequest(url, post)
|
| 445 |
End Function
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
<ExportAPI("POST", Info:="POST http request")>
|
| 457 |
<Extension> Public Function POST(url$,
|
| 458 |
Optional params As NameValueCollection = Nothing,
|
| 459 |
Optional headers As Dictionary(Of String, String) = Nothing,
|
| 460 |
Optional Referer$ = "",
|
| 461 |
Optional proxy$ = Nothing,
|
| 462 |
Optional contentEncoding As Encodings = Encodings.UTF8) As String
|
| 463 |
|
| 464 |
Static emptyBody As New DefaultValue(Of NameValueCollection) With {
|
| 465 |
.Value = New NameValueCollection,
|
| 466 |
.assert = Function(c)
|
| 467 |
Return c Is Nothing OrElse DirectCast(c, NameValueCollection).Count = 0
|
| 468 |
End Function
|
| 469 |
}
|
| 470 |
|
| 471 |
Using request As New WebClient
|
| 472 |
|
| 473 |
Call request.Headers.Add("User-Agent", UserAgent.GoogleChrome)
|
| 474 |
Call request.Headers.Add(NameOf(Referer), Referer)
|
| 475 |
|
| 476 |
For Each header In headers.SafeQuery
|
| 477 |
If Not request.Headers.ContainsKey(header.Key) Then
|
| 478 |
request.Headers.Add(header.Key, header.Value)
|
| 479 |
End If
|
| 480 |
Next
|
| 481 |
|
| 482 |
If String.IsNullOrEmpty(proxy) Then
|
| 483 |
proxy = WebServiceUtils.Proxy
|
| 484 |
End If
|
| 485 |
If Not String.IsNullOrEmpty(proxy) Then
|
| 486 |
Call request.SetProxy(proxy)
|
| 487 |
End If
|
| 488 |
|
| 489 |
Call $"[POST] {url}....".__DEBUG_ECHO
|
| 490 |
|
| 491 |
Dim response As Byte() = request.UploadValues(url, "POST", params Or emptyBody)
|
| 492 |
Dim str$ = contentEncoding.CodePage.GetString(response)
|
| 493 |
|
| 494 |
Call $"[GET] {response.Length} bytes...".__DEBUG_ECHO
|
| 495 |
|
| 496 |
Return str
|
| 497 |
End Using
|
| 498 |
End Function
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
<Extension>
|
| 508 |
Public Function POSTFile(url$, buffer As Byte(), Optional name$ = "", Optional referer$ = Nothing) As String
|
| 509 |
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
|
| 510 |
|
| 511 |
request.Method = "POST"
|
| 512 |
request.Accept = "application/json"
|
| 513 |
request.ContentLength = buffer.Length
|
| 514 |
request.ContentType = "multipart/form-data; boundary=------WebKitFormBoundaryBpijhG6dKsQpCMdN--;"
|
| 515 |
request.UserAgent = UserAgent.GoogleChrome
|
| 516 |
request.Referer = referer
|
| 517 |
' request.Headers("fileName") = name Or File.FileName.AsDefault
|
| 518 |
|
| 519 |
If Not String.IsNullOrEmpty(Proxy) Then
|
| 520 |
Call request.SetProxy(Proxy)
|
| 521 |
End If
|
| 522 |
|
| 523 |
Call $"[POST] {url}....".__DEBUG_ECHO
|
| 524 |
|
| 525 |
|
| 526 |
With request.GetRequestStream()
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
Call .Write(buffer, Scan0, buffer.Length)
|
| 532 |
Call .Flush()
|
| 533 |
End With
|
| 534 |
|
| 535 |
|
| 536 |
Dim response = DirectCast(request.GetResponse(), HttpWebResponse)
|
| 537 |
|
| 538 |
Using responseStream As New StreamReader(response.GetResponseStream())
|
| 539 |
Dim html As New StringBuilder
|
| 540 |
Dim s As New Value(Of String)
|
| 541 |
|
| 542 |
Do While Not (s = responseStream.ReadLine) Is Nothing
|
| 543 |
Call html.AppendLine(+s)
|
| 544 |
Loop
|
| 545 |
|
| 546 |
Call $"Get {html.Length} bytes from server response...".__DEBUG_ECHO
|
| 547 |
|
| 548 |
Return html.ToString
|
| 549 |
End Using
|
| 550 |
End Function
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
<ExportAPI("POST", Info:="POST http request")>
|
| 560 |
<Extension> Public Function POST(url$, data As Dictionary(Of String, String()),
|
| 561 |
Optional Referer$ = "",
|
| 562 |
Optional proxy$ = Nothing,
|
| 563 |
Optional ua As String = UserAgent.GoogleChrome) As String
|
| 564 |
|
| 565 |
Dim postString As New List(Of String)
|
| 566 |
|
| 567 |
For Each postValue As KeyValuePair(Of String, String()) In data
|
| 568 |
postString += postValue.Value _
|
| 569 |
.Select(Function(v) postValue.Key & "=" & HttpUtility.UrlEncode(v))
|
| 570 |
Next
|
| 571 |
|
| 572 |
Dim postData As String = postString.JoinBy("&")
|
| 573 |
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
|
| 574 |
|
| 575 |
request.Method = "POST"
|
| 576 |
request.Accept = "application/json"
|
| 577 |
request.ContentLength = postData.Length
|
| 578 |
request.ContentType = "application/x-www-form-urlencoded; charset=utf-8"
|
| 579 |
request.UserAgent = ua
|
| 580 |
request.Referer = Referer
|
| 581 |
|
| 582 |
If Not String.IsNullOrEmpty(proxy) Then
|
| 583 |
Call request.SetProxy(proxy)
|
| 584 |
End If
|
| 585 |
|
| 586 |
Call $"[POST] {url}....".__DEBUG_ECHO
|
| 587 |
|
| 588 |
|
| 589 |
Using sender As New StreamWriter(request.GetRequestStream())
|
| 590 |
sender.Write(postData)
|
| 591 |
End Using
|
| 592 |
|
| 593 |
|
| 594 |
Dim response = DirectCast(request.GetResponse(), HttpWebResponse)
|
| 595 |
Using responseStream As New StreamReader(response.GetResponseStream())
|
| 596 |
Dim html As New StringBuilder
|
| 597 |
Dim s As New Value(Of String)
|
| 598 |
|
| 599 |
Do While Not (s = responseStream.ReadLine) Is Nothing
|
| 600 |
Call html.AppendLine(+s)
|
| 601 |
Loop
|
| 602 |
|
| 603 |
Call $"[GET] {html.Length} bytes...".__DEBUG_ECHO
|
| 604 |
|
| 605 |
Return html.ToString
|
| 606 |
End Using
|
| 607 |
End Function
|
| 608 |
|
| 609 |
<Extension>
|
| 610 |
Public Sub SetProxy(ByRef request As HttpWebRequest, proxy As String)
|
| 611 |
request.Proxy = proxy.GetProxy
|
| 612 |
End Sub
|
| 613 |
|
| 614 |
<Extension>
|
| 615 |
Public Sub SetProxy(ByRef request As WebClient, proxy As String)
|
| 616 |
request.Proxy = proxy.GetProxy
|
| 617 |
End Sub
|
| 618 |
|
| 619 |
<Extension>
|
| 620 |
Public Function GetProxy(proxy As String) As WebProxy
|
| 621 |
Return New WebProxy With {
|
| 622 |
.Address = New Uri(proxy),
|
| 623 |
.Credentials = New NetworkCredential()
|
| 624 |
}
|
| 625 |
End Function
|
| 626 |
|
| 627 |
Public Property DefaultUA As String = UserAgent.GoogleChrome
|
| 628 |
|
| 629 |
#If FRAMEWORD_CORE Then
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
<ExportAPI("wget", Info:="Download data from the specific URL location.")>
|
| 638 |
<Extension> Public Function DownloadFile(<Parameter("url")> strUrl$,
|
| 639 |
<Parameter("Path.Save", "The saved location of the downloaded file data.")>
|
| 640 |
save$,
|
| 641 |
Optional proxy$ = Nothing,
|
| 642 |
Optional ua$ = UserAgent.FireFox,
|
| 643 |
Optional retry% = 0,
|
| 644 |
Optional progressHandle As DownloadProgressChangedEventHandler = Nothing,
|
| 645 |
Optional refer$ = Nothing,
|
| 646 |
<CallerMemberName>
|
| 647 |
Optional trace$ = Nothing) As Boolean
|
| 648 |
#Else
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
<Extension> Public Function DownloadFile(strUrl As String, SavedPath As String) As Boolean
|
| 657 |
#End If
|
| 658 |
RE0:
|
| 659 |
Try
|
| 660 |
Using browser As New WebClient()
|
| 661 |
If Not String.IsNullOrEmpty(proxy) Then
|
| 662 |
Call browser.SetProxy(proxy)
|
| 663 |
End If
|
| 664 |
If Not refer.StringEmpty Then
|
| 665 |
browser.Headers.Add(NameOf(refer), refer)
|
| 666 |
End If
|
| 667 |
If Not progressHandle Is Nothing Then
|
| 668 |
AddHandler browser.DownloadProgressChanged, progressHandle
|
| 669 |
End If
|
| 670 |
|
| 671 |
Call browser.Headers.Add(UserAgent.UAheader, ua)
|
| 672 |
Call $"{strUrl} --> {save}".__DEBUG_ECHO
|
| 673 |
Call save.ParentPath.MkDIR
|
| 674 |
Call browser.DownloadFile(strUrl, save)
|
| 675 |
End Using
|
| 676 |
|
| 677 |
Return True
|
| 678 |
Catch ex As Exception
|
| 679 |
Call App.LogException(New Exception(strUrl, ex), trace)
|
| 680 |
Call ex.PrintException
|
| 681 |
|
| 682 |
If retry > 0 Then
|
| 683 |
retry -= 1
|
| 684 |
GoTo RE0
|
| 685 |
Else
|
| 686 |
|
| 687 |
End If
|
| 688 |
|
| 689 |
Return False
|
| 690 |
Finally
|
| 691 |
If save.FileExists Then
|
| 692 |
Call $"[{FileIO.FileSystem.GetFileInfo(save).Length} Bytes]".__DEBUG_ECHO
|
| 693 |
Else
|
| 694 |
Call $"Download failure!".__DEBUG_ECHO
|
| 695 |
End If
|
| 696 |
End Try
|
| 697 |
End Function
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
<ExportAPI("GET.Download", Info:="Download file from http request and save to a specific location.")>
|
| 707 |
<Extension> Public Function GetDownload(url As String, savePath As String) As Boolean
|
| 708 |
Try
|
| 709 |
Dim responseStream As Stream = GetRequestRaw(url)
|
| 710 |
Dim localBuffer As Stream = responseStream.CopyStream
|
| 711 |
Call $"[{localBuffer.Length} Bytes]".__DEBUG_ECHO
|
| 712 |
Return localBuffer.FlushStream(savePath)
|
| 713 |
Catch ex As Exception
|
| 714 |
ex = New Exception(url, ex)
|
| 715 |
Call ex.PrintException
|
| 716 |
Call App.LogException(ex)
|
| 717 |
Return False
|
| 718 |
End Try
|
| 719 |
End Function
|
| 720 |
|
| 721 |
Public Const IPAddress As String = "http://ipaddress.com/"
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
Public Const MicrosoftDNS As String = "4.2.2.1"
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
Public Function GetMyIPAddress() As String
|
| 732 |
Dim hasInternet As Boolean
|
| 733 |
|
| 734 |
Try
|
| 735 |
hasInternet = Not PingUtility.Ping(System.Net.IPAddress.Parse(MicrosoftDNS)) > Integer.MaxValue
|
| 736 |
Catch ex As Exception
|
| 737 |
hasInternet = False
|
| 738 |
End Try
|
| 739 |
|
| 740 |
If hasInternet Then
|
| 741 |
|
| 742 |
Return __getMyIPAddress()
|
| 743 |
Else
|
| 744 |
|
| 745 |
Return TcpRequest.LocalIPAddress
|
| 746 |
End If
|
| 747 |
End Function
|
| 748 |
|
| 749 |
Public Const RegexIPAddress As String = "\d{1,3}(\.\d{1,3}){3}"
|
| 750 |
|
| 751 |
Private Function __getMyIPAddress() As String
|
| 752 |
Dim page As String = IPAddress.GET
|
| 753 |
Dim ipResult As String = Regex.Match(page, $"IP[:] {RegexIPAddress}<br><img", RegexOptions.IgnoreCase).Value
|
| 754 |
ipResult = Regex.Match(ipResult, RegexIPAddress).Value
|
| 755 |
Return ipResult
|
| 756 |
End Function
|
| 757 |
End Module
|