用winsock模拟html的post提交上传文件到服务器
  
  
 
A :
        
        
        
一个窗体,一个command1按钮开始发送,一个text1文本框输入文件位置,一个label1显示发送状态。服务器端asp接收程序:  化境编程界文件上传  2.0。  
VB代码如下:  
---------------------------------------------------------------------------  
Private  Sub  Command1_Click()  
        Me.Winsock1.RemoteHost  =  "127.0.0.1"  
        Me.Winsock1.RemotePort  =  80  
        Me.Winsock1.Connect  
        Me.Label1  =  "Connecting..."  
        DoEvents  
        tmr  =  Timer  
        Do  Until  Me.Winsock1.State  =  7  
                DoEvents  
                If  Timer  -  tmr  >=  10  Then  
                        MsgBox  "Connection  Timeout.",  vbOKOnly,  "Error"  
                        Me.Winsock1.Close  
                        Exit  Sub  
                End  If  
        Loop  
        Me.Label1  =  "Ready  to  send..."  
          
          
        strhttpheader  =  strhttpheader  &  "POST  /example/upfile.asp  HTTP/1.1"  &  vbCrLf  
        strhttpheader  =  strhttpheader  &  "Accept:  image/gif,  image/x-xbitmap,  image/jpeg,  image/pjpeg,  application/x-shockwave-flash,  application/vnd.ms-excel,  application/vnd.ms-powerpoint,  application/msword,  */*"  &  vbCrLf  
        strhttpheader  =  strhttpheader  &  "Referer:  http://127.0.0.1/example/upfile.htm"  &  vbCrLf  
        strhttpheader  =  strhttpheader  &  "Accept-Language:  zh-cn"  &  vbCrLf  
        strhttpheader  =  strhttpheader  &  "Content-Type:  multipart/form-data;  boundary=---------------------------7d32b5e2406c6"  &  vbCrLf  
        strhttpheader  =  strhttpheader  &  "Accept-Encoding:  gzip,  deflate"  &  vbCrLf  
        strhttpheader  =  strhttpheader  &  "Host:  127.0.0.1:80"  &  vbCrLf  
        strhttpheader  =  strhttpheader  &  "User-Agent:  Mozilla/4.0  (compatible;  MSIE  6.0;  Windows  NT  5.0;  .NET  CLR  1.1.4322)"  &  vbCrLf  
        strhttpheader  =  strhttpheader  &  "Connection:  Keep-Alive"  &  vbCrLf  
        strhttpheader  =  strhttpheader  &  "Cache-Control:  no-cache"  &  vbCrLf  
          
          
        strbody  =  strbody  &  "---------------------------7d32b5e2406c6"  &  vbCrLf  
        strbody  =  strbody  &  "Content-Disposition:  form-data;  name=""act"""  &  vbCrLf  
        strbody  =  strbody  &  ""  &  vbCrLf  
        strbody  =  strbody  &  "upload"  &  vbCrLf  
        strbody  =  strbody  &  "---------------------------7d32b5e2406c6"  &  vbCrLf  
        strbody  =  strbody  &  "Content-Disposition:  form-data;  name=""upcount"""  &  vbCrLf  
        strbody  =  strbody  &  ""  &  vbCrLf  
        strbody  =  strbody  &  "1"  &  vbCrLf  
        strbody  =  strbody  &  "---------------------------7d32b5e2406c6"  &  vbCrLf  
        strbody  =  strbody  &  "Content-Disposition:  form-data;  name=""filepath"""  &  vbCrLf  
        strbody  =  strbody  &  ""  &  vbCrLf  
        strbody  =  strbody  &  "."  &  vbCrLf  
        strbody  =  strbody  &  "---------------------------7d32b5e2406c6"  &  vbCrLf  
        strbody  =  strbody  &  "Content-Disposition:  form-data;  name=""file1""  filename="""  &  Me.Text1  &  """"  &  vbCrLf  
        strbody  =  strbody  &  "Content-Type:  application/octet-stream"  &  vbCrLf  
        strbody  =  strbody  &  ""  &  vbCrLf  
          
        strbody2  =  "---------------------------7d32b5e2406c6"  &  vbCrLf  
        strbody2  =  strbody2  &  "Content-Disposition:  form-data;  name=""Submit"""  &  vbCrLf  
        strbody2  =  strbody2  &  ""  &  vbCrLf  
        strbody2  =  strbody2  &  "·  提交  ·"  &  vbCrLf  
        strbody2  =  strbody2  &  "---------------------------7d32b5e2406c6--"  &  vbCrLf  &  vbCrLf  
      
        strhttpheader  =  strhttpheader  &  "Content-Length:  "  &  CLng(Len(strbody)  +  Len(strbody2)  +  FileLen(Me.Text1))  &  vbCrLf  &  vbCrLf  
          
          
        Me.Winsock1.SendData  strhttpheader  &  strbody  
        DoEvents  
          
          
        Me.Label1  =  "Sending  file  data..."  
        DoEvents  
        Dim  bytBuff(8191)  As  Byte  
        Dim  bytRem()  As  Byte  
        Open  Me.Text1.Text  For  Binary  As  #1  
                times  =  Int(LOF(1)  /  8192)  
                ReDim  bytRem(LOF(1)  Mod  8192  -  1)  
                For  i  =  1  To  times  
                        Get  #1,  ,  bytBuff  
                        Me.Winsock1.SendData  bytBuff  
                        DoEvents  
                Next  
                Get  #1,  ,  bytRem  
                Me.Winsock1.SendData  bytRem  
                DoEvents  
        Close  #1  
  
          
          
        Me.Winsock1.SendData  vbCrLf  &  strbody2  
        DoEvents  
          
        Me.Label1  =  "File  was  sent  successfully."  
        DoEvents  
        Me.Winsock1.Close  
          
End  Sub   
       
本文版权:
http://www.ndfweb.cn/news-293.html