先建立一个WebApplication

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 == ""))
{
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, , postFile.ContentLength); string savePath = Path.Combine(saveDirectory, postFileName);
using (FileStream stream = new FileStream(savePath, FileMode.Create, FileAccess.Write))
{
stream.Write(bufferFile, , 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 = ;
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, , 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, , bufferFileInfo.Length);
} return bufferFileInfo; } private void btnSubmit_Click(object sender, EventArgs e)
{
Submit("http://localhost.:3558/Server.ashx", "abc", "", "F:\\报表下载清单2.xls");
}
}
}

最新文章

  1. Canvas基础认识
  2. android 多线程
  3. java中常见的几种异常
  4. Android 自定义Adapter 但listview 只显示第一条数据
  5. django with mysql (part-2)
  6. 135. Candy
  7. [bzoj3196]Tyvj 1730 二逼平衡树——线段树套平衡树
  8. C#集合之并发集合
  9. Vulkan Tutorial 07 Window surface
  10. Mybatis框架分析
  11. Redis-事务即简单锁应用
  12. wireshark数据包分析实战 第二章
  13. JDK8 Stream操作整理
  14. QT中定时器
  15. php文件及文件夹操作(创建、删除、移动、复制)
  16. 重写equals和hashCode的方法
  17. ADB 命令介绍
  18. FFmpeg简易播放器的实现-音视频播放
  19. 利用ForgeryPy生成虚拟数据
  20. BZOJ3786: 星系探索(伪ETT)

热门文章

  1. OpenGL ES crash notes 01 - Nice to meet you
  2. oracle字符查出一位
  3. Unity协程截图,WWWForm、WWW配合上传
  4. Python SQLAlchemy --2
  5. Jquery当中当data为json串时,eval(&#39;(&#39; +data+ &#39;)&#39;)的解释
  6. Web服务器的工作原理
  7. 使用spring配置quartz定时器
  8. 谷歌Cartographer学习(1)-快速安装测试
  9. iOS10字体
  10. 如何搭建一个WAMP环境