Winsock, hentning af kildekode
Har den her kode, hvor jeg vil hente kildekoden fra newz.dk og så bagefter sammenligne linksene i kildekoden med tidligere hentet kildekode og så udskrive de nye links, som er kommet, men det ser ikke ud til winsock virker, jeg kan i hvert fald ikke få skidtet til at virke.På forhånd tak!
Option Explicit
Dim ref1 As String
Dim ref2 As String
Dim i As Integer
Dim n As Integer
Dim t As Integer
Dim links1 As Variant
Dim links2 As Variant
Dim newlinks() As String
Dim foundit As Boolean
Dim re As New RegExp
Private Sub Command1_Click()
SetRefs
FindLinks
CompareURLs
End Sub
Private Sub SetRefs()
Dim m_strRemoteHost As String 'the web server to connect to
Dim m_strFilePath As String 'relative path to the file to retrieve
Dim m_strHttpResponse As String 'the server response
Dim m_bResponseReceived As Boolean
m_strRemoteHost = "http://www.newz.dk"
m_strFilePath = "http://www.newz.dk"
m_strHttpResponse = ""
m_bResponseReceived = False
With wscHttp
.Close
.LocalPort = 0
.Connect m_strRemoteHost, 80
End With
End Sub
Private Sub wscHttp_Connect()
Dim strHttpRequest As String
strHttpRequest = "GET " & m_strFilePath & " HTTP/1.1" & vbCrLf
strHttpRequest = strHttpRequest & "Host: " & m_strRemoteHost & vbCrLf
strHttpRequest = strHttpRequest & "Accept: */*" & vbCrLf
strHttpRequest = strHttpRequest & "Connection: close" & vbCrLf
strHttpRequest = strHttpRequest & vbCrLf
wscHttp.SendData strHttpRequest
End Sub
Private Sub wscHttp_DataArrival(ByVal bytesTotal As Long)
On Error Resume Next
Dim strData As String
wscHttp.GetData strData
m_strHttpResponse = m_strHttpResponse & strData
End Sub
Private Sub wscHttp_Close()
If Not m_bResponseReceived Then
m_strHttpResponse = Mid(m_strHttpResponse, _
InStr(1, m_strHttpResponse, _
vbCrLf & vbCrLf) + 4)
ref1 = m_strHttpResponse
m_bResponseReceived = True
End If
End Sub
Private Sub Command2_Click()
SetRefs
End Sub
Private Sub Form_Load()
ReDim newlinks(0)
'ref1 = "Lorem ipsum. <a href=""http://www.hej.com"">hej.com</a>. Lorem."
ref2 = "Lorem ipsum. <a href=""http://www.hej.com"">hej.com</a><a href=""http://www.nesdafj.com"">neja.com</a>. Lorem."
End Sub
Private Sub CompareURLs()
For i = 0 To links2.Count - 1
foundit = False
For n = 0 To links1.Count - 1
If links2(i) = links1(n) Then
foundit = True
End If
Next n
If foundit = False Then
If newlinks(0) <> "" Then
ReDim Preserve newlinks(UBound(newlinks) + 1)
Else
ReDim Preserve newlinks(0)
End If
newlinks(UBound(newlinks)) = links2(i)
End If
Next i
Popup
End Sub
Private Sub FindLinks()
Set re = New RegExp
re.Global = True
re.Pattern = "<a[^<]*</a>"
Set links1 = re.Execute(ref1)
Set links2 = re.Execute(ref2)
End Sub
Private Sub Popup()
For t = 0 To UBound(newlinks)
MsgBox ("hej: " & newlinks(t))
Next t
End Sub
