gpt4 book ai didi

c# - 使用 JSON 发布请求,但服务器端有 Request.Form.Count == 0

转载 作者:行者123 更新时间:2023-11-30 23:01:45 25 4
gpt4 key购买 nike

我正在尝试使用 POST 方法将 JSON 从控制台应用程序发送到 WebPage.aspx这将对 JSON 进行重新定位,并对数据进行一些思考。请求状态正常,但在服务器端我得到空的 Request.form(不是 null 只是 count = 0)。但是服务器需要从中读取数据。我写的代码只是与另一个页面集成的原型(prototype),这就是为什么我需要在 Request.form 中发送数据。

你能帮忙修复我的帖子吗?

    public async static void KeepMeLogged()
{
string urlPart = "ReadJson";
string BaseUrl = "https://baseurl/external-api/"
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(BaseUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));

ForRequest json = new ForRequest
{
Login = Login
};

try
{
string stringJson = await Task.Run(() => JsonConvert.SerializeObject(json));
var httpContent = new StringContent(stringJson, Encoding.UTF8, "application/json");

using (var httpClient = new HttpClient())
{
// Do the actual request and await the response
var httpResponse = await httpClient.PostAsync(BaseUrl + urlPart, httpContent);

// If the response contains content we want to read it!
if (httpResponse.Content != null)
{
Console.WriteLine("IsOk");
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}

public class ForRequest
{
[JsonProperty("login")]
public string Login { get; set; }
}

//ServerSide, Request.Form.Count always 0
public partial class ReadJsonPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form != null)
{

foreach (string key in Request.Form)
{
//Request.Form.Count == 0
//do something
}
}

}
}

最佳答案

您可以使用此代码获取您的 json 数据:

Request.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
using (var sr = new System.IO.StreamReader(Request.InputStream))
{
string json = sr.ReadToEnd();
}

关于c# - 使用 JSON 发布请求,但服务器端有 Request.Form.Count == 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50857505/

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