简体   繁体   English

PostAsync() 方法在 dot-net 核心窗口服务中不起作用

[英]PostAsync() method is not working in dot-net core window service

I am trying the run the console application as a window service in dot-net core and able to create ,start ,stop and delete the service.我正在尝试将控制台应用程序作为dot-net core 中的窗口服务运行,并且能够创建、启动、停止和删除该服务。
I am using PostAsync() method in application but the issue is that this method is working perfectly in console application but in window service most of the times PostAsync() never returned any response.我在应用程序中使用PostAsync()方法,但问题是这种方法在控制台应用程序中运行良好,但在窗口服务中大多数时候PostAsync()从未返回任何响应。

Any help would be highly appreciated.任何帮助将不胜感激。 Thanks!谢谢!

string abc=\"firstparameter\":\"English\",\"secondParameter\":\"Hindi\",\"Marks\":\"Result \"}";
var response = await client.PostAsync("url", new StringContent(ab, Encoding.UTF8, "application/json")).ConfigureAwait(false))

and By this way并且通过这种方式

var response = client.PostAsync("url", content).Result;
var objResult = response.Content.ReadAsStringAsync().Result;

Here is an example of how you can use it.这是一个如何使用它的示例。 In this example I have used HttpClientFactory to create a HttpClient.在这个例子中,我使用 HttpClientFactory 创建了一个 HttpClient。

var userApi = httpClientFactory.CreateClient("UserApi");
var request = new HttpRequestMessage(HttpMethod.Post, "systemuser/login");
request.Content = new StringContent(JsonConvert.SerializeObject(loginRequest), Encoding.UTF8, "application/json");

var response = await userApi.SendAsync(request);

if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
    var body = await response.Content.ReadAsAsync<LoginResponse>();
    return Ok(body);
}
return StatusCode((int)response.StatusCode);

Hope this helps希望这可以帮助

I know this is a late response but just in case this could help someone here is my answer.我知道这是一个迟到的回复,但以防万一这可以帮助这里的人,这是我的答案。

StringContent is not a good option when doing Post.在进行 Post 时, StringContent不是一个好的选择。 You may want to use FormUrlEncodedContent :您可能想要使用FormUrlEncodedContent

var data = new Dictionary<string, string>
{
    { "firstparameter", "firstparametervalue" },
    { "secondparameter", "secondparametervalue" }
};

var content = new FormUrlEncodedContent(data);

var response = await client.PostAsync(apiUri, content);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从动态 URL 中读取 JSON 并需要填充到 Dot-net Core MVC 下拉列表中 - Read JSON from Dynamic URL and need to populate in Dot-net Core MVC Dropdownlist C#/dot-NET 的字体渲染库? - Font rendering libraries for C# / dot-NET? 如何通过点网运行Selenium API(使用C#) - How to run selenium API with dot-net (using c#) 从WCF中通过点网创建的getDataAsync函数获取错误 - Getting an error from a dot-net created function of getDataAsync in WCF 在 window worker service dot.net core 中,当我发布时比得到数据库错误否则它有效吗? - In window worker service dot net core when i publish than getting database error otherwise it works? 在 window worker service dot.net core 连接到数据库时出现错误? - In window worker service dot net core while connectiong to database i am getting error? 我尝试使用 dot-net 生成 X509Certificate2 密钥对,但缺少私钥 - I try to generate a X509Certificate2 key pair with dot-net but private key is missing dot net core 3.1 web API的POST方法在ajax调用中不起作用 - POST method of dot net core 3.1 web API not working in ajax call 从捆绑的 Windows 服务中获取 Dot Net Core 框架位置? - Obtain the Dot Net Core Framework Location from a Bundled Windows Service? 在点网核心中初始化后台服务的正确方法 - Proper way to initialize a background service in dot net core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM