Hello,
I tried to rewrite a C# HttpClient program from this GITHUB C# REST Client Sample.
https://github.com/dotnet/samples/tree/master/csharp/getting-started/console-webapiclient
The following is Program.cs: (I don’t need the repo.cs)
using System; using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; namespace WebAPIClient { class Program { private static readonly HttpClient client = new HttpClient(); static void Main(string[] args) { var token1 = SessinToken(); Console.WriteLine("Done get session token"); } private static async Task<string> SessinToken() { string Login_URL1 = "https://www.webserver1.com/api/login"; string Login_Data1 = "username=abc&password=abc123"; client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var Pay_Load1 = new StringContent(Login_Data1, Encoding.UTF8, "application/x-www-form-urlencoded"); var response = await client.PostAsync(Login_URL1, Pay_Load1); return await response.Content.ReadAsStringAsync(); } } }
In order to browse the webserver1.com, first I have to use HTTP POST send a payload string (username=XXX&password=YYYYYYY) using "application/x-www-form-urlencoded" format, then I should receive a string from the webserver1.com, which contains a session token value in JSon format. To make things easy to understand, I just want to return the string from the web server’s response.
The code got complied, but when I run it, I can see the returned token1 value is some thing like:
token1Id = 5, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"
System.Threading.Tasks.Task<string>
It seems the session token is not returned yet.
If I use Rest Client like Insomnia to get the session token using the same HTTP POST, it will take 1 or 2 seconds to get the session token, not very quick, but it is awaitable. However, using the above code, I never get any returned response.
The console application type is: Microsoft.NetCore.App(1.0.5); I installed .Net Core using this program: dotnet-sdk-2.1.302-win-gs-x64.exe downloaded from this URL:
Any suggestion?