官方教程地址:
https://www.yuque.com/books/share/cd909cd4-cee7-4479-8d5c-2f0c41572dc3/kly1ig
POST方式上传文件
string uploadUrl = "http://47.92.121.230:9100/document/fileupload/qycode";
string result = "";
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uploadUrl);
webrequest.ContentType = "multipart/form-data; boundary=" + boundary; webrequest.Method = "POST";
StringBuilder sb = new StringBuilder(); sb.Append("--");
sb.Append(boundary); sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"file");
sb.Append("\"; filename=\"" + fileName + "\"");
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: application/octet-stream");
sb.Append("\r\n");
sb.Append("\r\n");
string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
webrequest.ContentLength = uploadFile.InputStream.Length + postHeaderBytes.Length + boundaryBytes.Length; Stream requestStream = webrequest.GetRequestStream();
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
byte[] buffer = new Byte[(int)uploadFile.InputStream.Length]; //声明文件长度的二进制类型
uploadFile.InputStream.Read(buffer, 0, buffer.Length); //将文件转成二进制
requestStream.Write(buffer, 0, buffer.Length); //赋值二进制数据
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); webrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
WebResponse responce = webrequest.GetResponse();
requestStream.Close();
using (Stream s = responce.GetResponseStream())
{
using (StreamReader sr = new StreamReader(s)) {
result = sr.ReadToEnd();
}
}
responce.Close();