C# winform 上傳文件 (多種方案) 2

//文件的內容

-----------------------8c64f47716481f0

這時候,我們只需自己編碼來模擬這麼一組數據就行(我們還可以好好借鑑MS的代碼呢),以下就是代碼(聲明一下,我是借用了別人的代碼)
public class wwHttp
{

 ///<summary>
 /// Fires progress events when using GetUrlEvents() to retrieve aURL.
 /// </summary>
 public event OnReceiveDataHandler OnReceiveData;

 ///<summary>
 /// Determines how data is POSTed when cPostBuffer is set.
 /// 1 - UrlEncoded
 /// 2 - Multi-Part form vars
 /// 4 - XML (raw buffer content type: text/xml)
 /// </summary>
 public int PostMode
 {
  get { return this.nPostMode; }
  set { this.nPostMode = value; }
 }

 ///<summary>
 ///  User name used for Authentication.
 ///  To use the currently logged in user when accessing anNTLM resource you can use "AUTOLOGIN".
 /// </summary>
 public string Username
 {
  get { return this.cUsername; }
  set { cUsername = value; }
 }

 ///<summary>
 /// Password for Authentication.
 /// </summary>
 public string Password
 {
  get {return this.cPassword;}
  set {this.cPassword = value;}
 }

 ///<summary>
 /// Address of the Proxy Server to be used.
 /// Use optional DEFAULTPROXY value to specify that you want toIE's Proxy Settings
 /// </summary>
 public string ProxyAddress  
 {
  get {return this.cProxyAddress;}
  set {this.cProxyAddress = value;}
 }

 ///<summary>
 /// Semicolon separated Address list of the servers the proxy isnot used for.
 /// </summary>
 public string ProxyBypass
 {
  get {return this.cProxyBypass;}
  set {this.cProxyBypass = value;}
 }

 ///<summary>
 /// Username for a password validating Proxy. Only used if the proxyinfo is set.
 /// </summary>
 public string ProxyUsername
 {
  get {return this.cProxyUsername;}
  set {this.cProxyUsername = value;}
 }
 /// <summary>
 /// Password for a password validating Proxy. Only used if theproxy info is set.
 /// </summary>
 public string ProxyPassword
 {
  get {return this.cProxyPassword;}
  set {this.cProxyPassword = value;}
 }

 ///<summary>
 /// Timeout for the Web request in seconds. Times out onconnection, read and send operations.
 /// Default is 30 seconds.
 /// </summary>
 public int Timeout
 {
  get {return this.nConnectTimeout; }
  set {this.nConnectTimeout = value; }
 }

 ///<summary>
 /// Error Message if the Error Flag is set or an error value isreturned from a method.
 /// </summary>
 public string ErrorMsg
 {
  get { return this.cErrorMsg; }
  set { this.cErrorMsg = value; }
 }

 /// <summary>
 /// Error flag if an error occurred.
 /// </summary>
 public bool Error
 {
  get { return this.bError; }
  set { this.bError = value; }
 }

 ///<summary>
 /// Determines whether errors cause exceptions to be thrown. Bydefault errors
 /// are handled in the class and the Error property is set forerror conditions.
 /// (not implemented at this time).
 /// </summary>
 public bool ThrowExceptions
 {
  get { return bThrowExceptions; }
  set { this.bThrowExceptions = value;}
 }

 ///<summary>
 /// If set to a non-zero value will automatically track cookies.The number assigned is the cookie count.
 /// </summary>
 public bool HandleCookies
 {
  get { return this.bHandleCookies; }
  set { this.bHandleCookies = value; }
 }
 public CookieCollection Cookies {
  get { return this.oCookies; }
  set { this.Cookies = value; }
 }
 public HttpWebResponse WebResponse  {
  get { return this.oWebResponse;}
  set { this.oWebResponse = value; }
 }
 public HttpWebRequest WebRequest  {
  get { return this.oWebRequest; }
  set { this.oWebRequest = value; }
 }

 // ***member properties
 //string cPostBuffer = "";
 MemoryStream oPostStream;
 BinaryWriter oPostData;

 intnPostMode = 1;

 intnConnectTimeout = 30;
 string cUserAgent = "West Wind HTTP .NET";

 stringcUsername = "";
 string cPassword = "";

 stringcProxyAddress = "";
 string cProxyBypass = "";
 string cProxyUsername = "";
 string cProxyPassword = "";

 boolbThrowExceptions = false;
 bool bHandleCookies = false;

 string cErrorMsg = "";
 bool bError = false;

 HttpWebResponse oWebResponse;
 HttpWebRequest oWebRequest;
 CookieCollection oCookies;

 stringcMultiPartBoundary = "-----------------------------7cf2a327f01ae";

 publicvoid wwHTTP()
 {
  //
  // TODO: Add constructor logic here
  //

 }

 ///<summary>
 /// Adds POST form variables to the request buffer.
 /// HttpPostMode determines how parms are handled.
 /// 1 - UrlEncoded Form Variables. Uses key and value pairs (ie."Name","Rick") to create URLEncoded content
 /// 2 - Multi-Part Forms - not supported
 /// 4 - XML block - Post a single XML block. Pass in as Key (1st Parm)
 /// other - raw content buffer. Just assign to Key.
 /// </summary>
 /// <param name="Key">Key value or raw bufferdepending on post type</param>
 /// <param name="Value">Value to store. Used onlyin key/value pair modes</param>
 public void AddPostKey(string Key, byte[] Value)
 {

  if (this.oPostData == null)
  {
   this.oPostStream = new MemoryStream();
   this.oPostData = new BinaryWriter(this.oPostStream);
  }

  if (Key == "RESET")
  {
   this.oPostStream = new MemoryStream();
   this.oPostData = new BinaryWriter(this.oPostStream);
  }

  switch(this.nPostMode)
  {
   case 1:
    this.oPostData.Write(Encoding.GetEncoding(1252).GetBytes(
         Key + "="+ System.Web.HttpUtility.UrlEncode(Value) + "&"));
    break;
   case 2:
    this.oPostData.Write(Encoding.GetEncoding(1252).GetBytes(
     "--" + this.cMultiPartBoundary +"\r\n" +
     "Content-Disposition: form-data;name=\"" +Key+"\"\r\n\r\n") );

    this.oPostData.Write( Value );

    this.oPostData.Write(Encoding.GetEncoding(1252).GetBytes("\r\n") );
    break;
   default:
    this.oPostData.Write( Value );
    break;
  }
 }

 publicvoid AddPostKey(string Key, string Value)
 {
  this.AddPostKey(Key,Encoding.GetEncoding(1252).GetBytes(Value));
 }

 ///<summary>
 /// Adds a fully self contained POST buffer to the request.
 /// Works for XML or previously encoded content.
 /// </summary>
 /// <param name="PostBuffer"></param>
 public void AddPostKey(string FullPostBuffer)
 {
  this.oPostData.Write(Encoding.GetEncoding(1252).GetBytes(FullPostBuffer) );
 }

 publicbool AddPostFile(string Key,string FileName)
 {
  byte[] lcFile;

  if(this.nPostMode != 2) {
   this.cErrorMsg = "File upload allowed only withMulti-part forms";
   this.bError = true;
   return false;
  }

  try
  {  
   FileStream loFile = newFileStream(FileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);

   lcFile= new byte[loFile.Length];
   loFile.Read(lcFile,0,(int) loFile.Length);
   loFile.Close();
  }
  catch(Exception e)
  {
   this.cErrorMsg = e.Message;
   this.bError = true;
   return false;
  }

  this.oPostData.Write(Encoding.GetEncoding(1252).GetBytes(
    "--" + this.cMultiPartBoundary +"\r\n"  +
    "Content-Disposition: form-data;name=\"" + Key + "\" filename=\"" +
    new FileInfo(FileName).Name +"\"\r\n\r\n") );

  this.oPostData.Write(lcFile );

  this.oPostData.Write(Encoding.GetEncoding(1252).GetBytes("\r\n")) ;

  returntrue;
 }


