public void ProcessRequest(HttpContext context)
//参考http://www.cnblogs.com/greenerycn/archive/2010/05/15/csharp_http_post.html
string filePath = "d:\\apple4.jpg";
string fileName = "apple4.jpg";
string postURL = "http://192.168.1.11/testhandler/accfile.ashx";
// 边界符
var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
// 最后的结束符
var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");
// 文件参数头
const string filePartHeader =
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n";
var fileHeader = string.Format(filePartHeader, "file", fileName);
var fileHeaderBytes = Encoding.UTF8.GetBytes(fileHeader);
// 开始拼数据
var memStream = new MemoryStream();
memStream.Write(beginBoundary, 0, beginBoundary.Length);
// 文件数据
memStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);
var buffer = new byte[1024];
int bytesRead; // =0
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
memStream.Write(buffer, 0, bytesRead);
fileStream.Close();
// Key-Value数据
var stringKeyHeader = "\r\n--" + boundary +
"\r\nContent-Disposition: form-data; name=\"{0}\"" +
"\r\n\r\n{1}\r\n";
Dictionary<string, string> stringDict = new Dictionary<string, string>();
stringDict.Add("len", "500");
stringDict.Add("wid", "300");
foreach (byte[] formitembytes in from string key in stringDict.Keys
select string.Format(stringKeyHeader, key, stringDict[key])
into formitem
select Encoding.UTF8.GetBytes(formitem))
memStream.Write(formitembytes, 0, formitembytes.Length);
// 写入最后的结束边界符
memStream.Write(endBoundary, 0, endBoundary.Length);
//倒腾到tempBuffer?
memStream.Position = 0;
var tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
// 创建webRequest并设置属性
var webRequest = (HttpWebRequest)WebRequest.Create(postURL);
webRequest.Method = "POST";
webRequest.Timeout = 100000;
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
webRequest.ContentLength = tempBuffer.Length;
var requestStream = webRequest.GetRequestStream();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
string responseContent;
using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(),Encoding.GetEncoding("utf-8")))
responseContent = httpStreamReader.ReadToEnd();
httpWebResponse.Close();
webRequest.Abort();
context.Response.ContentType = "text/plain";
context.Response.Write(responseContent);
public void ProcessRequest(HttpContext context)
context.Response.ContentType = "text/plain";
if (context.Request.Files.Count == 0)
context.Response.Write("No file");
return;
HttpPostedFile f1 = context.Request.Files[0];
System.Drawing.Image image = System.Drawing.Image.FromStream(f1.InputStream);
image.Save("d:\\upload.jpg");
string strPars="";
foreach (var key in context.Request.Form.AllKeys)
string val = context.Request[key];
strPars += "["+key + ":" + val + "] ";
image.Dispose();
context.Response.Write("OK Get File:" + f1.FileName + " Pars:" + strPars);
这个小程序参考了另一位博友的代码,做了稍许调整,创建的两个Handler程序,一个上传的ashx,一个接收的ashx上传文件代码public void ProcessRequest(HttpContext context) { //参考http://www.cnblogs.com/greenerycn/archive/2010/05/15/csharp_http_post.htm
Dictionary<string, string> parameters = new Dictionary<string, string>(); //参数列表
parameters.Add("paraName", "paraValue");
string url = "";
HttpWebRequest request = null;
HttpWebRespon...
在做CS调用第三方接口的时候遇到了这样的一个问题,通过PSOTman调试需要分别在parmas、Headers、Body里面同时传递参数。在网上查询了很多资料,以此来记录一下开发脱坑历程。
POSTman调试界面:params参数
POSTman调试界面:Headers参数
POSTman调试界面:Body参数
在postman调试里面可以这么传...
1、Httpwebrequest是用于发送Http数据 httpwebresponse用于接收http数据
2、这两个类位 于System.Net命名空间
3、HttpWebRequest对象不是利用new关键字通过构 造函数来创建的,而是利用工厂机制(factory mechanism)通过Create()方法来创建的
4、另外,你可能预计需要显式地调用一个“Send”方法,实际上不需要。接下来...
当我们打开一个网页时,浏览器要向网站服务器发送一个HTTP请求头,然后网站服务器根据HTTP请求头的内容生成当次请求的内容发送给浏览器。你明白HTTP请求头的具体含意吗?下面一条条的为你详细解读,先看某一次HTTP请求头的具体内容:
Accept-Lan