|
[code=vb]Imports System.Collections.Generic
Imports System.Text
Imports System.Net
Imports System.IO
Imports System.Threading
Namespace jayleke
Public Class HttpHelper
#Region "私有变量"
Private Shared cc As New CookieContainer()
Private Shared contentType As String = "application/x-www-form-urlencoded"
Private Shared accept As String = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*"
Private Shared userAgent As String = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"
Private Shared m_encoding As Encoding = Encoding.GetEncoding("utf-8")
Private Shared delay As Integer = 3000
'延迟访问防止连续访问被发现
Private Shared m_maxTry As Integer = 300
Private Shared currentTry As Integer = 0
#End Region
#Region "属性"
''' <summary></summary>
''' Cookie容器
'''
Public Shared ReadOnly Property CookieContainer() As CookieContainer
Get
Return cc
End Get
End Property
''' <summary></summary>
''' 获取网页源码时使用的编码
'''
''' <value></value>
Public Shared Property Encoding() As Encoding
Get
Return m_encoding
End Get
Set(value As Encoding)
m_encoding = value
End Set
End Property
Public Shared Property NetworkDelay() As Integer
Get
Dim r As New Random()
Return (r.[Next](delay \ 1000, (delay \ 1000) * 2)) * 1000
End Get
Set(value As Integer)
delay = value
End Set
End Property
Public Shared Property MaxTry() As Integer
Get
Return m_maxTry
End Get
Set(value As Integer)
m_maxTry = value
End Set
End Property
#End Region
#Region "公共方法"
Public Shared Function GetHtml(url As String, postData As String, isPost As Boolean, cookieContainer As CookieContainer) As String
If String.IsNullOrEmpty(postData) Then Return GetHtml(url, cookieContainer)
Thread.Sleep(NetworkDelay)
currentTry += 1
Dim httpWebRequest__1 As HttpWebRequest = Nothing
Dim httpWebResponse As HttpWebResponse = Nothing
Try
Dim byteRequest As Byte() = Encoding.[Default].GetBytes(postData)
httpWebRequest__1 = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
httpWebRequest__1.CookieContainer = cookieContainer
httpWebRequest__1.ContentType = contentType
httpWebRequest__1.ServicePoint.ConnectionLimit = m_maxTry
httpWebRequest__1.Referer = url
httpWebRequest__1.Accept = accept
httpWebRequest__1.UserAgent = userAgent
httpWebRequest__1.Method = If(isPost, "POST", "GET")
httpWebRequest__1.ContentLength = byteRequest.Length
Dim stream As Stream = httpWebRequest__1.GetRequestStream()
stream.Write(byteRequest, 0, byteRequest.Length)
stream.Close()
httpWebResponse = DirectCast(httpWebRequest__1.GetResponse(), HttpWebResponse)
Dim responseStream As Stream = httpWebResponse.GetResponseStream()
Dim streamReader As New StreamReader(responseStream, m_encoding)
Dim html As String = streamReader.ReadToEnd()
streamReader.Close()
responseStream.Close()
currentTry = 0
httpWebRequest__1.Abort()
httpWebResponse.Close()
Return html
Catch e As Exception
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") & e.Message)
Console.ForegroundColor = ConsoleColor.White
If currentTry <= m_maxTry Then GetHtml(url, postData, isPost, cookieContainer)
currentTry -= 1
If httpWebRequest__1 IsNot Nothing Then httpWebRequest__1.Abort()
If httpWebResponse IsNot Nothing Then httpWebResponse.Close()
Return String.Empty
End Try
End Function
Public Shared Function GetHtml(url As String, cookieContainer As CookieContainer) As String
Thread.Sleep(NetworkDelay)
currentTry += 1
Dim httpWebRequest__1 As HttpWebRequest = Nothing
Dim httpWebResponse As HttpWebResponse = Nothing
Try
httpWebRequest__1 = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
httpWebRequest__1.CookieContainer = cookieContainer
httpWebRequest__1.ContentType = contentType
httpWebRequest__1.ServicePoint.ConnectionLimit = m_maxTry
httpWebRequest__1.Referer = url
httpWebRequest__1.Accept = accept
httpWebRequest__1.UserAgent = userAgent
httpWebRequest__1.Method = "GET"
httpWebResponse = DirectCast(httpWebRequest__1.GetResponse(), HttpWebResponse)
Dim responseStream As Stream = httpWebResponse.GetResponseStream()
Dim streamReader As New StreamReader(responseStream, m_encoding)
Dim html As String = streamReader.ReadToEnd()
streamReader.Close()
responseStream.Close()
currentTry -= 1
httpWebRequest__1.Abort()
httpWebResponse.Close()
Return html
Catch e As Exception
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") & e.Message)
Console.ForegroundColor = ConsoleColor.White
If currentTry <= m_maxTry Then
GetHtml(url, cookieContainer)
End If
currentTry -= 1
If httpWebRequest__1 IsNot Nothing Then
httpWebRequest__1.Abort()
End If
If httpWebResponse IsNot Nothing Then
httpWebResponse.Close()
End If
Return String.Empty
End Try
End Function
Public Shared Function GetHtml(url As String) As String
Return GetHtml(url, cc)
End Function
Public Shared Function GetHtml(ByVal url As String, ByRef postPara As System.Collections.Hashtable, ByVal isPost As Boolean) As String
Return GetHtml(url, ColToStr(postPara), isPost, cc)
End Function
Public Shared Function GetHtml(url As String, postData As String, isPost As Boolean) As String
Return GetHtml(url, postData, isPost, cc)
End Function
Public Shared Function GetStream(url As String) As Stream
Return GetStream(url, cc)
End Function
Public Shared Function GetStream(url As String, cookieContainer As CookieContainer) As Stream
'Thread.Sleep(delay);
currentTry += 1
Dim httpWebRequest__1 As HttpWebRequest = Nothing
Dim httpWebResponse As HttpWebResponse = Nothing
Try
httpWebRequest__1 = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
httpWebRequest__1.CookieContainer = cookieContainer
httpWebRequest__1.ContentType = contentType
httpWebRequest__1.ServicePoint.ConnectionLimit = m_maxTry
httpWebRequest__1.Referer = url
httpWebRequest__1.Accept = accept
httpWebRequest__1.UserAgent = userAgent
httpWebRequest__1.Method = "GET"
httpWebResponse = DirectCast(httpWebRequest__1.GetResponse(), HttpWebResponse)
Dim responseStream As Stream = httpWebResponse.GetResponseStream()
currentTry -= 1
'httpWebRequest.Abort();
'httpWebResponse.Close();
Return responseStream
Catch e As Exception
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") & e.Message)
Console.ForegroundColor = ConsoleColor.White
If currentTry <= m_maxTry Then
GetHtml(url, cookieContainer)
End If
currentTry -= 1
If httpWebRequest__1 IsNot Nothing Then
httpWebRequest__1.Abort()
End If
If httpWebResponse IsNot Nothing Then
httpWebResponse.Close()
End If
Return Nothing
End Try
End Function[/code] |
|