目的:通过VBS上传本地的文件到服务端。
服务端接受文件:upload_file.php
图形操作界面:upload.php
通过Wireshark抓包看到:VBS模拟Post的数据包和通过IE手工发送完全一样,但是服务器端就是不认,哪个地方出错了呢?代码如下:
VBScript code:
'''客户端文件upload.vbs Dim objFile,fileContent Set objFile = CreateObject("Adodb.Stream") objFile.Type = 2 objFile.Charset = "gb2312" objFile.Open objFile.LoadFromFile "C:\test.txt" Do While Not objFile.EOS fileContent = fileContent & objFile.ReadText Loop objFile.Close s = s & "-----------------------------7da2612fbe0662" s = s & Chr(13) & Chr(10) s = s & "Content-Disposition: form-data; name=" & Chr(34) & "file" & Chr(34) & "; filename=" & Chr(34) & "test.txt" & Chr(34) s = s & Chr(13) & Chr(10) s = s & "Content-Type: text/plain" s = s & Chr(13) & Chr(10) s = s & Chr(13) & Chr(10) s = s & read s = s & Chr(13) & Chr(10) s = s & "-----------------------------7da2612fbe0662" s = s & Chr(13) & Chr(10) s = s & "Content-Disposition: form-data; name=" & Chr(34) & "submit" & Chr(34) s = s & Chr(13) & Chr(10) s = s & Chr(13) & Chr(10) s = s & "Submit" s = s & Chr(13) & Chr(10) s = s & "-----------------------------7da2612fbe0662--" s = s & Chr(13) & Chr(10) Dim xmlhttp Set xmlhttp = CreateObject("MSXML2.XMLHTTP") 'Set xmlhttp = CreateObject("Microsoft.XMLHTTP") xmlhttp.Open "POST", "http://192.168.0.10:8080/upload_file.php", False xmlhttp.setRequestHeader "Content-Type", "multipart/form-data; boundary=-----------------------------7da2612fbe0662" 'xmlhttp.setRequestHeader "User-Agent", "Microsoft.XMLHTTP" 'xmlhttp.setRequestHeader "Referer", "http://192.168.0.10:8080/upload.php" 'xmlhttp.setRequestHeader "Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*" xmlhttp.send s WScript.Echo xmlhttp.responseText Set objFile = Nothing Set xmlhttp = Nothing
服务端:upload.php
HTML code:
enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form>
服务端:upload_file.php
PHP code:
<?php var_dump($_FILES); ?>
vbs上传数据包:
HTML code:
POST /upload_file.php HTTP/1.1 Accept: */* Accept-Language: zh-cn Content-Type: multipart/form-data; boundary=-----------------------------7da2612fbe0662 Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; QQDownload 667; TencentTraveler 4.0) Host: 192.168.0.10:8080 Content-Length: 294 Connection: Keep-Alive Cache-Control: no-cache -----------------------------7da2612fbe0662 Content-Disposition: form-data; name="file"; filename="test.txt" Content-Type: text/plain ffff -----------------------------7da2612fbe0662 Content-Disposition: form-data; name="submit" Submit -----------------------------7da2612fbe0662-- HTTP/1.1 200 OK Content-Type: text/html Server: Microsoft-IIS/6.0 X-Powered-By: PHP/5.1.2 X-Powered-By: ASP.NET Date: Mon, 27 Dec 2010 16:24:10 GMT Connection: close array(0) { } array(0) { } Upload: <br />Type: <br />Size: 0 Kb<br />Temp file: <br />