[英]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.