 /// <summary>
 /// Return a the result from an HTTP Url into a StreamReader.
 /// Client code should call Close() on the returned object whendone reading.
 /// </summary>
 /// <param name="Url">Url toretrieve.</param>
 /// <param name="WebRequest">An HttpWebRequestobject that can be passed in with properties preset.</param>
 /// <returns></returns>
 protected StreamReader GetUrlStream(string Url,HttpWebRequestRequest)
 {
  try
  {
   this.bError = false;
   this.cErrorMsg = "";


   if (Request == null)
   {
    Request =  (HttpWebRequest)System.Net.WebRequest.Create(Url);
   }

   Request.UserAgent = this.cUserAgent;
   Request.Timeout = this.nConnectTimeout * 1000;

   //*** Save for external access
   this.oWebRequest = Request;

   //*** Handle Security for the request
   if (this.cUsername.Length > 0)
   {
    if (this.cUsername=="AUTOLOGIN")
     Request.Credentials =CredentialCache.DefaultCredentials;
    else
     Request.Credentials = newNetworkCredential(this.cUsername,this.cPassword);
   }


   // *** Handle Proxy Server configuration
   if (this.cProxyAddress.Length > 0)
   {
    if (this.cProxyAddress =="DEFAULTPROXY")
    {
     Request.Proxy = new WebProxy();
     Request.Proxy = WebProxy.GetDefaultProxy();
    }
    else
    {
     WebProxy loProxy = newWebProxy(this.cProxyAddress,true);
     if (this.cProxyBypass.Length > 0)
     {
      loProxy.BypassList =this.cProxyBypass.Split(';');
     }

     if(this.cProxyUsername.Length > 0)
      loProxy.Credentials = newNetworkCredential(this.cProxyUsername,this.cProxyPassword);

     Request.Proxy= loProxy;
    }
   }

   // *** Handle cookies - automatically re-assign
   if (this.bHandleCookies)
   {
    Request.CookieContainer = new CookieContainer();
    if (this.oCookies != null &&this.oCookies.Count > 0)
    {
     Request.CookieContainer.Add(this.oCookies);
    }
   }

   //*** Deal with the POST buffer if any
   if (this.oPostData != null)
   {
    Request.Method = "POST";
    switch (this.nPostMode)
    {
     case 1:
      Request.ContentType ="application/x-www-form-urlencoded";
      // strip off any trailing & whichcan cause problems with some
      // http servers
//       if(this.cPostBuffer.EndsWith("&"))
//        this.cPostBuffer =this.cPostBuffer.Substring(0,this.cPostBuffer.Length-1);
      break;
     case 2:
      Request.ContentType ="multipart/form-data; boundary=" + this.cMultiPartBoundary;
      this.oPostData.Write(Encoding.GetEncoding(1252).GetBytes( "--" + this.cMultiPartBoundary +"\r\n" ) );
      break;
     case 4:
      Request.ContentType ="text/xml";
      break;
     default:
      goto case 1;
    }

    StreamloPostData = Request.GetRequestStream();
    //loPostData.Write(lcPostData,0,lcPostData.Length);
    this.oPostStream.WriteTo(loPostData);  

    byte[] buffer = newbyte[this.oPostStream.Length];
    buffer = this.oPostStream.ToArray();
    Console.Write(Encoding.GetEncoding(1252).GetString(buffer,0,buffer.Length));

    //***Close the memory stream
    this.oPostStream.Close();
    this.oPostStream = null;


    //*** Close the Binary Writer
    this.oPostData.Close();
    this.oPostData = null;

    //***Close Request Stream
    loPostData.Close();

    //*** clear the POST buffer
    //this.cPostBuffer = "";
   }


   // *** Retrieve the response headers
   HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

   //** Save cookies the server sends
   if (this.bHandleCookies)  
   {
    if (Response.Cookies.Count > 0)  
    {
     if (this.oCookies == null)  
     {
      this.oCookies = Response.Cookies;
     }
     else
     {
      // ** If we already have cookiesupdate the list
      foreach (Cookie oRespCookie inResponse.Cookies)  
      {
       bool bMatch = false;
       foreach(Cookie oReqCookie inthis.oCookies)  
       {
        if (oReqCookie.Name ==oRespCookie.Name)  
        {
         oReqCookie.Value =oRespCookie.Name;
         bMatch = true;
         break; //
        }
       } // for each ReqCookies
       if (!bMatch)
        this.oCookies.Add(oRespCookie);
      } // for each Response.Cookies
     }  // this.Cookies == null
    } // if Response.Cookie.Count > 0
   }  // if this.bHandleCookies = 0


   // *** Save the response object for external access
   this.oWebResponse = Response;

   Encodingenc;
   try
   {
    if (Response.ContentEncoding.Length  > 0)
     enc = Encoding.GetEncoding(Response.ContentEncoding);
    else
     enc = Encoding.GetEncoding(1252);
   }
   catch
   {
    // *** Invalid encoding passed
    enc = Encoding.GetEncoding(1252);
   }

   // *** drag to a stream
   StreamReader strResponse =
    new StreamReader(Response.GetResponseStream(),enc);
   return strResponse;
  }
  catch(Exception e)
  {
   if (this.bThrowExceptions)
    throw e;

   this.cErrorMsg= e.Message;
   this.bError = true;
   return null;
  }
 }

 ///<summary>
 /// Return a the result from an HTTP Url into a StreamReader.
 /// Client code should call Close() on the returned object whendone reading.
 /// </summary>
 /// <param name="Url">Url toretrieve.</param>
 /// <returns></returns>
 public StreamReader GetUrlStream(string Url)
 {
  HttpWebRequest oHttpWebRequest = null;
  return this.GetUrlStream(Url,oHttpWebRequest);
 }

