C#实现HttpPost提交文件
2014-04-26 21:42
232 查看
先建立一个WebApplication
Web.config
Server.ashx
选建个html测试下
再用C#按Http协议提交:
Web.config
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <!--<globalization requestEncoding="gb2312" responseEncoding="gb2312" fileEncoding="gb2312"/>--> <compilation debug="true" /> <authentication mode="Windows" /> </system.web> </configuration>
Server.ashx
using System; using System.Data; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; using System.IO; using System.Text; namespace WebApplication1 { [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Server : IHttpHandler { public void ProcessRequest(HttpContext context) { try { string saveDirectory = @"f:\temp"; string user = context.Request.Form["user"].ToString(); string pass = context.Request.Form["pass"].ToString(); if (!(user == "abc" && pass == "123")) { context.Response.Write("验证出错!"); } HttpPostedFile postFile = context.Request.Files["myFile"]; string postFileName = Path.GetFileName(postFile.FileName); byte[] bufferFile = new byte[postFile.ContentLength]; postFile.InputStream.Read(bufferFile, 0, postFile.ContentLength); string savePath = Path.Combine(saveDirectory, postFileName); using (FileStream stream = new FileStream(savePath, FileMode.Create, FileAccess.Write)) { stream.Write(bufferFile, 0, bufferFile.Length); stream.Flush(); stream.Close(); } context.Response.Write("提交成功!"); } catch (Exception ex) { context.Response.Write(ex.Message); } } public bool IsReusable { get { return false; } } } }
选建个html测试下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>测试提交</title> </head> <body> <form action="Server.ashx" enctype="multipart/form-data" method="post"> <input type="text" name="user" value="abc" /> <input type="text" name="pass" value="123"/> <input type="file" name="myFile" /> <input type="submit" /> </form> </body> </html>
再用C#按Http协议提交:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.IO; namespace WinFormHttpPost { public partial class Form1 : Form { private Encoding currentEncode = Encoding.GetEncoding("utf-8"); public Form1() { InitializeComponent(); } private void Submit(string url, string user, string pass, string filePath) { string boundary = Guid.NewGuid().ToString(); string beginBoundary = "--" + boundary; string endBoundary = "--" + boundary + "--"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.ContentType = "multipart/form-data; boundary=" + boundary; request.Method = "POST"; request.KeepAlive = true; StringBuilder sbBody = new StringBuilder(); sbBody.AppendLine(beginBoundary); sbBody.AppendLine("Content-Disposition: form-data; name=\"user\""); sbBody.AppendLine(); sbBody.AppendLine(user); sbBody.AppendLine(beginBoundary); sbBody.AppendLine("Content-Disposition: form-data; name=\"pass\""); sbBody.AppendLine(); sbBody.AppendLine(pass); sbBody.AppendLine(beginBoundary); sbBody.AppendLine(string.Format("Content-Disposition: form-data; name=\"myFile\"; filename=\"{0}\"", filePath)); sbBody.AppendLine("Content-Type: application/octet-stream"); sbBody.AppendLine(); byte[] bufferContent = currentEncode.GetBytes(sbBody.ToString()); byte[] bufferFile = GetFileByte(filePath); byte[] bufferEndBoundary = currentEncode.GetBytes("\r\n" + endBoundary); byte[] bufferBody = new byte[bufferContent.Length + bufferFile.Length + bufferEndBoundary.Length]; int startIndex = 0; bufferContent.CopyTo(bufferBody, startIndex); startIndex += bufferContent.Length; bufferFile.CopyTo(bufferBody, startIndex); startIndex += bufferFile.Length; bufferEndBoundary.CopyTo(bufferBody, startIndex); request.ContentLength = bufferBody.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(bufferBody, 0, bufferBody.Length); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream receiveStream = response.GetResponseStream(); StreamReader readStream = new StreamReader(receiveStream, currentEncode); tbResult.Text = readStream.ReadToEnd(); response.Close(); readStream.Close(); } private byte[] GetFileByte(string filePath) { byte[] bufferFileInfo = null; using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { bufferFileInfo = new byte[stream.Length]; stream.Read(bufferFileInfo, 0, bufferFileInfo.Length); } return bufferFileInfo; } private void btnSubmit_Click(object sender, EventArgs e) { Submit("http://localhost.:3558/Server.ashx", "abc", "123", "F:\\报表下载清单2.xls"); } } }
相关文章推荐
- C#实现http协议支持上传下载文件的GET、POST请求
- 关于C#提交表单post和get的实现 HttpWebRequest 和ttpebeponse
- HTTP的POST请求的详细构造[实现机器提交表单含上传文件及普通文本域]
- C# HttpWebRequest用Post同时提交参数和文件的封装类
- C#实现http协议支持上传下载文件的GET、POST请求
- 问题:HttpWebRequest request post 传参; 结果:好用的C# HttpWebRequest用Post同时提交参数和文件的封装类
- C#实现http协议支持上传下载文件的GET、POST请求
- C#中使用HttpWebRequest用Post提交MultiPart数据
- HTTP POST上传文件代码实现
- C# http post上传文件
- c#实现 ftp http共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载
- HTTP POST请求报文格式分析与Java实现文件上传
- HTTP POST请求报文格式分析与Java实现文件上传
- 实现HTTP协议Get、Post和文件上传功能——使用libcurl接口实现
- Multipart/form-data POST文件上传详解 理论 简单的HTTP POST 大家通过HTTP向服务器发送POST请求提交数据,都是通过form表单提交的,代码如下: <form me
- C#实现POST提交方式
- C#模拟POST提交表单(二)--HttpWebRequest以及HttpWebResponse
- C# 使用HttpWebRequest Post提交数据,携带Cookie和相关参数示例
- C#三种模拟自动登录和提交POST信息的实现方法
- C#通过WebClient/HttpWebRequest实现http的post/get方法