C#WebClient常见用法

System.Net.WebClient.DownloadFile(Uri address, String fileName)

namespace:System.Net

参数:

address:The URI from which to download data.
fileName:The name of the local file that is to receive the data.

eg:

 1 /// <summary>
 2 /// 保存文件到本地
 3 /// </summary>
 4 /// <param name="filePath">uri</param>
 5 /// <param name="folderPath">localDir</param>
 6 /// <param name="localFilePath">folderPath+fileName</param>
 7 public void SaveDownFile(string filePath, string folderPath, string localFilePath)
 8 {
 9     try
10     {
11         if (!Directory.Exists(folderPath))
12         {
13             Directory.CreateDirectory(folderPath);
14         }
15         WebClient DownFile = new WebClient();
16         DownFile.DownloadFile(filePath, localFilePath);
17         logger.WriteSystemLog(LogLevel.Const, "successfully saveDownFile:" + localFilePath);
18     }
19     catch (Exception ex)
20     {
21         logger.WriteExceptionLog(ex, " saveDownFile Exception: httpUrl=" + filePath);
22     }
23 }
 1 public static long userId = 1;
 2 public static string userCode;
 3 public static string token;
 4 public static string clientIP;
 5 // 单点登录
 6 protected void sso()
 7 {
 8     clientIP = GetClientIP();// local IP
 9     userId = GetUserId();
10     userCode = GetUserCode();
11     token = sendMessage(userId, userCode, clientIP);// 发送验证消息
12 
13     if (!string.IsNullOrEmpty(token))
14     {
15         delayTime(2);
16         simLogin(token);
17     }
18 }
19 
20 // 登录
21 private void simLogin(string token)
22 {
23     var url = string.Format("http://192.168.12.250:8900/Login?userId={0}&clientIP={1}&token={2}", userCode, clientIP, token);
24     WebClient wc = new WebClient();
25     byte[] ret = wc.DownloadData(url);
26 }
27 
28 private void delayTime(double secend)
29 {
30     DateTime tempTime = DateTime.Now;
31     while (tempTime.AddSeconds(secend).CompareTo(DateTime.Now) > 0)
32         System.Windows.Forms.Application.DoEvents();
33 }
原文地址:https://www.cnblogs.com/wuln/p/6231635.html