当前位置:编程学习 > C#/ASP.NET >>

C#模拟提交表单post方式。但是表单中有一个项是需要附件上传带附件 如何实现!

谁做过 能否给出代码?
我在google.com里搜找到了个JAVA 的代码 但是我用c#实在搞不定! --------------------编程问答-------------------- public void SendFile(string fileName, Uri uri, CredentialCache credentialCache)
        {

            CookieContainer cookies = new CookieContainer();
            // cast the WebRequest to a HttpWebRequest since we're using HTTPS 
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
            httpWebRequest.Credentials = credentialCache;
            httpWebRequest.CookieContainer = cookies;
            WebResponse webResponse = httpWebRequest.GetResponse();
            string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
            HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(uri);
            httpWebRequest2.Credentials = credentialCache;
            httpWebRequest2.CookieContainer = cookies;
            httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
            httpWebRequest2.Method = "POST";

            // Build up the post message header 
            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: application/octet-stream");
            sb.Append("\r\n");
            sb.Append("\r\n");

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

            // Build the trailing boundary string as a byte array 
            // ensuring the boundary appears on a line by itself 
            byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
            httpWebRequest2.ContentLength = length;

            Stream requestStream = httpWebRequest2.GetRequestStream();

            // Write out our post header 
            requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

            // Write out the file contents 
            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);

            // Write out the trailing boundary 
            requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);

            WebResponse webResponse2 = httpWebRequest2.GetResponse();

        } 

找了个这样的代码 可是怎么把 其他表单的文字数据也一起发上去呀??? --------------------编程问答-------------------- 顶!!!!!!!!!!!!!!!!!! --------------------编程问答-------------------- 顶啊!  谁能帮我搞顶!!!!!!! --------------------编程问答-------------------- --------------------编程问答-------------------- multipart/form-data; boundary=" 
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,