C# HttpWebRequest向远程地址Post文件
HttpWebRequest向远程地址Post文件
/// <summary>
/// 上传文件到远程服务器
/// </summary>
/// <param name="url">远程服务器接收Api</param>
/// <param name="postStream">文件流</param>
/// <param name="contentType">内容类型默认application/octet-stream</param>
/// <returns></returns>
static public string PostFileByHttpWebRequest(string url, Stream postStream, string contentType = "application/octet-stream")
{
string result = string.Empty;
HttpWebRequest request = null;
try
{
ServicePointManager.DefaultConnectionLimit = int.MaxValue;
request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = ;
request.Method = "POST";
request.ContentType = contentType;
request.ContentLength = postStream.Length; using (Stream writer = request.GetRequestStream())
{
WriteTo(postStream, writer, postStream.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
result = reader.ReadToEnd();
}
}
}
catch (WebException ex)
{
request.Abort();
result = ex.Message.ToString();
}
catch (Exception ex)
{
request.Abort();
result = ex.Message.ToString();
}
finally
{
if (request != null)
{
request.Abort();
}
}
return result;
} static private long WriteTo(Stream orignStream, Stream destStream, long totalSize)
{
int BufferSize = * ; var buffer = new byte[BufferSize]; long alreadyRead = ;
while (alreadyRead < totalSize)
{
var readSize = orignStream.Read(buffer, , BufferSize);
if (readSize <= )
break; if (alreadyRead + readSize > totalSize)
readSize = (int)(totalSize - alreadyRead);
alreadyRead += readSize;
destStream.Write(buffer, , readSize);
}
destStream.Flush(); return alreadyRead;
}
C# HttpWebRequest向远程地址Post文件的更多相关文章
- C# HttpWebRequest请求远程地址获取返回消息
HttpWebRequest请求远程地址获取返回消息 /// <summary> /// 请求远程Api获取响应返回字符串 /// </summary> /// <par ...
- java实现远程储存读取文件
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileN ...
- go 语言的库文件放在哪里?如何通过nginx代理后还能正确获取远程地址
/usr/local/Cellar/go/1.5.1/libexec/src/ 他的RemoteAddr 是从哪里获取? func (c *conn) RemoteAddr() Addr { if ! ...
- phpcms—— 内容中的附件调用和添加远程地址的调用
phpcms中几个地址调用的方法 1,CSS路径有{CSS_PATH}2,图片路径有{IMG_PATH}3,JS路径有{JS_PATH} 4,那么附件的路径如何调用,使用下面的方式可以得到附件的路径前 ...
- Window2003、xp远程备份数据库文件(xcopy+rar+pscp)
Window2003.xp远程备份数据库文件 xcopy+rar+pscp .bat脚本 Eg: xcopy d:\dbtest\*.* d:\dbtemp\ /y D:\backup\Rar.exe ...
- 关于在git添加远程地址的过程中遇到的问题
问题产生的过程 我根据菜鸟教程的步骤,做了如下操作: 1.打开安装文件夹中的git-bash程序 2.设置username和email 3.添加远程地址 结果如下: 之后通过百度知道要先git ini ...
- git项目远程地址修改后本地如何处理
今天运维人员为了方便管理,将远程的项目地址给迁移了, 原来是 git@git.lalala.com:yuanlaide/happy.git 变成了 git@git.lalala.com:houlaid ...
- Delphi实例分析:远程传输数据和文件
在Windows操作系统的平台上,WinSock是首选的网络编程接口,用于在网络上传输数据和交换信息,它构成了Windows操作系统进行网络编程的基础.对于编写网络应用程序来说,WinSock是一门非 ...
- 根据第三方提供的webservice地址获取文件信息
import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.en ...
随机推荐
- python 字符串(str)和列表(list)的互相转换
1.str to list str1 = "12345"list1 = list(str1)print list1 str2 = "123 sjhid dhi" ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- C语言处理CSV数据
以下代码为博客 <Python的并行求和例子>: http://www.cnblogs.com/instant7/p/4312786.html 中并行python代码的C语言重写版. 用C ...
- python之scrapy模块scrapy-redis使用
1.redis的使用,自己可以多学习下,个人也是在学习 https://www.cnblogs.com/ywjfx/p/10262662.html官网可以自己搜索下. 2.下载安装scrapy-red ...
- PCL点云库(Point Cloud Library)简介
博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=29 什么是PCL PCL(Point Cloud Library)是在吸收了 ...
- Scala API - 集合
- Linux Shell 自动备份脚本
写一个使用shell脚本增量备份系统文件,顺便复习一下shell脚本相关的命令,这个脚本可以根据自己的需求来备份不同的文件或者文件夹,进行完整备份和增量备份. 参考的网址:http://blog.51 ...
- Cgroup与LXC简介
原文地址: https://blog.51cto.com/speakingbaicai/1359825 一.Docker.LXC.Cgroup的结构关系 根据Docker布道师Jerome Petaz ...
- DP经典问题—————(LCIS)最长公共上升子序列
这道题是LIS(最长上升子序列)与LCS(最长公共子序列)问题的综合版本,有关这两个问题可以看一下我的文章:https://www.cnblogs.com/myhnb/p/11305551.html ...
- Nmap之用法
简介 Nmap是一款开源免费的网络发现(Network Discovery)和安全审计(Security Auditing)工具.软件名字Nmap是Network Mapper的简称. 功能架构图 N ...