You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用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 HttpClient for 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 EnsureSuccessStatusCode call 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 HttpClientHandler with 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+) or Newtonsoft.Json.

内容的提问来源于stack exchange,提问作者Kyle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:31:10