如何用VB发送如下HTTP协议头
GET / HTTP/1.1
Accept: */*
Referer: http://www.orsoon.com/
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Host: www.wmzhe.com
Connection: Keep-Alive
这只是个例子
我想做成一个软件,有一个textbox,一个发送按钮
把这段头文件复制进文本框,点发送,文件提交
头部信息我可以自己修改
谁能给个完整的代码啊
[解决办法]
请参考下面代码:
- VB code
Private Sub GetfromInet(strURL As String, Optional strProxy As String) Dim SocketBuffer As sockaddr Dim IpAddr As Long Dim SlashPos As Integer Dim strPath As String Dim strHost As String Dim tmpHost As String Dim intPort As Integer Dim RC As Long Dim strMsg As String 'Separate URL into Host and Path SlashPos = InStr(8, strURL, "/") If SlashPos = 0 Then SlashPos = Len(strURL) + 1 strPath = Mid$(strURL, SlashPos) If strPath = "" Then strPath = "/" strHost = Mid$(strURL, 8, SlashPos - 8) If strProxy <> "" Then 'There is a Proxy tmpHost = "http://" & strHost strHost = Mid$(strProxy, 1, InStr(1, strProxy, ":") - 1) intPort = CInt(Mid$(strProxy, InStr(1, strProxy, ":") + 1)) Else 'No Proxy intPort = 80 End If 'Start winsock Call StartWinsock 'Create socket hSock = socket(AF_INET, SOCK_STREAM, 0) If hSock = SOCKET_ERROR Then Exit Sub IpAddr = GetHostByNameAlias(strHost) If IpAddr = -1 Then 'Err.Raise vbObjectError + 1, , "Unknown host" Exit Sub End If With SocketBuffer .sin_family = AF_INET .sin_port = htons(intPort) .sin_addr = IpAddr .sin_zero = String$(8, 0) End With DoEvents 'Connect to server RC = connect(hSock, SocketBuffer, Len(SocketBuffer)) If RC = SOCKET_ERROR Then closesocket hSock Call EndWinsock Err.Raise vbObjectError + 1, , "Could not connect to " & strHost Exit Sub Else End If DoEvents 'Set receive window RC = WSAAsyncSelect(hSock, Me.hwnd, ByVal &H202, ByVal FD_READ Or FD_CLOSE) If RC = SOCKET_ERROR Then closesocket hSock Call EndWinsock Exit Sub End If 'Prepare GET header 'When to use GET? -> When the amount of data that you 'need to pass to the server is not much strMsg = "GET " & tmpHost & strPath & " HTTP/1.0" & vbCrLf strMsg = strMsg & "Accept: */*" & vbCrLf' strMsg = strMsg & "Accept-Language: zh-cn" & vbCrLf' strMsg = strMsg & "Accept-Encoding: gzip , deflate" & vbCrLf strMsg = strMsg & "User-Agent: " & App.Title & vbCrLf strMsg = strMsg & "Host: " & strHost & vbCrLf strMsg = strMsg & vbCrLf 'Example POST header 'When to use POST? -> Anytime, it is failsafe since the 'content-length is passed to the server everytime 'strCommand = "Temp1=hello&temp2=12345&Etc=hallo" 'strMsg = "POST " & tmpHost & strPath & " HTTP/1.0" & vbCrLf 'strMsg = strMsg & "Accept: */*" & vbCrLf 'strMsg = strMsg & "User-Agent: " & App.Title & vbCrLf 'strMsg = strMsg & "Host: " & strHost & vbCrLf 'strMsg = strMsg & "Content-Type: application/x-www-form-urlencoded" & vbCrLf 'strMsg = strMsg & "Content-Length: " & Len(strCommand) & vbCrLf 'strMsg = strMsg & vbCrLf & strCommand ' lblStatus = "Sending request..." DoEvents 'Send request SendData hSock, strMsg If tmpHost = "" Then tmpHost = strHost 'Wait for page to be downloaded 'Seconds to wait = 10 Dim Start As Integer Start = (Format$(Now, "NN") * 60 + Format$(Now, "SS")) + 10 While Not Start <= (Format$(Now, "NN") * 60 + Format$(Now, "SS")) And hSock > 0 'hlblStatus = "Waiting for response from " & tmpHost & "..." & Start - (Format$(Now, "NN") * 60 + Format$(Now, "SS")) DoEvents 'You can put a routine that will check if a boolean variable is True here 'This could indicate that the request has been canceled 'If CancelFlag = True Then ' lblStatus = "Cancelled request" ' Exit Sub 'End If WendEnd Sub