tnblog
首页
视频
资源
登录

http上传文件

3597人阅读 2022/1/4 10:59 总访问:12571 评论:0 收藏:0 手机
分类: .NET
  1.    public static string HttpUploadFile(string url, string path)
  2.         {
  3.             // 设置参数
  4.             HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
  5.             CookieContainer cookieContainer = new CookieContainer();
  6.             request.CookieContainer = cookieContainer;
  7.             request.AllowAutoRedirect = true;
  8.             request.Method = "POST";
  9.             string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
  10.             request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
  11.             byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
  12.             byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
  13.             int pos = path.LastIndexOf("\\");
  14.             string fileName = path.Substring(pos + 1);
  15.             //请求头部信息
  16.             StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
  17.             byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
  18.             FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
  19.             byte[] bArr = new byte[fs.Length];
  20.             fs.Read(bArr, 0, bArr.Length);
  21.             fs.Close();
  22.             Stream postStream = request.GetRequestStream();
  23.             postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
  24.             postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
  25.             postStream.Write(bArr, 0, bArr.Length);
  26.             postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
  27.             postStream.Close();
  28.             //发送请求并获取相应回应数据
  29.             HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  30.             //直到request.GetResponse()程序才开始向目标网页发送Post请求
  31.             Stream instream = response.GetResponseStream();
  32.             StreamReader sr = new StreamReader(instream, Encoding.UTF8);
  33.             //返回结果网页(html)代码
  34.             string content = sr.ReadToEnd();
  35.             //得到文件的回传路径并返回
  36.             Root root = JsonConvert.DeserializeObject<Root>(content);
  37.             List<string> list = root.data;
  38.             return list.FirstOrDefault();
  39.         }


评价

net core启动报错Unable to configure httpS endpoint. No server certificate was specified

这是因为net core2.1默认使用的https,如果使用Kestrel web服务器的话没有安装证书就会报这个错其实仔细看他的错误提示,其...

httpmodule忽略静态资源

只需要配置一个preCondition=&quot;managedHandler&quot;即可&lt;system.webServer&gt; &lt;modules&gt; &lt;addname=&qu...

wcf DuplexhttpBinding双向通信

一:建立接口CallbackContract = typeof(ICallback)指定需要回调通信的接口,该接口方法由前端实现[ServiceContract(Callba...

ASP.NET管道模型之httpModule

HttpModule是什么在Http请求处理过程中,请求会前后两次通过一系列的HttpModule,这些Module对Http请求具有完全控制权,可...

httpModule管道模型小细节

//在HttpModule管道模型中,如果用重定向跳转页面,因为HttpModule是程序最开始的执行地方,所以在里面永远都是循环的在执...

.NET httpClient和webapi相关的东东先记录一点。传递Authorization headertoken

get请求传递Authorization header,tokenstringurl=&quot;你请求的url&quot;; stringtoken=&quot;yourtoken&quot;; Http...

net core无法使用http访问

是因为默认开启了Https重定向,去掉就好了

NotSupportedException: http/2 over TLS is not supported on Windows 7 due to missing

.net core grpc报错:NotSupportedException: HTTP/2 over TLS is not supported on Windows 7 due to missing ALPN suppor...

启动grpc报错:http/2 over TLS is not supported on Windows 7

.net core grpc报错:NotSupportedException: HTTP/2 over TLS is not supported on Windows 7 due to missing ALPN suppor...

IIS配置httpS

一、下载证书(我这里是下载的阿里云免费的证书)下载iis版本然后解压解压效果如下:二、安装证书(我这里是windows server...

nginx https的证书配置与301跳转

在 etc/nginx/ 目录下新建 ssl 文件夹来存放证书。把 crt 证书文件和 key 私钥文件上传到这里。然后就可以配置 Nginx 配置...

阿里云免费https证书申请

找到安全相关里边的ssl证书,点击购买然后选择免费版然后一直下一步下一步就可以购买成功了然后点击证书申请把你想要绑定的...

windows nginx配置https证书

windows nginx配置https证书还是比较简单的我这里申请的证书是阿里云的,去下载nginx版本的解压后有两个文件一个.key,一个....

七牛云cdn配置https

1:域名管理选择一个自己的域名2:点击配置3:拉到下面有个https配置4:选择免费证书即可5:成功等待

七牛云配置https支持

一.域名管理选择一个自己的域名二.点击配置

ng : 无法加载文件 C:\Users\WSJ\AppData\Roaming\npm\ng.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com /fwlink/?LinkID=135170 中的.

错误提示:解决方法:在win10搜索那里搜索Windos PowerShell然后选择管理员身份打开,ps:一定要选择 以管理员身份运行打开...
没有个性,不需要签名
排名
78
文章
3
粉丝
0
评论
0
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术