C#新手想问为什么我运行后会显示连接已重置~~~~~

我想将文件上传至阿里云的OSS中,点击button就会显示连接已重置,不知道是端口的问题还是其他的?请各位大神指教~

设置method="post" enctype="multipart/form-data",里面有一个 和上传按钮button。

protected void button_Click(object sender, EventArgs e)
{
HttpRequest request = System.Web.HttpContext.Current.Request;
HttpFileCollection FileCollect = request.Files; //获取成HttpFileCollection对象集合,集合中包含了上传所有内容,所有的上传的内容都在集合里了
if (FileCollect.Count > 0) //如果集合的数量大于0
{
foreach (string str in FileCollect)
{
HttpPostedFile file = FileCollect[str]; //用key获取单个文件对象HttpPostedFile
string Path = "/" + FileCollect.Get(str).FileName;
string AbsolutePath = Server.MapPath(Path);

MultipartUploadSample.UploadMultipart("cpu-maitian", file_value.Text.ToString(), AbsolutePath, 1024 * 1024);
}
}
}

}

namespace Aliyun.OpenServices.OpenStorageService.Samples
{
public class MultipartUploadSample
{
private const string _accessId = "";
private const string _accessKey = "";
private const string _endpoint = "http://oss-cn-beijing.aliyuncs.com";

    private static readonly OssClient _ossClient = new OssClient(_endpoint, _accessId, _accessKey);

    /// <summary>
    /// 分片上传。
    /// </summary>
    /// <param name="bucketName"><see cref="Bucket"/></param>
    /// <param name="objectName"><see cref="OssObject"/></param>
    /// <param name="fileToUpload">指定分片上传文件路径</param>
    /// <param name="partSize">分片大小(单位:字节)</param>
    public static void UploadMultipart(String bucketName, String objectName, String fileToUpload, int partSize)
    {
        var uploadId = InitiateMultipartUpload(bucketName, objectName);
        var partETags = BeginUploadPart(bucketName, objectName, fileToUpload, uploadId, partSize);
        var completeResult = CompleteUploadPart(bucketName, objectName, uploadId, partETags);
        Console.WriteLine("Upload multipart result : " + completeResult.Location);
    }

    /// <summary>
    /// 分片拷贝。
    /// </summary>
    /// <param name="targetBucket">目标<see cref="Bucket"/></param>
    /// <param name="targetKey">目标<see cref="OssObject"/></param>
    /// <param name="sourceBucket">源<see cref="Bucket"/></param>
    /// <param name="sourceKey">源<see cref="OssObject"/></param>
    /// <param name="partSize">分片大小(单位:字节)</param>
    public static void UploadMultipartCopy(String targetBucket, String targetKey, String sourceBucket, String sourceKey, int partSize)
    {
        var uploadId = InitiateMultipartUpload(targetBucket, targetKey);
        var partETags = BeginUploadPartCopy(targetBucket, targetKey, sourceBucket, sourceKey, uploadId, partSize);
        var completeResult = CompleteUploadPart(targetBucket, targetKey, uploadId, partETags);

        Console.WriteLine("Upload multipart copy result : ");
        Console.WriteLine(completeResult.Location);
    }

    private static string InitiateMultipartUpload(String bucketName, String objectName)
    {
        var request = new InitiateMultipartUploadRequest(bucketName, objectName);
        var result = _ossClient.InitiateMultipartUpload(request);
        return result.UploadId;
    }

    private static List<PartETag> BeginUploadPart(String bucketName, String objectName, String fileToUpload,
        String uploadId, int partSize)
    {
        var fileSize = -1;
        using (var fs = File.Open(fileToUpload, FileMode.Open))
        {
            fileSize = (int)fs.Length;
        }

        var partCount = fileSize / partSize;
        if (fileSize % partSize != 0)
        {
            partCount++;
        }

        var partETags = new List<PartETag>();
        for (var i = 0; i < partCount; i++)
        {
            using (FileStream fs = File.Open(fileToUpload, FileMode.Open))
            {
                var skipBytes = (long)partSize * i;
                fs.Seek(skipBytes, 0);
                var size = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
                var request = new UploadPartRequest(bucketName, objectName, uploadId);
                request.InputStream = fs;
                request.PartSize = size;
                request.PartNumber = i + 1;
                var result = _ossClient.UploadPart(request);
                partETags.Add(result.PartETag);
            }
        }
        return partETags;
    }

    private static List<PartETag> BeginUploadPartCopy(String targetBucket, String targetKey, String sourceBucket, String sourceKey,
        String uploadId, int partSize)
    {
        var metadata = _ossClient.GetObjectMetadata(sourceBucket, sourceKey);
        var fileSize = metadata.ContentLength;

        var partCount = (int)fileSize / partSize;
        if (fileSize % partSize != 0)
        {
            partCount++;
        }

        var partETags = new List<PartETag>();
        for (var i = 0; i < partCount; i++)
        {
            var skipBytes = (long)partSize * i;
            var size = (partSize < fileSize - skipBytes) ? partSize : (fileSize - skipBytes);
            var request =
                new UploadPartCopyRequest(targetBucket, targetKey, sourceBucket, sourceKey, uploadId);
            request.PartSize = size;
            request.PartNumber = i + 1;
            request.BeginIndex = skipBytes;
            var result = _ossClient.UploadPartCopy(request);
            partETags.Add(result.PartETag);
        }
        return partETags;
    }

    private static CompleteMultipartUploadResult CompleteUploadPart(String bucketName, String objectName,
        String uploadId, List<PartETag> partETags)
    {
        var completeMultipartUploadRequest =
            new CompleteMultipartUploadRequest(bucketName, objectName, uploadId);
        foreach (var partETag in partETags)
        {
            completeMultipartUploadRequest.PartETags.Add(partETag);
        }

        return _ossClient.CompleteMultipartUpload(completeMultipartUploadRequest);
    }
}

5个回答

将文件上传至阿里云的OSS中,你的程序中有登录吗?没通过身份验证连接当然会被重置了。

sinat_25213573
sinat_25213573 回复微wx笑: 好吧,那怎么升级带宽呢?
5 年多之前 回复
testcs_dn
微wx笑 回复sinat_25213573: 但如果你是0元试用的用户,没有升级过带宽的话,应该是无法通过程序去上传文件的。
5 年多之前 回复
testcs_dn
微wx笑 回复sinat_25213573: 设置了accessid 和accesskey就可以了,但如果你是0元试用的用户,没有升级过带宽的话,
5 年多之前 回复
sinat_25213573
sinat_25213573 为什么要升级带宽?我不太清楚到底怎么样才算登陆了那个OSS
5 年多之前 回复
testcs_dn
微wx笑 回复sinat_25213573: 你现在是免费体验的吧?升级带宽了没?
5 年多之前 回复
sinat_25213573
sinat_25213573 accessid 和accesskey我是直接设置在那个命名空间里的,这样算是登陆么?
5 年多之前 回复

网络的问题,特别是dns,检查下,ping下看看dns能不能解析。

sinat_25213573
sinat_25213573 网络是连着的
5 年多之前 回复

连接重置,是不是防火墙?

应该是弄了防火墙吧?

请参考以下页面的常见问题:
http://free.aliyun.com/?spm=5176.383518.278239.10.ASL9ay

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