我目前正在开发一个应用程序,需要我能够使用那里的ID下载osubeatmap文件,我尝试将链接复制到下载,对我来说这就是 https://osu.ppy.sh/beatmapsets/1012634/download 然后只是向它发送一个网络请求并下载响应,但它并没有完全起作用,它生成了一个不是beatmap的200kb文件
我还发现了这个 GitHub osu! API问题https://github.com/ppy/osu-api/issues/288说要添加cookie,但我不知道如何让它在c#中工作。
我尝试过的代码:
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string downloadUrl = "https://osu.ppy.sh/beatmapsets/1012634/download";
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(downloadUrl);
if (response.IsSuccessStatusCode)
{
// Read the response content as a byte array
byte[] fileBytes = await response.Content.ReadAsByteArrayAsync();
// Save the downloaded file
string filePath = @"C:\Users\chead\Downloads\map.osz";
File.WriteAllBytes(filePath, fileBytes);
Console.WriteLine($"File downloaded successfully to: {filePath}");
}
else
{
Console.WriteLine($"Failed to download file. Status code: {response.StatusCode}");
}
}
Console.WriteLine("That's it!");
Console.ReadKey();
}
}
我尝试使用osu! API,但似乎没有任何端点可以下载beatmap
我还尝试了使用网络客户端下载文件 说要这样做,但没有成功
这是我尝试过的代码:
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
WebClient wc = new WebClient();
string strFile = Path.Combine(Directory.GetCurrentDirectory(), "downloaded_file.osz");
wc.Credentials = new System.Net.NetworkCredential("me", "pw");
wc.Headers.Add("User-Agent", "my user agent");
wc.DownloadFile("https://osu.ppy.sh/beatmapsets/1012634/download", strFile);
Console.WriteLine("That's it!");
Console.ReadKey();
}
}
我也尝试过这个(没用):
using System.Net;
class Program
{
static void Main(string[] args)
{
using (var client = new HttpClient())
{
using (var stream = client.GetStreamAsync("https://osu.ppy.sh/beatmapsets/1012634/download"))
{
using (var FileStream = new FileStream("map.osz", FileMode.OpenOrCreate))
{
stream.Result.CopyTo(FileStream);
Console.WriteLine($"Code Returned 0, saved to map.osz in the dir");
}
}
}
Console.ReadKey();
}
}
任何帮助将不胜感激。
尝试使用 CookieAwareWebClient(来自此答案:将 CookieContainer 与 WebClient 类一起使用)
static void Main(string[] args)
{
var wc = new CookieAwareWebClient(); // <------ CHANGE HERE
string strFile = Path.Combine(Directory.GetCurrentDirectory(), "downloaded_file.osz");
wc.Credentials = new System.Net.NetworkCredential("me", "pw");
wc.Headers.Add("User-Agent", "my user agent");
wc.DownloadFile("https://osu.ppy.sh/beatmapsets/1012634/download", strFile);
Console.WriteLine("That's it!");
}
public class CookieAwareWebClient : WebClient
{
private readonly CookieContainer m_container = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
HttpWebRequest webRequest = request as HttpWebRequest;
if (webRequest != null)
{
webRequest.CookieContainer = m_container;
}
return request;
}
}