有没有分析源代码和URL的方法?谢谢!
解决方案 »
- C# FTP 求救
- 如何给自定义Dll文件生成元数据文件并查看
- socket的一个问题还是不明白啊
- 有没有Winform 下编写自定义控件的好书??
- DataGrid
- ThreadPool.GetMaxThreads
- 请大家推荐一个好的自定义报表工具或控件.
- 抓网页老抓不全
- 关于C#截取字符串的问题
- Page.RegisterStartupScript("","<script>if(confirm('您选择的模板项目有:\n"+ Session["Moban"] +"您确认吗?')){alert('dsfsdf')}</s
- 请教:网页对话框接收从其他网页传过来的中文数据是乱码,怎么解决?
- 救命.真的是人命关天
Imports System.Threading
Imports System.Text
Imports System.Net
Imports System.IO' The RequestState class is used to pass data
' across async calls
'Alpha Wu:[email protected]
Public Class RequestState
Public RequestData As New StringBuilder("")
Public BufferRead(1024) As Byte
Public Request As HttpWebRequest
Public ResponseStream As Stream
' Create Decoder for appropriate encoding type
' Public StreamDecode As Encoding = Encoding.GetEncoding("gb2312")
Public StreamDecode As Decoder = Encoding.GetEncoding("gb2312").GetDecoder Public Sub New()
Request = Nothing
ResponseStream = Nothing
End Sub
End Class' ClientGetAsync issues the async request
Public Class SourceCodeBuilder
Private allDone As New ManualResetEvent(False)
Const BUFFER_SIZE As Integer = 1024
Private iUrlSourceCode As String Private Function Urlcode(ByVal str As String)
Dim enGb As Encoding = Encoding.GetEncoding("gb2312")
Dim i As Integer = 0
Dim iValue As String
For i = 0 To str.Length - 1
Dim StrTemp As String = str.Substring(i, 1)
Dim TempString As String
Dim iLen As Integer = Encoding.Default.GetByteCount(StrTemp)
Dim Bytes() As Byte = enGb.GetBytes(StrTemp)
If iLen = 2 Then
TempString = "%" + Hex(Bytes(0)).ToString + "%" + Hex(Bytes(1)).ToString
Else Select Case Hex(Bytes(0))
Case 20
TempString = "%" + Hex(Bytes(0)).ToString
Case Else
TempString = StrTemp
End Select
End If
iValue = iValue + TempString
Next
Return iValue
End Function'调用此函数取得网页源码
Public Function UrlSourceCode(ByVal url As String) As String
url = Urlcode(url)
Dim iValue As String = "Falied"
Try
Dim HttpSite As Uri = New Uri(url, False)
UseThreading(HttpSite)
iValue = iUrlSourceCode
Catch ex As WebException
iValue = ex.Status
End Try
Return iValue
End Function Private Sub UseThreading(ByVal httpsite As Uri)
Try ' Get the URI from the command line ' Create the request object
Dim wreq As HttpWebRequest = CType(WebRequest.Create(httpsite), HttpWebRequest) ' Create the state object
Dim rs As RequestState = New RequestState ' Add the request into the state so it can be passed around
rs.Request = wreq ' Issue the async request
Dim r As IAsyncResult = CType(wreq.BeginGetResponse(New AsyncCallback(AddressOf RespCallback), rs), IAsyncResult) ' Set the ManualResetEvent to Wait so that the app
' doesn't exit until after the callback is called
allDone.WaitOne()
Catch
End Try End Sub Private Sub RespCallback(ByVal ar As IAsyncResult) ' Get the RequestState object from the async result
Dim rs As RequestState = CType(ar.AsyncState, RequestState) ' Get the HttpWebRequest from RequestState
Dim req As HttpWebRequest = rs.Request ' Calling EndGetResponse produces the HttpWebResponse object
' which came from the request issued above
Dim resp As HttpWebResponse
Dim ResponseStream As Stream
resp = CType(req.EndGetResponse(ar), HttpWebResponse)
' Now that we have the response, it is time to start reading
' data from the response stream
ResponseStream = resp.GetResponseStream() ' The read is also done using async so we'll want
' to store the stream in RequestState
rs.ResponseStream = ResponseStream ' Note that rs.BufferRead is passed in to BeginRead. This is
' where the data will be read into.
Dim iarRead As IAsyncResult = ResponseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, New AsyncCallback(AddressOf ReadCallBack), rs) End Sub Private Sub ReadCallBack(ByVal asyncResult As IAsyncResult)
Try
' Get the RequestState object from the asyncresult
Dim rs As RequestState = CType(asyncResult.AsyncState, RequestState) ' Pull out the ResponseStream that was set in RespCallback
Dim responseStream As Stream = rs.ResponseStream ' At this point rs.BufferRead should have some data in it.
' Read will tell us if there is any data there
Dim read As Integer = responseStream.EndRead(asyncResult)
If read > 0 Then
' Prepare Char array buffer for converting to Unicode
Dim charBuffer(1024) As Char ' Convert byte stream to Char array and then String
' len shows how many characters are converted to Unicode
Dim len As Integer = rs.StreamDecode.GetChars(rs.BufferRead, 0, read, charBuffer, 0)
Dim str As String = New String(charBuffer, 0, len) ' Append the recently read data to the RequestData stringbuilder object
' contained in RequestState
rs.RequestData.Append(str) ' Now fire off another async call to read some more data
' Note that this will continue to get called until
' responseStream.EndRead returns -1
Dim ar As IAsyncResult = responseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, New AsyncCallback(AddressOf ReadCallBack), rs)
Else
If rs.RequestData.Length > 1 Then
' All of the data has been read, so display it to the console
iUrlSourceCode = rs.RequestData.ToString()
End If ' Close down the response stream
responseStream.Close() ' Set the ManualResetEvent so the main thread can exit
allDone.Set()
End If Return
Catch ex As Exception
iUrlSourceCode = "Falied"
End Try
End Sub
End Class