日期:2014-05-17  浏览次数:20977 次

向服务器POST数据(参数+文件)
   public string PostFile(string FilePath,string user,string codeid, Encoding encoding, ProgressBar p)
        {
            string strResult = string.Empty;
            string param = "userid" + "=" + user + "&" +"code" + "=" + codeid + "&" + "Photo" + "=";
            HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(PostUrl);
            myRequest.Method = "Post";
            myRequest.KeepAlive = true;
            myRequest.ContentType = "application/x-www-form-urlencoded";  
            myRequest.Accept = "*/*";

            byte[] PostArray = encoding.GetBytes(param);
            FileStream Filestream = new FileInfo(FilePath).OpenRead();
            BinaryReader oReader = new BinaryReader(Filestream);
            byte[] imgdata = oReader.ReadBytes(Convert.ToInt32(Filestream.Length));

            byte[] data = new byte[PostArray.Length + imgdata.Length];
        
            myRequest.ContentLength = PostArray.Length + imgdata.Length;

            PostArray.CopyTo(data, 0);
            imgdata.CopyTo(data, PostArray.Length); 
            Stream PostStream = myRequest.GetRequestStream();
            PostStream.Write(data, 0, data.Length);          
            PostStream.Close();
            ////获取服务器端的响应
            WebResponse webRespon;
            try
            {
                 webRespon = myRequest.GetResponse();
            }
            catch (WebException ex)
            {
                 webRespon = (HttpWebResponse)ex.Response;