zoukankan      html  css  js  c++  java
  • Webclient

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

    namespace ConsoleApp1
    {
    class Program
    {
    static void Main(string[] args)
    {
    TestWebclient testWebclient = new TestWebclient();
    testWebclient.WebClientUpload();
    //WebClientDownload();
    //WebClientUpload();
    //WebClientDelete();
    Console.ReadKey();
    }

    #region 下载
    /// <summary>
    /// 下载
    /// </summary>
    static void WebClientDownload()
    {
    WebClient webClient = new WebClient
    {
    Credentials = CredentialCache.DefaultCredentials
    };
    //Uri _uri = new Uri(@"http://localhost:8082/123.txt");
    Uri uri = new Uri(@"http://192.168.0.100:8082/123.txt");
    webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
    webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted;
    webClient.DownloadFileAsync(uri, @"D:download123.txt");
    }

    private static void WebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
    Console.WriteLine("下载完成...");
    }

    private static void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
    Console.WriteLine($"{e.ProgressPercentage}:{e.BytesReceived}/{e.TotalBytesToReceive}");
    }

    #endregion

    #region 上传

    /// <summary>
    /// 上传
    /// </summary>
    static void WebClientUpload()
    {
    WebClient webClient = new WebClient
    {
    Credentials = new NetworkCredential("test", "123")
    };
    Uri uri = new Uri(@"http://192.168.0.100:8082/456.xlsx");
    webClient.UploadProgressChanged += WebClient_UploadProgressChanged;
    webClient.UploadFileCompleted += WebClient_UploadFileCompleted;
    webClient.UploadFileAsync(uri, "PUT", @"D:download456.xlsx");
    }

    private static void WebClient_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
    {
    Console.WriteLine("上传完成...");
    }

    private static void WebClient_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
    {
    Console.WriteLine($"{e.ProgressPercentage}:{e.BytesSent}/{e.TotalBytesToSend}");
    }
    #endregion

    #region 删除
    /// <summary>
    /// 删除
    /// </summary>
    static void WebClientDelete()
    {
    WebClient webClient = new WebClient
    {
    Credentials = new NetworkCredential("test", "123")
    };
    Uri uri = new Uri(@"http://192.168.0.100:8082/456.xlsx");
    webClient.UploadDataCompleted += WebClient_UploadDataCompleted;
    webClient.UploadDataAsync(uri, "DELETE", new byte[0]);
    }

    private static void WebClient_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e)
    {
    Console.WriteLine("已删除...");
    }
    #endregion
    }

    }

  • 相关阅读:
    随手乱记
    对拍程序
    生命游戏
    Command Operating System by cdsidi(ComSys) 0.2.x版本陆续更新
    C语言<stdio.h>的rename函数——重命名文件、更改文件路径或更改目录名
    C++ 类中的static 成员函数
    Command Operating System by cdsidi(ComSys) 0.1.x版本陆续更新
    Command Operating System by cdsidi (ComSys)首次发布(版本0.1.2)
    区间dp之 "石子合并"系列(未完结)
    C/C++快读(快速读入)有多——安全AC
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/11198523.html
Copyright © 2011-2022 走看看