C# 模拟表单 post数据 文件 和 文本 HttpWebRequest

实际上就是拼接 文件头和要传的信息,利用 HttpWebRequest 发送请求即可。 具体的文件头 和要传的信息 其实可以用火狐的工具查看到底发送了哪些东西,格式如何。方法如下,是把别人写的东西改了一点点。参考链接: http://www.cnblogs.com/chiname/articles/375151.html
/// <summary>
		/// 上传的方法
		/// </summary>
		/// <param name="uploadfile">单个文件名(上传多个文件的方法自己修改)</param>
		/// <param name="url">post请求的url</param>
		/// <param name="poststring">post的字符串 键值对,相当于表单上的文本框里的字符</param>
		/// <returns></returns>
		public static string UploadFileEx(string uploadfile, string url, NameValueCollection poststring)
		{
			Uri uri = new Uri(url);

			string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
			HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
			webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
			webrequest.Method = "POST";


			// Build up the post message header 
			StringBuilder sb = new StringBuilder();

			//加文本
			foreach (string key in poststring.Keys)
			{
				sb.Append("--");
				sb.Append(boundary);
				sb.Append("\r\n");
				sb.Append("Content-Disposition: form-data; name=\"");
				sb.Append(key);
				sb.Append("\"");
				sb.Append("\r\n");
				sb.Append("\r\n");
				sb.Append(poststring.Get(key));
				sb.Append("\r\n");
			}

			//加文件
			sb.Append("--");
			sb.Append(boundary);
			sb.Append("\r\n");
			sb.Append("Content-Disposition: form-data; name=\"filename\";");
			sb.Append("filename=\"");
			sb.Append(Path.GetFileName(uploadfile));
			sb.Append("\"");
			sb.Append("\r\n");
			sb.Append("Content-Type: multipart/form-data");
			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(uploadfile, FileMode.Open, FileAccess.Read);
			long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
			webrequest.ContentLength = length;

			Stream requestStream = webrequest.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);

			webrequest.Timeout = 1000000;

			//System.Windows.Forms.MessageBox.Show(webrequest.Timeout.ToString()); 

			WebResponse responce = webrequest.GetResponse();

			Stream s = responce.GetResponseStream();

			StreamReader sr = new StreamReader(s);

			string str = sr.ReadToEnd();


			fileStream.Close();

			requestStream.Close();

			sr.Close();

			s.Close();

			responce.Close();

			return str;
		}
实例调用

NameValueCollection poststring = new NameValueCollection();

   poststring["username"] = "laokaizzz";//post 的参数    poststring["password"] = "111111111";                string uploadfile;// set to file to upload

   uploadfile = "D:/2.txt";//上传的文件名

   string str = UploadFileEx(uploadfile, http://www.xxx.com/upload", poststring);    MessageBox.Show(str);


参考:http://www.cnblogs.com/chiname/articles/375151.html

其他:http://www.w3.org/TR/html401/interact/forms.html

http://www.cnblogs.com/greenerycn/archive/2010/05/15/csharp_http_post.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值