如何用C#发送Ajax Get请求获取Epic Games订单历史?
Convert Python Epic Games Order History Request to C#
Got it, let's turn that Python HTTP request into a clean, idiomatic C# implementation using HttpClient—the standard library for making HTTP calls in modern C# applications. Here's a complete, working example that matches all the headers you provided:
Full Implementation Code
using System; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; public class EpicGamesOrderClient { // Use a static HttpClient instance (best practice for performance) private static readonly HttpClient _httpClient = new HttpClient(); static EpicGamesOrderClient() { // Map all your request headers to the HttpClient's default headers _httpClient.DefaultRequestHeaders.UserAgent.ParseAdd( "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36" ); _httpClient.DefaultRequestHeaders.Host = "www.epicgames.com"; _httpClient.DefaultRequestHeaders.Pragma.ParseAdd("no-cache"); _httpClient.DefaultRequestHeaders.Connection.ParseAdd("keep-alive"); _httpClient.DefaultRequestHeaders.Referrer = new Uri("https://www.epicgames.com/account/payment/history"); _httpClient.DefaultRequestHeaders.Add("x-requested-with", "XMLHttpRequest"); _httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true }; } /// <summary> /// Fetches order history from Epic Games' ajax endpoint /// </summary> /// <returns>Raw JSON response string, or null on failure</returns> public static async Task<string> FetchOrderHistoryAsync() { const string requestUrl = "https://www.epicgames.com/payment/ajaxGetOrderHistory"; try { HttpResponseMessage response = await _httpClient.GetAsync(requestUrl); // Throw an exception if the HTTP status code isn't successful (2xx range) response.EnsureSuccessStatusCode(); // Read the raw response content as a string return await response.Content.ReadAsStringAsync(); } catch (HttpRequestException ex) { Console.WriteLine($"Request failed: {ex.Message}"); return null; } } // Example usage in a console app public static async Task Main() { string orderHistory = await FetchOrderHistoryAsync(); if (orderHistory != null) { Console.WriteLine("Successfully retrieved order history:"); Console.WriteLine(orderHistory); } } }
Key Notes:
- Static HttpClient: We use a static instance because creating a new
HttpClientfor every request can exhaust socket connections. This is the recommended approach for long-running applications. - Header Mapping: Every header from your original Python code is explicitly set in the static constructor, ensuring the request matches exactly what your Python code sends.
- Async/Await: All HTTP operations use async/await for non-blocking I/O, which is essential for responsive applications.
- Error Handling: The
EnsureSuccessStatusCodecall will throw an exception for non-success status codes (like 401 Unauthorized, 404 Not Found), which we catch and handle gracefully.
Optional Enhancements:
- If you need to handle authenticated sessions (since Epic Games requires login for order history), you can add a
HttpClientHandlerwith cookie support (enabled by default) to persist session cookies between requests. - To parse the JSON response into strongly-typed objects, use libraries like
System.Text.Json(built into .NET Core/.NET 5+) orNewtonsoft.Json.
内容的提问来源于stack exchange,提问作者Kyle
相关产品推荐
相关产品推荐

