HttpWebRequest 方式提交文件数据-以图片为例

public void StartPing1(string pingURL)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(pingURL);
            FileStream fs = new FileStream(@"D:\111.jpg", FileMode.Open, FileAccess.Read);
            Byte[] bytes = new Byte[10240];
            request.Method = "POST";
            request.Proxy = null;
            //request.Headers.Add("XXX", "XXX");
            request.ContentType = "application/octet-stream";
            Stream dataStream = request.GetRequestStream();
            int count = fs.Read(bytes, 0, 10240);
            while (count != 0)
            {
                dataStream.Write(bytes, 0, count);
                count = fs.Read(bytes, 0, 10240);
            }
            fs.Close();
            dataStream.Close();
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.ASCII);
                string ret = sr.ReadToEnd();
                response.Close();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("!!!!!!ERROR!!!!!!!!" + ex.ToString() + "!!!!!!!!ERROR!!!!!!!!");
            }
        }

  

---下面是服务器端接收方法:

加载事件中:
/* 
            //StartPing1 方法 传送文件的
            System.Drawing.Image postImage = System.Drawing.Image.FromStream(Request.InputStream);
            System.Drawing.Bitmap bitmap_b = new System.Drawing.Bitmap(postImage);
            string Opath = @"D:\";
            string photoname = DateTime.Now.Ticks.ToString();
            bitmap_b.Save(Opath + "\\" + photoname + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            Response.Write("<?xml version=\"1.0\"?><params><title>OK</title></params>");
             * */

  

posted @ 2013-03-18 16:26  左正  阅读(442)  评论(0编辑  收藏  举报