转:Http协议post文件

前段时间在做使用http协议向一个url发送xml文件的程序, 花了一点时间, 现把我的代码贴出来, 给大家共享。

--------------------------------------------------------------------------------------------
            try
            {
                CookieContainer cookies = new CookieContainer();

                string boundary = "---------------------------7d429871607fe";
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
                httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
                httpWebRequest.CookieContainer = cookies;
                httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
                httpWebRequest.Method = "POST";
                httpWebRequest.Timeout = 5* 60 * 1000;

                StringBuilder sb = new StringBuilder();
                sb.Append("--");
                sb.Append(boundary);
                sb.Append("\r\n");
                sb.Append("Content-Disposition: form-data; name=\"file\"; filename=\"");
                sb.Append(Path.GetFileName(fileName));
                sb.Append("\"");
                sb.Append("\r\n");
                sb.Append("Content-Type: text/xml");
                sb.Append("\r\n");
                sb.Append("\r\n");

                string postHeader = sb.ToString();
                byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);

                byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

                FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

                httpWebRequest.ContentLength = fileStream.Length + (long)postHeaderBytes.Length + (long)boundaryBytes.Length;

                using (Stream requestStream = httpWebRequest.GetRequestStream())
                {
                    requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
                    byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
                    int bytesRead = 0;
                    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                        requestStream.Write(buffer, 0, bytesRead);
                    requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                }
                fileStream.Close();
                fileStream = null;

                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (httpWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(httpWebResponse.GetResponseStream());
                    nDataCount = int.Parse(xmlDoc.DocumentElement.GetElementsByTagName("Success").Item(0).Attributes["MonDataCount"].Value);
                    lbToopInfo.BeginInvoke(ShowInfo, "收到服务器的回复");
                    xmlDoc.Save(Application.StartupPath + @"\XML_Receive\" + Path.GetFileName(fileName));
                    httpWebResponse.Close();
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                lbToopInfo.BeginInvoke(ShowInfo, "SendXMLFileex: " + ex.ToString());
            }
            finally
            { GC.Collect(); }

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/qingniaoit/archive/2007/10/12/1822490.aspx

原文地址:https://www.cnblogs.com/zjsct/p/1703128.html