简体   繁体   中英

Unable to receive response from HTTP Post request to C# Web API from Windows Application

I'm having a C# Web API application hosted at a certain domain.
Using postman all web api's work as expected, including in the application consuming it.

Now I'm having a scenario where in I need to issue a POST request to the hosted C# Web API from a C# Windows Application .

I've tried Google(ing) this issue and am able to find only partial solution to my problem.

When I call my webapi using Postman; this is the actual/required JSON response that I get:

{
    "access_token": "7oIHQgUyb4NJc-kAc4Ce7Xa1ly5vKCaoxh0w-3PDixpvZtJkBLy7dg6kyE0gp_EZ_9pmh7LC06ztBKNyLbpLcruV9uHEVvIrvKvt0zowUv3Ho9sDGddvGy808N_tliljM8HYe4wJlcwlE9VzlWEv7bz3CNRNj_o9WLqPjcRTu2LN9wMAyMSXGdI_rE2guw-1w1W6aoDgr71x61InbRJ_DNqNpH0MhIUprhTeEVh59WEaq76FLErHTo3TXUTcjximVlxHQPZWh0gWN3dX7sI_Wz60RUK_h4SZO2VZ3QWZivPXX8CsOlXaullq0MnSLogb",
    "token_type": "bearer",
    "expires_in": 28799,
    "Username": "Gomesdenver",
    "UserId": "490253d0-79f6-4aed-96a9-5a041375d84c",
    "Role": "3b55f063-eb24-4e76-b30d-c2509c37fd8a",
    "DisplayAY": "2019-2020"
}


Please see image below (Postman request's response is what I'm seeking): 对JSON响应感兴趣

My C# Windows Code is as below:

public async Task<string> LoginUser(string Username, string Password, Notifier notification)
{
    string strResult = string.Empty;
    dynamic res;
    HttpClient client = new HttpClient();
    try
    {
        notification("Attempting to contact database. Please wait..");
        await Task.Delay(750);

        var dict = new Dictionary<string, string>();
        dict.Add("username", "GomesDenver");
        dict.Add("password", "MyPassword");
        dict.Add("grant_type", "password");

        client.BaseAddress = new Uri("http://dummyuri.com/");
        client.DefaultRequestHeaders
            .Accept
            .Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "token")
        {
            Content = new FormUrlEncodedContent(dict)
        };

        res = client.SendAsync(request).Result;
    }
    catch (Exception ex)
    {
        strResult = ex.Message.ToString();
    }
    finally
    {
        // Do nothing
    }

    return strResult;
}

Everything works as expected; but I'm not able to fetch the response as in Postman. The result that I get using this code is: 收到结果

I'm getting "OK" Status Code, but how can I access the JSON response from this request? How can I get the token value in my C# Windows Application code.

Thanks in advance.

Utilize the HttpResponseMessage Class

        HttpResponseMessage httpResponseMessage = await client.SendAsync(request);
        string response = await httpResponseMessage.Content.ReadAsStringAsync();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM