'--------------------------------------------------------------------------------
Sub Form1_Command1_BN_Clicked(hWndForm As hWnd, hWndControl As hWnd) '单击
Dim aa As Long
Dim hostname As String
Dim path As String
Dim ip As UInteger
Dim s As SOCKET
Dim sa As sockaddr_in
Dim sendbuffer As String
Dim recvbuffer As String
Dim tob As String
Dim bytes As Integer
Dim duiko As Long
'初始化WinSock
aa = doInit()
If aa Then
text2.text = "初始化 WinSock 失败!!" & Str(aa)
Return
End If
'获取主机名
'gethostname(@HostName,256)
'tob= "主机名: " & HostName
'获取主机信息
'sHostEnt=gethostbyname(@HostName)
'检查网址
tob = Trim(text1.text)
If Left(LCase(tob), 7) = "http://" Then
tob = Mid(tob, 8)
duiko = 80
ElseIf Left(LCase(tob), 8) = "https://" Then
tob = Mid(tob, 9)
duiko = 80
Else
duiko = 80
End If
Cls
Print Time & " 端口:" & duiko
getHostAndPath(tob, hostname, path)
If (Len(hostname) = 0) Then
text2.text = "网址无效" & Chr(13, 10) & text1.text
Return
End If
Print Time & " 域名:" & hostname
Print Time & " 路径:" & path
'解决名称
'Print hostname
ip = resolveHost(hostname)
If (ip = 0) Then
text2.text = "网址不能转换成IP" & Chr(13, 10) & hostname
Return
End If
Print Time & " 数字IP:" & ip
'' 打开 socket
Print Time & " 打开 socket" & ip
s = opensocket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
'af为协议地址族,这里我们使用IPv4,必须为AF_INET
'type为socket类型,如果使用TCP/IP,type设为SOCK_STREAM,如果使用UDP,则为SOCK_DGRAM
'protocol在给定的地址族及socket类型有多个入口的情况下用来限定特定的传输,对于TCP其值为IPPROTO_TCP,而UDP则为IPPROTO_UDP
If (s = 0) Then
reportError("打开socket")
Return
End If
'' 连接到主机
sa.sin_port = htons(duiko)
sa.sin_family = AF_INET
sa.sin_addr.S_addr = ip
Print Time & " 连接到主机", ip
If (connect(s, Cast(PSOCKADDR, @sa), SizeOf(sa)) = SOCKET_ERROR) Then
reportError("连接到主机connect()")
closesocket(s) '关闭socket
Return
End If
'' send HTTP 请求
sendBuffer = "GET /" + path + " HTTP/1.0" + Chr(13, 10) + _
"Host: " + hostname + Chr(13, 10) + _
"Connection: close" + Chr(13, 10) + _
"User-Agent: GetHTTP 0.0" + Chr(13, 10) + _
Chr(13, 10)
Print Time & " HTTP 请求:" & sendBuffer
If (send(s, sendBuffer, Len(sendBuffer), 0) = SOCKET_ERROR) Then
reportError("HTTP 请求 send()")
closesocket(s)
Return
End If
'' 接收直到连接关闭
Print Time & " 接收直到连接关闭"
Dim by(4097) As Byte
tob = ""
'recvbuffer=String(4096,0)
Do
bytes = recv(s, @by(0), 4096, 0)
If (bytes <= 0) Then
Exit Do
End If
recvbuffer = String(bytes, 0)
memcpy SAdd(recvbuffer), @by(0), bytes
'Peek(String,@by(0))
tob +
= Left(recvbuffer, bytes)
'' 打印缓冲区作为字符串
'Print *recvbuffer
Loop
Print Time & "完成"
If Check1.Value Then
tob = Utf8toStr(tob)
End If
'AfxMsg(Hex(StrPtr(tob)))
text2.text = tob
shutdown(s, 2) '关闭socket
closesocket(s) '关闭socket
WSACleanup '释放
End Sub
Sub reportError(ByRef msg As String) '显示错误
text2.text = "发生错误:" & msg & Chr(13, 10) & ": error #" & WSAGetLastError()
End Sub
Function doInit() As Long '初始化
'' init winsock
Dim wsaData As WSAData
Return WSAStartup(MAKEWORD(1, 1), @wsaData)
End Function
Sub getHostAndPath(ByRef src As String, ByRef hostname As String, ByRef path As String)
'分离网址与路径
Dim p As Integer = InStr(src, "/")
If p = 0 Then
hostname = Trim(src)
path = ""
Else
hostname = Trim(Left(src, p - 1))
path = Trim(Mid(src, p + 1))
End If
End Sub '======================================================
Function Unicodetoutf8(ByVal Pswzunicode As WString Ptr) As String
Dim Sutf8 As String
Sutf8 = String(Len( * Pswzunicode), 0)
WideCharToMultiByte(Cp_utf8, _ |