2019-05-22 19:02:42 +08:00
|
|
|
|
#Region "Microsoft.VisualBasic::994b1956aa075a24286a1fac61a9418f, Microsoft.VisualBasic.Core\Extensions\WebServices\HttpGet.vb"
|
|
|
|
|
|
|
|
|
|
|
|
' Author:
|
|
|
|
|
|
'
|
|
|
|
|
|
' asuka (amethyst.asuka@gcmodeller.org)
|
|
|
|
|
|
' xie (genetics@smrucc.org)
|
|
|
|
|
|
' xieguigang (xie.guigang@live.com)
|
|
|
|
|
|
'
|
|
|
|
|
|
' Copyright (c) 2018 GPL3 Licensed
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' GNU GENERAL PUBLIC LICENSE (GPL3)
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' This program is free software: you can redistribute it and/or modify
|
|
|
|
|
|
' it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
' the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
|
' (at your option) any later version.
|
|
|
|
|
|
'
|
|
|
|
|
|
' This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
' but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
' GNU General Public License for more details.
|
|
|
|
|
|
'
|
|
|
|
|
|
' You should have received a copy of the GNU General Public License
|
|
|
|
|
|
' along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
' /********************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
' Summaries:
|
|
|
|
|
|
|
|
|
|
|
|
' Module HttpGet
|
|
|
|
|
|
'
|
|
|
|
|
|
' Properties: HttpRequestTimeOut
|
|
|
|
|
|
'
|
|
|
|
|
|
' Function: [GET], BuildWebRequest, Get_PageContent, httpRequest, LogException
|
|
|
|
|
|
' urlGet
|
|
|
|
|
|
'
|
|
|
|
|
|
' /********************************************************************************/
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
|
|
|
|
|
#End Region
|
|
|
|
|
|
|
|
|
|
|
|
Imports System.IO
|
|
|
|
|
|
Imports System.Net
|
|
|
|
|
|
Imports System.Runtime.CompilerServices
|
|
|
|
|
|
Imports System.Text
|
|
|
|
|
|
Imports Microsoft.VisualBasic.CommandLine.Reflection
|
|
|
|
|
|
Imports Microsoft.VisualBasic.Language
|
|
|
|
|
|
Imports Microsoft.VisualBasic.Scripting.MetaData
|
2019-05-20 19:39:25 +08:00
|
|
|
|
Imports Microsoft.VisualBasic.Text
|
2019-03-10 18:01:40 +08:00
|
|
|
|
Imports Microsoft.VisualBasic.Text.Parser.HtmlParser
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' Tools for http get
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
Public Module HttpGet
|
|
|
|
|
|
|
|
|
|
|
|
#If FRAMEWORD_CORE Then
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' Get the html page content from a website request or a html file on the local filesystem.
|
|
|
|
|
|
''' (同时支持http位置或者本地文件,失败或者错误会返回空字符串)
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <param name="url">web http request url or a file path handle</param>
|
|
|
|
|
|
''' <param name="retry">发生错误的时候的重试的次数</param>
|
|
|
|
|
|
''' <returns>失败或者错误会返回空字符串</returns>
|
|
|
|
|
|
''' <remarks>这个工具只适合于文本数据的传输操作</remarks>
|
|
|
|
|
|
<ExportAPI("Webpage.Request", Info:="Get the html page content from a website request Or a html file on the local filesystem.")>
|
|
|
|
|
|
<Extension> Public Function [GET](url As String,
|
|
|
|
|
|
<Parameter("Request.TimeOut")>
|
|
|
|
|
|
Optional retry As UInt16 = 0,
|
|
|
|
|
|
Optional headers As Dictionary(Of String, String) = Nothing,
|
|
|
|
|
|
Optional proxy As String = Nothing,
|
|
|
|
|
|
Optional doNotRetry404 As Boolean = True,
|
2019-03-06 20:07:16 +08:00
|
|
|
|
Optional UA$ = Nothing,
|
2019-05-20 19:39:25 +08:00
|
|
|
|
Optional refer$ = Nothing,
|
|
|
|
|
|
Optional ByRef is404 As Boolean = False) As String
|
2018-08-02 20:14:48 +08:00
|
|
|
|
#Else
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' Get the html page content from a website request or a html file on the local filesystem.
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <param name="url">web http request url or a file path handle</param>
|
|
|
|
|
|
''' <param name="RequestTimeOut">发生错误的时候的重试的次数</param>
|
|
|
|
|
|
''' <returns></returns>
|
|
|
|
|
|
''' <remarks></remarks>
|
|
|
|
|
|
'''
|
|
|
|
|
|
<Extension> Public Function Get_PageContent(url As String, Optional RequestTimeOut As UInteger = 20, Optional FileSystemUrl As Boolean = False) As String
|
|
|
|
|
|
#End If
|
2019-03-15 18:19:37 +08:00
|
|
|
|
Dim isFileUrl As String = (InStr(url, "http://", CompareMethod.Text) <> 1) AndAlso (InStr(url, "https://", CompareMethod.Text) <> 1)
|
|
|
|
|
|
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Call $"GET {If(isFileUrl, url.ToFileURL, url)}".__DEBUG_ECHO
|
|
|
|
|
|
|
2019-05-20 19:39:25 +08:00
|
|
|
|
' do status indicator reset
|
|
|
|
|
|
is404 = False
|
|
|
|
|
|
|
2019-03-15 18:19:37 +08:00
|
|
|
|
' 类似于php之中的file_get_contents函数,可以读取本地文件内容
|
|
|
|
|
|
If File.Exists(url) Then
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Call "[Job DONE!]".__DEBUG_ECHO
|
2019-03-15 18:19:37 +08:00
|
|
|
|
Return url.ReadAllText
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Else
|
|
|
|
|
|
If isFileUrl Then
|
2019-05-20 19:39:25 +08:00
|
|
|
|
Call $"URL {url.ToFileURL} can not be solved on your filesystem!".Warning
|
|
|
|
|
|
is404 = True
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Return ""
|
|
|
|
|
|
End If
|
|
|
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
|
|
If Not refer.StringEmpty Then
|
|
|
|
|
|
If headers Is Nothing Then
|
|
|
|
|
|
headers = New Dictionary(Of String, String)
|
|
|
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
|
|
headers(NameOf(refer)) = refer
|
|
|
|
|
|
End If
|
|
|
|
|
|
|
2019-05-20 19:39:25 +08:00
|
|
|
|
Return url.httpRequest(retry, headers, proxy, doNotRetry404, UA, is404)
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
|
|
<Extension>
|
2019-05-20 19:39:25 +08:00
|
|
|
|
Private Function httpRequest(url$, retries%, headers As Dictionary(Of String, String), proxy$, DoNotRetry404 As Boolean, UA$, ByRef is404 As Boolean) As String
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Dim retryTime As Integer = 0
|
|
|
|
|
|
|
|
|
|
|
|
If String.IsNullOrEmpty(proxy) Then
|
|
|
|
|
|
proxy = WebServiceUtils.Proxy
|
|
|
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
|
|
Try
|
2019-05-06 18:20:52 +08:00
|
|
|
|
RETRY: Return BuildWebRequest(url, headers, proxy, UA).urlGet()
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Catch ex As Exception When InStr(ex.Message, "(404) Not Found") > 0 AndAlso DoNotRetry404
|
2019-05-20 19:39:25 +08:00
|
|
|
|
is404 = True
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Return LogException(url, New Exception(url, ex))
|
|
|
|
|
|
|
|
|
|
|
|
Catch ex As Exception When retryTime < retries
|
|
|
|
|
|
|
|
|
|
|
|
retryTime += 1
|
|
|
|
|
|
|
|
|
|
|
|
Call "Data download error, retry connect to the server!".PrintException
|
|
|
|
|
|
GoTo RETRY
|
|
|
|
|
|
|
|
|
|
|
|
Catch ex As Exception
|
|
|
|
|
|
ex = New Exception(url, ex)
|
|
|
|
|
|
ex.PrintException
|
|
|
|
|
|
|
|
|
|
|
|
Return LogException(url, ex)
|
|
|
|
|
|
End Try
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
2019-05-20 19:39:25 +08:00
|
|
|
|
Private Function LogException(url$, ex As Exception) As String
|
|
|
|
|
|
Dim exMsg As String = {
|
|
|
|
|
|
"Unable to get the http request!",
|
|
|
|
|
|
$" Url:=[{url}]",
|
|
|
|
|
|
" EXCEPTION ===>",
|
|
|
|
|
|
"",
|
|
|
|
|
|
ex.ToString
|
|
|
|
|
|
}.JoinBy(ASCII.LF)
|
|
|
|
|
|
|
|
|
|
|
|
Return App.LogException(exMsg, NameOf([GET]) & "::HTTP_REQUEST_EXCEPTION")
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
|
|
Const doctorcomError$ = "Please login your Campus Broadband Network Client at first!"
|
|
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' Request timeout unit in seconds.
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <returns></returns>
|
|
|
|
|
|
Public Property HttpRequestTimeOut As Double
|
|
|
|
|
|
|
2019-03-15 18:19:37 +08:00
|
|
|
|
Public Function BuildWebRequest(url$, headers As Dictionary(Of String, String), proxy$, UA$) As HttpWebRequest
|
2018-08-02 20:14:48 +08:00
|
|
|
|
Dim webRequest As HttpWebRequest = HttpWebRequest.Create(url)
|
|
|
|
|
|
|
|
|
|
|
|
webRequest.Headers.Add("Accept-Language", "en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3")
|
2019-03-06 20:07:16 +08:00
|
|
|
|
webRequest.UserAgent = UA Or DefaultUA
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
|
|
|
|
|
If HttpRequestTimeOut > 0 Then
|
|
|
|
|
|
webRequest.Timeout = 1000 * HttpRequestTimeOut
|
|
|
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
|
|
If Not headers.IsNullOrEmpty Then
|
|
|
|
|
|
For Each x In headers
|
|
|
|
|
|
webRequest.Headers(x.Key) = x.Value
|
|
|
|
|
|
Next
|
|
|
|
|
|
End If
|
|
|
|
|
|
If Not String.IsNullOrEmpty(proxy) Then
|
|
|
|
|
|
Call webRequest.SetProxy(proxy)
|
|
|
|
|
|
End If
|
|
|
|
|
|
|
2019-03-15 18:19:37 +08:00
|
|
|
|
Return webRequest
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
2019-05-06 18:20:52 +08:00
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' Perform a web url query request
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <param name="webrequest"></param>
|
|
|
|
|
|
''' <returns></returns>
|
|
|
|
|
|
<Extension> Private Function urlGet(webrequest As HttpWebRequest) As String
|
2019-03-15 18:19:37 +08:00
|
|
|
|
Dim timer As Stopwatch = Stopwatch.StartNew
|
|
|
|
|
|
Dim url As String = webrequest.RequestUri.ToString
|
|
|
|
|
|
|
|
|
|
|
|
Using respStream As Stream = webrequest.GetResponse.GetResponseStream,
|
2018-08-02 20:14:48 +08:00
|
|
|
|
reader As New StreamReader(respStream)
|
|
|
|
|
|
|
|
|
|
|
|
Dim htmlBuilder As New StringBuilder
|
|
|
|
|
|
Dim line As Value(Of String) = ""
|
|
|
|
|
|
|
|
|
|
|
|
Do While Not (line = reader.ReadLine) Is Nothing
|
|
|
|
|
|
htmlBuilder.AppendLine(line)
|
|
|
|
|
|
Loop
|
|
|
|
|
|
|
|
|
|
|
|
Dim html As String = htmlBuilder.ToString
|
|
|
|
|
|
Dim title As String = html.HTMLTitle
|
|
|
|
|
|
|
|
|
|
|
|
' 判断是否是由于还没有登陆校园网客户端而导致的错误
|
|
|
|
|
|
If InStr(html, "http://www.doctorcom.com", CompareMethod.Text) > 0 Then
|
|
|
|
|
|
Call doctorcomError.PrintException
|
|
|
|
|
|
Return ""
|
2019-04-08 19:41:19 +08:00
|
|
|
|
Else
|
|
|
|
|
|
Dim time$ = ValueTypes.ReadableElapsedTime(timer.ElapsedMilliseconds)
|
|
|
|
|
|
Dim debug$ = $"[{url}] {title} - {Len(html)} chars in {time}"
|
|
|
|
|
|
|
|
|
|
|
|
If timer.ElapsedMilliseconds > 1000 Then
|
|
|
|
|
|
Call debug.Warning
|
|
|
|
|
|
Else
|
|
|
|
|
|
Call debug.__INFO_ECHO
|
|
|
|
|
|
End If
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
|
|
#If DEBUG Then
|
|
|
|
|
|
Call html.SaveTo($"{App.AppSystemTemp}/{App.PID}/{url.NormalizePathString}.html")
|
|
|
|
|
|
#End If
|
|
|
|
|
|
Return html
|
|
|
|
|
|
End Using
|
|
|
|
|
|
End Function
|
|
|
|
|
|
End Module
|