mirror of
https://ghfast.top/https://github.com/zsjy/AdbTools.git
synced 2025-03-14 23:13:10 +08:00
60 lines
2.0 KiB
C#
60 lines
2.0 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.IO;
|
|
|
|
namespace Update.AccessInterface
|
|
{
|
|
/// <summary>
|
|
/// post请求
|
|
/// </summary>
|
|
public static class RequestJson
|
|
{
|
|
|
|
public static void DownloadFile(string url, string savePath)
|
|
{
|
|
try
|
|
{
|
|
// 创建 HttpWebRequest 对象
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
request.Method = "GET";
|
|
|
|
// 发送请求并获取响应
|
|
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
|
using (Stream responseStream = response.GetResponseStream())
|
|
using (FileStream fileStream = new FileStream(savePath, FileMode.Create, FileAccess.Write))
|
|
{
|
|
// 缓冲区大小(可以根据需要调整)
|
|
byte[] buffer = new byte[4096];
|
|
int bytesRead;
|
|
|
|
// 从响应流中读取数据并写入文件
|
|
while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
|
|
{
|
|
fileStream.Write(buffer, 0, bytesRead);
|
|
}
|
|
}
|
|
|
|
Console.WriteLine($"File downloaded and saved to: {savePath}");
|
|
}
|
|
catch (WebException ex)
|
|
{
|
|
Console.WriteLine($"WebException: {ex.Message}");
|
|
if (ex.Response != null)
|
|
{
|
|
using (var errorResponse = (HttpWebResponse)ex.Response)
|
|
using (var reader = new StreamReader(errorResponse.GetResponseStream()))
|
|
{
|
|
string errorText = reader.ReadToEnd();
|
|
Console.WriteLine($"Error details: {errorText}");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|