我同事写了一个文件管理系统,可以上传下载附件,用户要求在邮件中可以下载当时上传的附件,我呢,就想偷个懒,不想自己写存放的地方,所以就打算调用她的接口,结果,半天搞不动,但是前端都能调用,简直是难过啊!好在,经过我不懈的努力终于还是调用成功了。
首先就是这个http请求该怎么发送呢,好在以前写过,找到代码,只是没有上传附件的,改了N久,嗯,妥了!
- string boundary = "----WebKitFormBoundary" + DateTime.Now.Ticks.ToString("x");
- //byte[] boundarybytes = Encoding.ASCII.GetBytes(boundary);
- byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
- byte[] endbytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
- //if (contentType == "multipart/form-data")
- // contentType += ";boundary=" + boundary;
- request.ContentType = "multipart/form-data;boundary=" + boundary;

文件转成流,存放于bytebuffer
filePath:文件的路径。上传你总得先获取到文件不是。
- FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
- byte[] bytebuffer;
- bytebuffer = new byte[fs.Length];
- fs.Read(bytebuffer, 0, (int)fs.Length);
- fs.Close();
我的文件是已经转成了流传进来的
- public static string HttpPostForm(string url, Dictionary<string, string> postData = null, Dictionary<string, byte[]> files = null, int timeOut = 120, Dictionary<string, string> headers = null)
- {
- // HTTP请求 会携带很多头部数据,以及body数据。头部数据量小,body数据量大,附件这种一般都是放body里。
- // 以字节流的形式放在stream里,随着请求一起发出去!!(理解不一定对,但大致应该是这样的),你给别人发消息
- postData = postData ?? null;
- ServicePointManager.DefaultConnectionLimit = 512;
- request = WebRequest.Create(url) as HttpWebRequest;
- string boundary = "----WebKitFormBoundary" + DateTime.Now.Ticks.ToString("x");
- byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
- byte[] endbytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
-
- request.ContentType = "multipart/form-data;boundary=" + boundary;
- request.Timeout = timeOut * 1000;
- if (headers != null)
- {
- foreach (var header in headers)
- request.Headers.Add(header.Key, header.Value);
- }
- request.Method = "POST";
-
- string postdata = JsonConvert.SerializeObject(postData);
- byte[] data = Encoding.UTF8.GetBytes(postdata);
-
- using (Stream stream = request.GetRequestStream())
- {
- //stream.Write(data, 0, data.Length);
- //1.1 key/value
- string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
- foreach (var item in postData)
- {
- stream.Write(boundarybytes, 0, boundarybytes.Length);
- string formitem = string.Format(formdataTemplate, item.Key, item.Value);
- byte[] formitembytes = Encoding.UTF8.GetBytes(formitem);
- stream.Write(formitembytes, 0, formitembytes.Length);
- }
-
- //1.2 file
- if (files != null)
- {
- // 有附件则上传
- foreach (var file in files)
- {
- stream.Write(boundarybytes, 0, boundarybytes.Length);
- string temp = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n\r\n";
- string header = string.Format(temp, "file", file.Key);
- byte[] fileName = Encoding.UTF8.GetBytes(header);
- stream.Write(fileName, 0, fileName.Length);
- stream.Write(file.Value);
- }
- }
- //1.3 form end
- stream.Write(endbytes, 0, endbytes.Length);
- }
-
-
- // HTTP响应,也就是你给别人发的消息,别人看到了,给你回消息
-
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- using (var httpStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
- {
- return httpStreamReader.ReadToEnd();
- }
- }
the request was rejected because no multipart boundary was found