个人收藏--未整理—C# http/https 上传下载文件
c# HTTP/HTTPS 文件上传。
分类: .net 2015-02-03 08:36 541人阅读 评论(0) 收藏 举报
方法主体
[csharp] view plaincopy
- public static string MyUploader(string strFileToUpload, string strUrl, string strFileFormName, NameValueCollection querystring, CookieContainer cookies)
- {
- string postdata;
- postdata = "?";
- if (querystring != null)
- {
- foreach (string key in querystring.Keys)
- {
- postdata += key + "=" + querystring.Get(key) + "&";
- }
- }
- //Uri uri = new Uri(strUrl + postdata);
- Uri oUri = new Uri(strUrl + postdata);
- string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
- // The trailing boundary string
- byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n");
- // The post message header
- StringBuilder sb = new StringBuilder();
- sb.Append("--");
- sb.Append(strBoundary);
- sb.Append("\r\n");
- sb.Append("Content-Disposition: form-data; name=\"");
- sb.Append(strFileFormName);
- sb.Append("\"; filename=\"");
- sb.Append(Path.GetFileName(strFileToUpload));
- sb.Append("\"");
- sb.Append("\r\n");
- sb.Append("Content-Type: ");
- sb.Append("application/octet-stream");
- sb.Append("\r\n");
- sb.Append("\r\n");
- string strPostHeader = sb.ToString();
- byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
- // The WebRequest
- HttpWebRequest oWebrequest = (HttpWebRequest)WebRequest.Create(oUri);
- //如果是发送HTTPS请求
- if (strUrl.StartsWith("https", StringComparison.OrdinalIgnoreCase))
- {
- ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
- oWebrequest = WebRequest.Create(oUri) as HttpWebRequest;
- oWebrequest.ProtocolVersion = HttpVersion.Version10;
- }
- else
- {
- oWebrequest = WebRequest.Create(oUri) as HttpWebRequest;
- }
- oWebrequest.ContentType = "multipart/form-data; boundary=" + strBoundary;
- oWebrequest.Method = "POST";
- // This is important, otherwise the whole file will be read to memory anyway...
- oWebrequest.AllowWriteStreamBuffering = false;
- // Get a FileStream and set the final properties of the WebRequest
- FileStream oFileStream = new FileStream(strFileToUpload, FileMode.Open, FileAccess.Read);
- long length = postHeaderBytes.Length + oFileStream.Length + boundaryBytes.Length;
- oWebrequest.ContentLength = length;
- Stream oRequestStream = oWebrequest.GetRequestStream();
- // Write the post header
- oRequestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
- // Stream the file contents in small pieces (4096 bytes, max).
- byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)oFileStream.Length))];
- int bytesRead = 0;
- while ((bytesRead = oFileStream.Read(buffer, 0, buffer.Length)) != 0)
- oRequestStream.Write(buffer, 0, bytesRead);
- oFileStream.Close();
- // Add the trailing boundary
- oRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
- WebResponse oWResponse = oWebrequest.GetResponse();
- Stream s = oWResponse.GetResponseStream();
- StreamReader sr = new StreamReader(s);
- String sReturnString = sr.ReadToEnd();
- // Clean up
- oFileStream.Close();
- oRequestStream.Close();
- s.Close();
- sr.Close();
- return sReturnString;
- }
[csharp] view plaincopy
- private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
- {
- return true; //总是接受
- }
调用方法
[csharp] view plaincopy
- CookieContainer cookies = new CookieContainer();
- //add or use cookies
- NameValueCollection querystring = new NameValueCollection();
- querystring["login_id"] = "your userid";
- querystring["password"] = "your password";
- string uploadfile = @"C:\Test.zip";// set to file to upload
- string outdata = MyUploader(filePath, updateUrl, "zipfile", querystring, cookies);
个人收藏--未整理—C# http/https 上传下载文件的更多相关文章
- 向linux服务器上传下载文件方式收集
向linux服务器上传下载文件方式收集 1. scp [优点]简单方便,安全可靠:支持限速参数[缺点]不支持排除目录[用法] scp就是secure copy,是用来进行远程文件拷贝的.数据传输使用 ...
- C#实现http协议支持上传下载文件的GET、POST请求
C#实现http协议支持上传下载文件的GET.POST请求using System; using System.Collections.Generic; using System.Text; usin ...
- HttpClient上传下载文件
HttpClient上传下载文件 java HttpClient Maven依赖 <dependency> <groupId>org.apache.httpcomponents ...
- 【WCF】利用WCF实现上传下载文件服务
引言 前段时间,用WCF做了一个小项目,其中涉及到文件的上传下载.出于复习巩固的目的,今天简单梳理了一下,整理出来,下面展示如何一步步实现一个上传下载的WCF服务. 服务端 1.首先新建一个名 ...
- springboot整合vue实现上传下载文件
https://blog.csdn.net/yhhyhhyhhyhh/article/details/89888953 文章目录 springboot整合vue实现上传下载文件 1上传下载文件api文 ...
- 上传下载文件到Linux服务器
转自链接:https://blog.csdn.net/drdongshiye/article/details/89430535Mac的终端是十分强大 , 可以通过命令进行上传下载下载文件夹 scp - ...
- 【转】Java IOUtils方式上传下载文件 on HDFS
[From]https://www.cnblogs.com/areyouready/p/9795442.html package com.css.hdfs04; import java.io.File ...
- Jmeter 上传下载文件
最近很多同学都在问jmeter上传.下载文件的脚本怎么做,要压测上传.下载文件的功能,脚本怎么做,网上查了都说的很含糊,这次呢,咱们就好好的把jmeter的上传下载文件好好缕缕,都整明白了,怎么个过程 ...
- rz和sz上传下载文件工具lrzsz
######################### rz和sz上传下载文件工具lrzsz ####################################################### ...
随机推荐
- fenby C语言 P12
条件语句的嵌套 注意格式对齐 #include <stdio.h> int main(){ int score=67; if(score<=100&&score> ...
- Spring Cloud gateway 网关服务二 断言、过滤器
微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...
- node留言板
这是一个nodejs + mongodb 的小项目,对数据库的增删改查 1. 引入项目依赖 "art-template": "^4.13.2", "b ...
- Everything可能泄漏大量电脑敏感资料
一款好用的文件搜索工具Everything被批露出现重大问题: Everything可以打开http服务,在没有加密的情况下任何外网电脑都可以连接的. 因此,Everything可能泄漏大量电脑敏感资 ...
- xml 需要转义的字符
XML实体中不允许出现"&","<",">"等特殊字符,否则XML语法检查时将出错,如果编写的XML文件必须包含这些字符 ...
- 在虚拟机上的关于Apache(阿帕奇)(1)开启Apache服务以及介绍基础服务
我们来开始讲述Apache(阿帕奇)服务 小知识: Apache 使得一台服务器上放很多网站,网站同时访问 可以使网站更安全(木马如果是root权限 如果webshell apache 可用 ...
- 暑期集训20190727 水(water)
[题目描述] 有一块矩形土地被划分成n×m个正方形小块.这些小块高低不平,每一小 块都有自己的高度.水流可以由任意一块地流向周围四个方向的四块地中,但 是不能直接流入对角相连的小块中. 一场大雨后,由 ...
- 【POJ3208】Apocalypse Someday
Description 666号被认为是神秘的"野兽之数",在所有以启示录为主题的大片中都是一个被广泛使用的数字.但是,这个数字666不能总是在脚本中使用,所以应该使用1666这样 ...
- python_day1(初始Python)
1.编码 ASCII (英文1字节,没中文)=> GB => GBK =>uncoode (中英文都2字节) => utf-8 (可变长字节储存,中文3字节,英文1字节) 2. ...
- linux下的压缩解压命令
.gz解压1:gunzip FileName.gz解压2:gzip -d FileName.gz压缩:gzip FileName .tar.gz 和 .tgz解压:tar zxvf FileName. ...