• 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 = 50000;
                    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 = 4 * 1024;
    
                var buffer = new byte[BufferSize];
    
                long alreadyRead = 0;
                while (alreadyRead < totalSize)
                {
                    var readSize = orignStream.Read(buffer, 0, BufferSize);
                    if (readSize <= 0)
                        break;
    
                    if (alreadyRead + readSize > totalSize)
                        readSize = (int)(totalSize - alreadyRead);
                    alreadyRead += readSize;
                    destStream.Write(buffer, 0, readSize);
                }
                destStream.Flush();
    
                return alreadyRead;
            }
    View Code
  • 相关阅读:
    HTTP 错误 404.2
    SQL Server 2008 R2如何开启数据库的远程连接(转)
    CSS中font-family:中文字体对应的英文名称
    15/18位身份证号码正则表达式(详细版)
    C#获取系统时间及时间格式
    C#正则表达式判断输入日期格式是否正确
    Linux查看机器负载
    模拟HTTP请求超时时间设置
    MySQL show命令的用法
    innodb事务隔离级别
  • 原文地址:https://www.cnblogs.com/hofmann/p/10805492.html
Copyright © 2020-2023  润新知