0

I wanna get Token width windows console from web API and there is my code and I occurred an error:

An error occurred while sending the request,

I use visual studio 2013, .Net FrameWork 4.5,

private static async Task<string> GetAccessToken()
    {
        string baseUrl = "http://my.strategy.maskan";
        string _ClientId = "abc";
        string _ClientSecret = "password123";
        string _Scope = "api1";

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(baseUrl);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            List<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>();
            postData.Add(new KeyValuePair<string, string>("ClientId", _ClientId));
            postData.Add(new KeyValuePair<string, string>("ClientSecret", _ClientSecret));
            postData.Add(new KeyValuePair<string, string>("Scope", _Scope));


            FormUrlEncodedContent content = new FormUrlEncodedContent(postData);
            HttpResponseMessage response = new HttpResponseMessage();
            try
            {
                response = await client.PostAsync("IDENTITYSERVER", content);
            }
            catch (HttpRequestException ex)
            {
                Console.WriteLine(ex.Message);
            }
            string jsonString = await response.Content.ReadAsStringAsync();
            return jsonString;
            
        }
    }

exception produce in response = await client.PostAsync("IDENTITYSERVER", content);. how can I fix this exception from httpClient.PostAsync.

  • 1
    You need to provide more information. Inner exception in `HttpRequestException ex` would help as well as the `ex.StatusCode` – Athanasios Kataras Nov 14 '20 at 07:12
  • You need to look at the [InnerException](https://stackoverflow.com/questions/22826067/what-is-inner-exception) – John Wu Nov 14 '20 at 07:12
  • There's something else after *An error occurred while sending the request*: what is that? Connection closed by the remote endpoint? Post the complete exception. -- Where is this code run, what System? – Jimi Nov 14 '20 at 07:12
  • I solved that problem, I was in wrong LAN Network from my organization. So I Execute it in correct Domain Server. – Mohammad Ganji Nezhad Nov 15 '20 at 05:49

1 Answers1

0

This Error occurred because of, I posted request in wrong Domain server in my organization. I ping my Address server then find out that does not have any response to me, so I execute it to another client that has Access to this Domain name server and then I have response from server.

ping my.strategy.maskan

response from wrong host: Ping request could not find host -----. please check the name and try again

response from correct host: reply from -.-.-.- bytes=32 time<1ms TTL=62