gpt4 book ai didi

c# - .NET HTTP 客户端。如何发布字符串值?

转载 作者:IT王子 更新时间:2023-10-29 03:30:59 25 4
gpt4 key购买 nike

如何使用 C# 和 HttpClient 创建以下 POST 请求: User-Agent: Fiddler Content-type: application/x-www-form-urlencoded Host: localhost:6740 Content-Length: 6

我的 WEB API 服务需要这样的请求:

[ActionName("exist")]
[HttpPost]
public bool CheckIfUserExist([FromBody] string login)
{
return _membershipProvider.CheckIfExist(login);
}

最佳答案

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

class Program
{
static void Main(string[] args)
{
Task.Run(() => MainAsync());
Console.ReadLine();
}

static async Task MainAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:6740");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("", "login")
});
var result = await client.PostAsync("/api/Membership/exists", content);
string resultContent = await result.Content.ReadAsStringAsync();
Console.WriteLine(resultContent);
}
}
}

关于c# - .NET HTTP 客户端。如何发布字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15176538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com