OK, here is a routine that you can use for posting to a webserver. For it to work, you embed the routine in your form. You also need two controls on your form. These are :(1) A Winsock control named Winsock and having an index property of 0. (2)A timer control named TimerWinsock. Set its Enabled property to false.as well as one global variable to be able to receive message from your winsock control. This variable is named WinsockRec$ and you can define it in either a module or in your form as :Public WinsockRec$The PostResult subroutine below takes 4 parameters as follows :Host$ - A string variable representing the host that you want to connect to. It can be either a FQDN for example http://www.yahoo.com/ or an IP address like 196.15.11.22Q$ - The Data that you wish to post. This normally contains your form data and to illustrate it, let's take and example. Say you have a form as follows:<FORM> <INPUT TYPE=HIDDEN NAME="X" VALUE="1234"> <INPUT SIZE=10 NAME="Z" VALUE="Hello"> <TEXTAREA NAME="Y" ROWS=5 COLS=10>Type it here</TEXTAREA> </FORM>Then Q$ should look as follows : X=1234&Z=Hello&Y=Type%20it%20hereDoc: The name of the script that you want to post to. For example, if you want to post to http://www.acme.com/cgi-bin/doit.sh, then Doc would be set to "/cgi-bin/doit.sh"Result - This is a parameter that is returned to your function. It contains the response of the remote script once you have posted your data. You will see that the result is the raw information that the webserver passes back, not just HTML.The function returns a boolean value that indicates that the post was successful (true) or unsuccessful (false).'--------------------------------- Private Function PostResult(ByVal Host$, ByVal Q$, ByVal Doc As String, ByRef Result As String) As Boolean ' This function uses the embedded Winsock control to ' send POST type data to a document. The Data to post is passed ' in Q$, the document to post to in Doc and the result in Result. ' The function returns a boolean to indicate success or failure.
On Local Error Resume Next
Dim Z$
If Doc = "" Then PostResult = False Exit Function End If Load Winsock(1) Err = 0 Winsock(1).RemoteHost = Host$ Winsock(1).RemotePort = 80 TimerWinsock.Interval = TimeoutValue TimerWinsock.Enabled = True Winsock(1).Connect While TimerWinsock.Enabled And (Winsock(1).State <> sckConnected) DoEvents Wend TimerWinsock.Enabled = False If Winsock(1).State = sckConnected Then Z$ = "POST " & "http://" & Host$ & Doc & " HTTP/1.0" & vbCrLf Z$ = Z$ & "Content-Type: application/x-www-form-urlencoded" & vbCrLf Z$ = Z$ & "Content-Length: " & Format$(Len(Q$)) & vbCrLf Z$ = Z$ & "Connection: Keep-Alive" & vbCrLf & vbCrLf Z$ = Z$ & Q$ & vbCrLf TimerWinsock.Interval = TimeoutValue WinsockRec$ = "" TimerWinsock.Enabled = True Winsock(1).SendData Z$ While (Winsock(1).State <> sckClosing) And TimerWinsock.Enabled DoEvents Wend TimerWinsock.Enabled = False If Winsock(1).State = sckClosing Then Winsock(1).Close Result = WinsockRec$ Unload Winsock(1) PostResult = True Exit Function End If Winsock(1).Close Unload Winsock(1) PostResult = False Else Winsock(1).Close Unload Winsock(1) PostResult = False End IfEnd Function '-------------------------------You further need two snippets of event handling code. The first of these is on the TimerWinsock_Timer() event which is simply as follows :'------------------------------- Private Sub TimerWinsock_Timer() TimerWinsock.Enabled = False End Sub '-------------------------------and on your Winsock_DataArrival(Index as Integer) event as follows :'------------------------------------ Private Sub Winsock_DataArrival(Index AS Integer, ByVal bytesTotal AS Long) On Local Error Resume Next Dim Q$ Err = 0 While (Winsock(Index).BytesReceived > 0) AND (Err=0) Winsock(Index).GetData Q$,vbString,bytesTotal WinsockRec$ = WinsockRec$ & Q WEnd End Sub 这是我从国外一个论坛找来的,大家讨论啊 可以把代码那去试,谈谈心得?????
如果你了解http协议, 那么这个问题就很容易解决, 建议楼主去看看有关http协议之类的东东 可以先自己用winsock做个服务端, 看看提交过来的页面的内容. 如: private sub form_load() winsock1.localport=80 winsock1.listen end sub private sub wisock1_connectrequest(.....) if winsock1.state<> wskclosed then winsock1.close winsock1.accept requestid end sub private sub winsock1_dataarrival(totals as long) dim str as string winsock1.getdata str debug.print str 'str中的内容即为提交过来的表单数据, 在下次提交时就可以按照这些格式来提交到指定的页面了 end sub以上代码未测试, 以前做过类似的, 仅供参考
一般来说INET更稳定,效率也高,不过写的好的winsock比他要好的多,因为他灵活
从学习的角度来说,应该尝试用winsock
[email protected]
webbrowser1.Navigate "xxxx.asp?username=sportdog&pass=123"
用的是get方法,用post方法我就不会了,搂主解决了,通知一声好吗
<INPUT TYPE=HIDDEN NAME="X" VALUE="1234">
<INPUT SIZE=10 NAME="Z" VALUE="Hello">
<TEXTAREA NAME="Y" ROWS=5 COLS=10>Type it here</TEXTAREA>
</FORM>Then Q$ should look as follows :
X=1234&Z=Hello&Y=Type%20it%20hereDoc: The name of the script that you want to post to. For example, if you want to post to http://www.acme.com/cgi-bin/doit.sh, then Doc would be set to "/cgi-bin/doit.sh"Result - This is a parameter that is returned to your function. It contains the response of the remote script once you have posted your data. You will see that the result is the raw information that the webserver passes back, not just HTML.The function returns a boolean value that indicates that the post was successful (true) or unsuccessful (false).'---------------------------------
Private Function PostResult(ByVal Host$, ByVal Q$, ByVal Doc As String, ByRef Result As String) As Boolean ' This function uses the embedded Winsock control to
' send POST type data to a document. The Data to post is passed
' in Q$, the document to post to in Doc and the result in Result.
' The function returns a boolean to indicate success or failure.
On Local Error Resume Next
Dim Z$
If Doc = "" Then
PostResult = False
Exit Function
End If
Load Winsock(1)
Err = 0
Winsock(1).RemoteHost = Host$
Winsock(1).RemotePort = 80
TimerWinsock.Interval = TimeoutValue
TimerWinsock.Enabled = True
Winsock(1).Connect
While TimerWinsock.Enabled And (Winsock(1).State <> sckConnected)
DoEvents
Wend
TimerWinsock.Enabled = False
If Winsock(1).State = sckConnected Then
Z$ = "POST " & "http://" & Host$ & Doc & " HTTP/1.0" & vbCrLf
Z$ = Z$ & "Content-Type: application/x-www-form-urlencoded" & vbCrLf
Z$ = Z$ & "Content-Length: " & Format$(Len(Q$)) & vbCrLf
Z$ = Z$ & "Connection: Keep-Alive" & vbCrLf & vbCrLf
Z$ = Z$ & Q$ & vbCrLf
TimerWinsock.Interval = TimeoutValue
WinsockRec$ = ""
TimerWinsock.Enabled = True
Winsock(1).SendData Z$
While (Winsock(1).State <> sckClosing) And TimerWinsock.Enabled
DoEvents
Wend
TimerWinsock.Enabled = False
If Winsock(1).State = sckClosing Then
Winsock(1).Close
Result = WinsockRec$
Unload Winsock(1)
PostResult = True
Exit Function
End If
Winsock(1).Close
Unload Winsock(1)
PostResult = False
Else
Winsock(1).Close
Unload Winsock(1)
PostResult = False
End IfEnd Function
'-------------------------------You further need two snippets of event handling code. The first of these is on the TimerWinsock_Timer() event which is simply as follows :'-------------------------------
Private Sub TimerWinsock_Timer()
TimerWinsock.Enabled = False
End Sub
'-------------------------------and on your Winsock_DataArrival(Index as Integer) event as follows :'------------------------------------
Private Sub Winsock_DataArrival(Index AS Integer, ByVal bytesTotal AS Long) On Local Error Resume Next Dim Q$ Err = 0
While (Winsock(Index).BytesReceived > 0) AND (Err=0)
Winsock(Index).GetData Q$,vbString,bytesTotal
WinsockRec$ = WinsockRec$ & Q
WEnd
End Sub
这是我从国外一个论坛找来的,大家讨论啊 可以把代码那去试,谈谈心得?????
如果用Winsock,在服务端需要写接收处理程序。
客户端发送数据时,使用TLV格式或者定长记录格式;服务端分解数据,写入数据库。
比较可靠的方法是发送一个至数个字节的EDC码,服务端如果验证通过,回送ERR_SUCCESS;否则回送错误码(可以自定义)。
客户端接收到ERR_SUCCESS就确认成功了。
webbrowser1.Navigate "xxxx.asp?username=sportdog&pass=123"
一些不是用post 传送数据的!!!!!
xxxx.asp?username=sportdog&pass=123
以这样方法太不安全了!!!呵呵不过!!!
另一种方法的程序我不会写!!!!!!!!
不知道那位兄弟知道!!!!!
可以先自己用winsock做个服务端, 看看提交过来的页面的内容. 如:
private sub form_load()
winsock1.localport=80
winsock1.listen
end sub
private sub wisock1_connectrequest(.....)
if winsock1.state<> wskclosed then winsock1.close
winsock1.accept requestid
end sub
private sub winsock1_dataarrival(totals as long)
dim str as string
winsock1.getdata str
debug.print str 'str中的内容即为提交过来的表单数据, 在下次提交时就可以按照这些格式来提交到指定的页面了
end sub以上代码未测试, 以前做过类似的, 仅供参考
winsock1.listen80端口已被占用了啊,会报错吧。
----------------------------------
POST /xxxx.asp HTTP/1.1
Host: 127.0.0.1
User-Agent: XXXX
Content-Type: application/x-www-form-urlencoded
Content-Length: xxxxusername=xxxx&password=xxxx
-----------------------------------
/xxxx.asp 相当于表单的 action="/xxxx.asp"Content-Length 是 "username=xxxx&password=xxxx" 的字节长度
还有 content-length 后面应该接两个换行再到 user....
还有 username= 和password= 后面的数据要用 url 编码