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

如何在C#中模拟给定的Fetch POST请求?

在C#中模拟指定POST请求的实现方法

下面提供两种常用的实现方式,完全匹配你给出的fetch请求参数:

方法一:使用HttpClient(推荐,.NET Core/.NET 5+ 首选)

HttpClient是.NET官方推荐的现代HTTP客户端,性能和易用性更优,代码示例如下:

using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        // 目标请求地址
        string url = "http://172.16.1.1:8090/login.xml";
        
        // 构造请求体参数,对应fetch中的body内容
        var formData = new Dictionary<string, string>
        {
            {"mode", "191"},
            {"username", "21011012"},
            {"password", "4321"},
            {"a", "1668601840902"},
            {"producttype", "0"}
        };
        
        using (HttpClient client = new HttpClient())
        {
            // 设置请求头
            client.DefaultRequestHeaders.Accept.ParseAdd("*/*");
            client.DefaultRequestHeaders.AcceptLanguage.ParseAdd("en-US,en;q=0.9");
            
            // 构造POST请求内容
            var content = new FormUrlEncodedContent(formData);
            
            // 设置Referrer(对应fetch中的referrer)
            var request = new HttpRequestMessage(HttpMethod.Post, url)
            {
                Content = content,
                Headers = { Referrer = new Uri("http://172.16.1.1:8090/httpclient.html") }
            };
            
            // 发送请求并获取响应
            HttpResponseMessage response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode(); // 确保请求成功,否则抛出异常
            
            // 读取响应内容
            string responseBody = await response.Content.ReadAsStringAsync();
            Console.WriteLine(responseBody);
        }
    }
}

代码说明:

  • FormUrlEncodedContent 自动处理content-type: application/x-www-form-urlencoded格式,无需手动拼接字符串
  • Headers.Referrer 对应fetch中的referrer参数
  • credentials: omit 表示不发送Cookie,HttpClient默认行为符合该要求,无需额外配置
  • 采用async/await异步模式,避免阻塞主线程

方法二:使用WebClient(适用于.NET Framework旧版本)

如果项目基于.NET Framework,可使用WebClient实现:

using System;
using System.Net;

class Program
{
    static void Main(string[] args)
    {
        string url = "http://172.16.1.1:8090/login.xml";
        
        using (WebClient client = new WebClient())
        {
            // 设置请求头
            client.Headers[HttpRequestHeader.Accept] = "*/*";
            client.Headers[HttpRequestHeader.AcceptLanguage] = "en-US,en;q=0.9";
            client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            // 设置Referrer
            client.Headers[HttpRequestHeader.Referer] = "http://172.16.1.1:8090/httpclient.html";
            
            // 构造请求体参数
            var formData = new System.Collections.Specialized.NameValueCollection
            {
                {"mode", "191"},
                {"username", "21011012"},
                {"password", "4321"},
                {"a", "1668601840902"},
                {"producttype", "0"}
            };
            
            // 发送POST请求并获取响应
            byte[] responseBytes = client.UploadValues(url, "POST", formData);
            string responseBody = System.Text.Encoding.UTF8.GetString(responseBytes);
            
            Console.WriteLine(responseBody);
        }
    }
}

代码说明:

  • NameValueCollection 用于构造表单参数
  • UploadValues 方法自动处理表单编码和POST请求发送
  • HttpRequestHeader.Referer 对应fetch中的referrer参数

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 20:55:16