您的位置:首页 > 理论基础 > 计算机网络

HttpWebRequest post "multipart/form-data"类型的web数据

2008-07-16 15:34 519 查看
webRqst.ContentType   =   "multipart/form-data;   boundary=xyz";  
  所以数据提交部份实际上是从你的boundary也就是  
  -----------------------------7d42bf2030536开始到结束。  
  以下是一个例子:  
   
  System.Uri   uri   =   new   Uri(msp_url);    
  string   dataBoundary   =   "--xyz";    
  System.Net.HttpWebRequest   webRqst   =   (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(uri);    
   
  //add   existing   cookies   to   the   webrequest.    
  webRqst.CookieContainer   =   new   CookieContainer();    
  webRqst.CookieContainer.Add(mspCookies);    
   
  //create   the   WebRequest   Content-Body   manually   for   file   upload.    
  webRqst.ContentType   =   "multipart/form-data;   boundary=xyz";    
  webRqst.Method   =   "POST";    
  webRqst.KeepAlive   =   true;    
   
  //get   the   file   content   that   will   be   uploaded.    
  StreamReader   sr   =   new   StreamReader(filepath);    
  string   FileData   =   sr.ReadToEnd();   //   test   data   to   send.    
  sr.Close();    
   
  //make   a   string   to   store   the   content.    
  StringBuilder   DataString   =   new   StringBuilder();    
   
  DataString.Append(dataBoundary   +   "/r/n");    
  DataString.Append("Content-Disposition:   form-data;   name=/"filename/"/r/n/r/n"   +   "test.txt/r/n");    
   
  DataString.Append(dataBoundary   +   "/r/n");    
  DataString.Append("Content-Disposition:   form-data;   name="   +   "/"submitFile/""   +    
  ";   filename="   +   "/""   +   filename   +   "/""   +   "/r/n");    
  DataString.Append("Content-Type:   text/plain/r/n/r/n");    
  DataString.Append(FileData);    
  DataString.Append(dataBoundary   +   "--/r/n");    
   
  byte   []Postdata   =   System.Text.Encoding.Default.GetBytes(DataString.ToString());    
   
  //change   the   requests   content   length   to   the   size   of   data   being   posted   in   request.    
  webRqst.ContentLength   =   Postdata.Length;    
   
  //get   the   response   stream   from   the   webrequest   and   write   the   data   to   be   posted   to   the    
  //Request   Stream    
   
  Stream   tempStream   =   webRqst.GetRequestStream();    
  tempStream.Write(Postdata,0,Postdata.Length);    
  tempStream.Flush();    
  tempStream.Close();    
   
   
  //change   certificate   policy   to   always   allow.    
  System.Net.ServicePointManager.CertificatePolicy   =   new   MyPolicy();    
  System.Net.HttpWebResponse   webRsp   =   (System.Net.HttpWebResponse)webRqst.GetResponse();    

[抄录]http://topic.csdn.net/t/20040621/10/3109172.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