|
本帖最后由 xmyjk 于 2014-11-2 19:00 编辑
wcymiss 发表于 2014-11-1 17:13
上传文件
同样,上传文件也可以用fiddler抓包。
好东西,这让我想起了,当时想借good.gd做一个自动上传程序更新,然后分发的工具。
可惜后来烂尾了。不过文件上传模块倒是写好了,没想到现在还能用。
借花献佛了。
原理基本如吴姐所说。
xmlhttp等控件,如果要发送数据流,就不能使用文本格式的报文主体,但是VB中又不能直接把byte的字节形式的数组送给.send方法来发。
解决方法就是,用一个变体的变量来过度。
程序流程类似,就是我是把文件用open binary函数读入,前后字符串用strconv函数转成ansi的byte数组,然后再用copymemery连接数组。
后话:
在vb中如果发送字节数据,xmlhttp不会转码,就这么出去,如果发送的是文本,不符合规范的文本会自动转为按utf8进行编码再出去。所以gbk那些都要手工转码。
献丑了,当年的代码附件如下:
- Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
- Sub upload()
- Dim xmlhttp, sess As String, uid As String, b As String, i As Long, n As Long
- Dim fl() As Byte, senddata, flnm As String, aft() As Byte, bef() As Byte, tmp As String, arr() As Byte
- Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
- flnm = Format(Date, "yymmdd") & " " & Format(Time, "hh时mm分") & "bak.xls"
- ThisWorkbook.SaveCopyAs ThisWorkbook.Path & "" & flnm
- With xmlhttp
- .Open "GET", "http://upload.good.gd/upload.aspx", False
- .send
- sess = Split(Split(.responsetext, "var SESSIONID = '")(1), "'")(0)
- uid = Split(Split(.responsetext, "var UploadID = '")(1), "'")(0)
- b = getboundary
- Open ThisWorkbook.Path & "" & flnm For Binary As #1
- ReDim fl(LOF(1) - 1) As Byte
- Get #1, , fl()
- Close #1
- tmp = "Content-Disposition: form-data; name=""Filename""" & vbCrLf & vbCrLf & _
- flnm & vbCrLf & _
- "------------" & b & vbCrLf & _
- "Content-Disposition: form-data; name=""ASPSESSID""" & vbCrLf & vbCrLf & _
- sess & vbCrLf & _
- "------------" & b & vbCrLf & _
- "Content-Disposition: form-data; name=""Filedata""; filename=""" & flnm & """" & vbCrLf & _
- "Content-Type: application/octet-stream" & vbCrLf & vbCrLf
- bef = StrConv(tmp, vbFromUnicode)
- tmp = ""
- tmp = vbCrLf & "------------" & b & vbCrLf & _
- "Content-Disposition: form-data; name=""Upload""" & vbCrLf & vbCrLf & _
- "Submit Query" & vbCrLf & _
- "------------" & b & "--"
- aft = StrConv(tmp, vbFromUnicode)
- ReDim arr(UBound(bef) + 1 + UBound(aft) + 1 + UBound(fl)) As Byte
- CopyMemory arr(0), bef(0), UBound(bef) + 1
- CopyMemory arr(UBound(bef) + 1), fl(0), UBound(fl) + 1
- CopyMemory arr(UBound(bef) + 1 + UBound(fl) + 1), aft(0), UBound(aft) + 1
- senddata = arr
- .Open "POST", "http://upload.good.gd/Receive/ReceiveData.aspx?c=uploadgood&flash=true&uid=" & uid, False
- .setRequestHeader "Content-Type", "multipart/form-data; boundary=" & "----------" & b
- .send senddata
- [b2] = "http://good.gd/" & Split(.responsetext, "|")(1) & ".htm"
- End With
- Kill ThisWorkbook.Path & "" & flnm
- End Sub
- Function getboundary() As String
- Dim i&
- For i = 1 To 10
- getboundary = getboundary & Chr(Int(Rnd * 26) + 65) & Chr(Int(Rnd * 26) + 97)
- getboundary = getboundary & Chr(Int(Rnd * 10) + 48)
- Next
- End Function
复制代码
good网页备份文件.zip
(16.35 KB, 下载次数: 103)
|
评分
-
1
查看全部评分
-
|