 ///<summary>
 /// Return a the result from an HTTP Url into a StreamReader.
 /// Client code should call Close() on the returned object when donereading.
 /// </summary>
 /// <param name="Request">A Requestobject</param>
 /// <returns></returns>
 public StreamReader GetUrlStream(HttpWebRequest Request)
 {
  returnthis.GetUrlStream(Request.RequestUri.AbsoluteUri,Request);
 }


 /// <summary>
 /// Return a the result from an HTTP Url into a string.
 /// </summary>
 /// <param name="Url">Url toretrieve.</param>
 /// <returns></returns>
 public string GetUrl(string Url)
 {
  StreamReader oHttpResponse = this.GetUrlStream(Url);
  if (oHttpResponse == null)
   return "";

  stringlcResult = oHttpResponse.ReadToEnd();
  oHttpResponse.Close();

  returnlcResult;
 }

 ///<summary>
 /// Return a the result from an HTTP Url into a string.
 /// </summary>
 /// <param name="Url">Url toretrieve.</param>
 /// <returns></returns>
 public byte[] GetUrlBytes(string Url)
 {
  StreamReader oHttpResponse = this.GetUrlStream(Url);

  if (oHttpResponse == null)
  {
   return null;
  }

  stringlcResult = oHttpResponse.ReadToEnd();
  oHttpResponse.Close();

  returnnull;
 }

 ///<summary>
 /// Retrieves URL with events in the OnReceiveData event.
 /// </summary>
 /// <param name="Url"></param>
 /// <param name="BufferSize"></param>
 /// <returns></returns>
 public string GetUrlEvents(string Url,long BufferSize)
 {

  StreamReader oHttpResponse = this.GetUrlStream(Url);
  if (oHttpResponse == null)
   return "";

  longlnSize = BufferSize;
  if (this.oWebResponse.ContentLength > 0)
   lnSize = this.oWebResponse.ContentLength;
  else
   lnSize = 0;

  Encodingenc = Encoding.GetEncoding(1252);

  StringBuilderloWriter = new StringBuilder((int) lnSize);

  char[] lcTemp = new char[BufferSize];

  OnReceiveDataEventArgsoArgs = new OnReceiveDataEventArgs();
  oArgs.TotalBytes = lnSize;

  lnSize= 1;
  int lnCount = 0;
  long lnTotalBytes = 0;

  while(lnSize > 0)
  {
   lnSize = oHttpResponse.Read(lcTemp,0,(int) BufferSize);
   if (lnSize > 0)
   {
    loWriter.Append( lcTemp,0,(int) lnSize );
    lnCount++;
    lnTotalBytes += lnSize;

    //*** Raise an event if hooked up
    if (this.OnReceiveData != null)
    {
     /// *** Update the event handler
     oArgs.CurrentByteCount = lnTotalBytes;
     oArgs.NumberOfReads = lnCount;
     oArgs.CurrentChunk = lcTemp;
     this.OnReceiveData(this,oArgs);

     //*** Check for cancelled flag
     if (oArgs.Cancel)
      goto CloseDown;
    }
   }
  } // while


 CloseDown:
  oHttpResponse.Close();

  //*** Send Done notification
  if (this.OnReceiveData != null && !oArgs.Cancel)
  {
   // *** Update the event handler
   oArgs.Done = true;
   this.OnReceiveData(this,oArgs);
  }

//   returnlcHtml;
  return loWriter.ToString();
 }


 public delegate void OnReceiveDataHandler(object sender,OnReceiveDataEventArgs e);
 public class OnReceiveDataEventArgs
 {
  public long CurrentByteCount=0;
  public long TotalBytes = 0;
  public int NumberOfReads = 0;
  public char[] CurrentChunk;
  public bool Done = false;
  public bool Cancel = false;
 }  
}

wwHttp這個類裏面,不僅僅可以傳送文件AddPostFile方法,還可以傳送變量AddPostKey方法。

這樣,如果我們要通過代理服務器傳送文件,就可以編寫如下的代碼了:
wwHttp ww = new wwHttp();
  ww.ProxyAddress ="202.132.156.124";  
  ww.PostMode = 2;
  ww.AddPostKey("testKey","test");
  ww.AddPostFile("myfile",@"D:\Temp\Java\JavaStart\JavaStart2.jar");  

  string shtml = ww.GetUrlEvents("
http://localhost/UploadFileWeb/WebForm1.aspx",409600);
  Console.Write(shtml);

小結:

1)通過Web Services傳文件。

2)如果不需要使用代理,使用WebClient

3)如果需要使用代理,使用擴展得到的類wwHttp



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章