| 1 |
#Region "Microsoft.VisualBasic::ebff84e767340583f68aa576db37f8ad, Microsoft.VisualBasic.Core\Extensions\WebServices\HttpGet.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 |
#End Region
|
| 43 |
|
| 44 |
Imports System.IO
|
| 45 |
Imports System.Net
|
| 46 |
Imports System.Runtime.CompilerServices
|
| 47 |
Imports System.Text
|
| 48 |
Imports Microsoft.VisualBasic.CommandLine.Reflection
|
| 49 |
Imports Microsoft.VisualBasic.Language
|
| 50 |
Imports Microsoft.VisualBasic.Net.Http
|
| 51 |
Imports Microsoft.VisualBasic.Scripting.MetaData
|
| 52 |
Imports Microsoft.VisualBasic.Text.HtmlParser
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
Public Module HttpGet
|
| 58 |
|
| 59 |
#If FRAMEWORD_CORE Then
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
<ExportAPI("Webpage.Request", Info:="Get the html page content from a website request Or a html file on the local filesystem.")>
|
| 69 |
<Extension> Public Function [GET](url As String,
|
| 70 |
<Parameter("Request.TimeOut")>
|
| 71 |
Optional retry As UInt16 = 0,
|
| 72 |
<Parameter("FileSystem.Works?", "Is this a local html document on your filesystem?")>
|
| 73 |
Optional isFileUrl As Boolean = False,
|
| 74 |
Optional headers As Dictionary(Of String, String) = Nothing,
|
| 75 |
Optional proxy As String = Nothing,
|
| 76 |
Optional doNotRetry404 As Boolean = True,
|
| 77 |
Optional UA$ = UserAgent.GoogleChrome,
|
| 78 |
Optional refer$ = Nothing) As String
|
| 79 |
#Else
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
''
|
| 88 |
#End If
|
| 89 |
' Call $"Request data from: {If(isFileUrl, url.ToFileURL, url)}".__DEBUG_ECHO
|
| 90 |
Call $"GET {If(isFileUrl, url.ToFileURL, url)}".__DEBUG_ECHO
|
| 91 |
|
| 92 |
If FileIO.FileSystem.FileExists(url) Then
|
| 93 |
Call "[Job DONE!]".__DEBUG_ECHO
|
| 94 |
Return FileIO.FileSystem.ReadAllText(url)
|
| 95 |
Else
|
| 96 |
If isFileUrl Then
|
| 97 |
Call $"URL {url.ToFileURL} can not solved on your filesystem!".Warning
|
| 98 |
Return ""
|
| 99 |
End If
|
| 100 |
End If
|
| 101 |
|
| 102 |
If Not refer.StringEmpty Then
|
| 103 |
If headers Is Nothing Then
|
| 104 |
headers = New Dictionary(Of String, String)
|
| 105 |
End If
|
| 106 |
|
| 107 |
headers(NameOf(refer)) = refer
|
| 108 |
End If
|
| 109 |
|
| 110 |
Return url.__httpRequest(retry, headers, proxy, doNotRetry404, UA)
|
| 111 |
End Function
|
| 112 |
|
| 113 |
<Extension>
|
| 114 |
Private Function __httpRequest(url$, retries%, headers As Dictionary(Of String, String), proxy$, DoNotRetry404 As Boolean, UA$) As String
|
| 115 |
Dim retryTime As Integer = 0
|
| 116 |
|
| 117 |
If String.IsNullOrEmpty(proxy) Then
|
| 118 |
proxy = WebServiceUtils.Proxy
|
| 119 |
End If
|
| 120 |
|
| 121 |
Try
|
| 122 |
RETRY: Return __get(url, headers, proxy, UA)
|
| 123 |
Catch ex As Exception When InStr(ex.Message, "(404) Not Found") > 0 AndAlso DoNotRetry404
|
| 124 |
Return LogException(url, New Exception(url, ex))
|
| 125 |
|
| 126 |
Catch ex As Exception When retryTime < retries
|
| 127 |
|
| 128 |
retryTime += 1
|
| 129 |
|
| 130 |
Call "Data download error, retry connect to the server!".PrintException
|
| 131 |
GoTo RETRY
|
| 132 |
|
| 133 |
Catch ex As Exception
|
| 134 |
ex = New Exception(url, ex)
|
| 135 |
ex.PrintException
|
| 136 |
|
| 137 |
Return LogException(url, ex)
|
| 138 |
End Try
|
| 139 |
End Function
|
| 140 |
|
| 141 |
Private Function LogException(url As String, ex As Exception) As String
|
| 142 |
Dim exMessage As String = String.Format("Unable to get the http request!" & vbCrLf &
|
| 143 |
" Url:=[{0}]" & vbCrLf &
|
| 144 |
" EXCEPTION ===>" & vbCrLf & ex.ToString, url)
|
| 145 |
Call App.LogException(exMessage, NameOf([GET]) & "::HTTP_REQUEST_EXCEPTION")
|
| 146 |
Return ""
|
| 147 |
End Function
|
| 148 |
|
| 149 |
Const doctorcomError$ = "Please login your Campus Broadband Network Client at first!"
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
Public Property HttpRequestTimeOut As Double
|
| 156 |
|
| 157 |
Private Function __get(url$, headers As Dictionary(Of String, String), proxy$, UA$) As String
|
| 158 |
Dim timer As Stopwatch = Stopwatch.StartNew
|
| 159 |
Dim webRequest As HttpWebRequest = HttpWebRequest.Create(url)
|
| 160 |
|
| 161 |
webRequest.Headers.Add("Accept-Language", "en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3")
|
| 162 |
webRequest.UserAgent = If(UA = UserAgent.GoogleChrome, DefaultUA, UA)
|
| 163 |
|
| 164 |
If HttpRequestTimeOut > 0 Then
|
| 165 |
webRequest.Timeout = 1000 * HttpRequestTimeOut
|
| 166 |
End If
|
| 167 |
|
| 168 |
If Not headers.IsNullOrEmpty Then
|
| 169 |
For Each x In headers
|
| 170 |
webRequest.Headers(x.Key) = x.Value
|
| 171 |
Next
|
| 172 |
End If
|
| 173 |
If Not String.IsNullOrEmpty(proxy) Then
|
| 174 |
Call webRequest.SetProxy(proxy)
|
| 175 |
End If
|
| 176 |
|
| 177 |
Using respStream As Stream = webRequest.GetResponse.GetResponseStream,
|
| 178 |
reader As New StreamReader(respStream)
|
| 179 |
|
| 180 |
Dim htmlBuilder As New StringBuilder
|
| 181 |
Dim line As Value(Of String) = ""
|
| 182 |
|
| 183 |
Do While Not (line = reader.ReadLine) Is Nothing
|
| 184 |
htmlBuilder.AppendLine(line)
|
| 185 |
Loop
|
| 186 |
|
| 187 |
Dim html As String = htmlBuilder.ToString
|
| 188 |
Dim title As String = html.HTMLTitle
|
| 189 |
|
| 190 |
|
| 191 |
If InStr(html, "http://www.doctorcom.com", CompareMethod.Text) > 0 Then
|
| 192 |
Call doctorcomError.PrintException
|
| 193 |
Return ""
|
| 194 |
End If
|
| 195 |
|
| 196 |
Call $"[{title} {url}] --> sizeOf:={Len(html)} chars; response_time:={timer.ElapsedMilliseconds} ms.".__DEBUG_ECHO
|
| 197 |
#If DEBUG Then
|
| 198 |
Call html.SaveTo($"{App.AppSystemTemp}/{App.PID}/{url.NormalizePathString}.html")
|
| 199 |
#End If
|
| 200 |
Return html
|
| 201 |
End Using
|
| 202 |
End Function
|
| 203 |
End Module
|