个人收藏--未整理—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 ####################################################### ...
随机推荐
- ORM之多表操作
一.创建模型 from django.db import models # Create your models here. class Book(models.Model): nid = model ...
- Android_Fragment栈操作 commit()问题分析
栈操作时遇到一个问题 getFragmentManager().beginTransaction() .replace(R.id.fl_container,bFragment) .addToBackS ...
- Nexus安装(Windows)
1. nexus下载 官网下载:https://www.sonatype.com/download-oss-sonatype 网盘下载:https://pan.baidu.com/s/1CXOW7Lv ...
- Luogu P1816 忠诚
rmq模板题.用st表切一个. 关于st表的详解见我的博客:st表.树状数组与线段树 笔记与思路整理 题目描述 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家 ...
- [考试反思]1108csp-s模拟测试105: 傀儡
评测机是真的老了... 我的脑力也老了... 昨天写完T3之后感觉脑子就留在那了,直到现在还感觉自己神志不清... T1OJ上过了(跑得挺慢但是的确过了),但是文件评测同样是开O2居然只剩下70分.. ...
- [考试反思]1015csp-s模拟测试74:压迫
其实同时也是第27,一大片并列的. 真的是越考越烂. T1是个弱化的贪心原题,15分钟拿下没什么可说的. T2打的记忆化搜索,hash_mod太小撞哈希了,50->30 T3,想不到正解,90分 ...
- C/c.pp:贪心,二分答案
说是贪心有点牵强. 其次,答案满足单调性,如果在k次操作能完成那么在k+1次操作内也能完成. 因为大不了你就把多的一次对方操作再进行一次就好了. 怎么操作呢? 我们从头扫这个序列,遇到每一个不匹配位置 ...
- 划艇:dp/组合数/区间离散化
Description 在首尔城中,汉江横贯东西.在汉江的北岸,从西向东星星点点地分布着 N 个划艇学校,编号依次为 1 到 N.每个学校都拥有若干艘划艇.同一所学校的所有划艇颜色相同,不同的学校的划 ...
- Linux下安装jdk8步骤
作为Java开发人员,在Linux下安装一些开发工具是必备技能,本文以安装jdk为例,详细记录了每一步的操作命令,以供参考. 下载jdk8 登录网址:http://www.oracle.com/tec ...
- Mybatis精讲(一)---环境配置及架构梳理
目录 简介 ORM模型 Hibernate Ibatis 环境搭建 jar 配置 xml方式配置 代码方式配置 两种方式对比 Mybatis结构 源码解读xml环境加载 映射器解读 Ibatis # ...