C#WebClient常见用法

时间:2023-12-17 14:33:08

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:

 /// <summary>
/// 保存文件到本地
/// </summary>
/// <param name="filePath">uri</param>
/// <param name="folderPath">localDir</param>
/// <param name="localFilePath">folderPath+fileName</param>
public void SaveDownFile(string filePath, string folderPath, string localFilePath)
{
try
{
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
WebClient DownFile = new WebClient();
DownFile.DownloadFile(filePath, localFilePath);
logger.WriteSystemLog(LogLevel.Const, "successfully saveDownFile:" + localFilePath);
}
catch (Exception ex)
{
logger.WriteExceptionLog(ex, " saveDownFile Exception: httpUrl=" + filePath);
}
}
 public static long userId = ;
public static string userCode;
public static string token;
public static string clientIP;
// 单点登录
protected void sso()
{
clientIP = GetClientIP();// local IP
userId = GetUserId();
userCode = GetUserCode();
token = sendMessage(userId, userCode, clientIP);// 发送验证消息 if (!string.IsNullOrEmpty(token))
{
delayTime();
simLogin(token);
}
} // 登录
private void simLogin(string token)
{
var url = string.Format("http://192.168.12.250:8900/Login?userId={0}&clientIP={1}&token={2}", userCode, clientIP, token);
WebClient wc = new WebClient();
byte[] ret = wc.DownloadData(url);
} private void delayTime(double secend)
{
DateTime tempTime = DateTime.Now;
while (tempTime.AddSeconds(secend).CompareTo(DateTime.Now) > )
System.Windows.Forms.Application.DoEvents();
}