• 大小: 3KB
    文件类型: .cs
    金币: 1
    下载: 0 次
    发布日期: 2021-05-24
  • 语言: C#
  • 标签:

资源简介

C#HttpWebRequest大文件断点续传类,简洁,清晰易懂,大家有空可以下载拿去研究

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace Thunder8
{
    public class HttpUploader
    {
        const long partLength = 1024 * 1024 * 100;
        const string succuessFlag = “1“;

        public static string Upload(string fileName string httpUrllong seekPosition)
        {
            string result = string.Empty;
            FileStream fs = File.OpenRead(fileName);
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            Stream requestStream = null;
            Stream responseStream = null;

            try
            {
                long fileLength = fs.Length;
                long endPosition = seekPosition + partLength;

                request = (HttpWebRequest)HttpWebRequest.Create(httpUrl);
                request.Timeout = -1;
                request.ContentType = “application/octet-stream“;
                request.AddRange((int)seekPosition);
                requestStream = request.GetRequestStream();

                byte[] buffer = new byte[1024 * 10];
                fs.Seek(seekPosition SeekOrigin.Begin);

                int readLength = fs.Read(buffer 0 buffer.Length);
                while (seekPosition < endPosition && readLength > 0)
                {
                    requestStream.Write(buffer 0 readLength);
                    requestStream.Flush();
                    seekPosition += readLength;
                    readLength = fs.Read(buffer 0 buffer.Length);
                }

                response = (HttpWebResponse)request.GetResponse();
 

评论

共有 条评论

相关资源