gpt4 book ai didi

.net - 从旧引用更改方法

转载 作者:可可西里 更新时间:2023-11-01 17:10:56 24 4
gpt4 key购买 nike

public string Post(T obj)
{
HttpRequestMessage request = new HttpRequestMessage();
MediaTypeFormatter[] formatter = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };
var content = request.CreateContent<T>(obj, MediaTypeHeaderValue.Parse("application/json"), formatter, new FormatterSelector());
HttpResponseMessage response = client.PostAsync(this.url, content).Result;
return response.Content.ToString();
}

这是我在 HTTPClient 中使用的方法 Post,但有一个问题 - CreateContentFormatterSelector - 类来自旧引用。如何将此代码重写为最新引用:

using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using Newtonsoft.Json;

我明白是什么问题。这个方法是扩展方法!所以我无法使用它们。

最佳答案

你可以试试这段代码:

public async Task<string> Post<T>(T obj)
{
MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
HttpContent content = new ObjectContent<T>(obj, jsonFormatter);

var response = await client.PostAsync(this.Url, content);
return response.Content.ToString();
}

关于.net - 从旧引用更改方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15614956/

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