Forum General

Httpclient did not work.

Paras12Paras12 INMember ✭✭

Hello friends i am facing one issue.
in my application httpclient did not work for run wcf rest api while same api work in postman and ajax request.
i also debug my wcf service but i did not get any exception .
when i call api with httpclient then page never complete process it is indicate loading.

 private static async Task GetCustomerResult()
    {
        string response = null;
        using (var client = new HttpClient())
        {
            var uri = new Uri("http://localhost:62226/Data/HelloWorldTestData");
            var authData = string.Format("{0}:{1}", "testuser", "testpassword");
            var authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(authData));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue);
            var result = await client.GetAsync(uri);
        }
        Console.WriteLine(response);
    }

Answers

  • JohnHJohnH GBMember ✭✭✭✭✭

    @Paras12 said:
    Hello friends i am facing one issue.
    in my application httpclient did not work for run wcf rest api while same api work in postman and ajax request.
    i also debug my wcf service but i did not get any exception .
    when i call api with httpclient then page never complete process it is indicate loading.

     private static async Task GetCustomerResult()
        {
            string response = null;
            using (var client = new HttpClient())
            {
                var uri = new Uri("http://localhost:62226/Data/HelloWorldTestData");
                var authData = string.Format("{0}:{1}", "testuser", "testpassword");
                var authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(authData));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue);
                var result = await client.GetAsync(uri);
            }
            Console.WriteLine(response);
        }
    

    Are you sure localhost will resolve to your service on your pc? I would expect localhost to resolve to the device that is running the call. When testing such calls I either use machine name or ip address.

  • Paras12Paras12 INMember ✭✭

    yes in my localhost service successfully run in my postman application and I also tried to use Machine name and ip address but no luck

  • Paras12Paras12 INMember ✭✭

    Same Code work for live Hosted wcf api. I don't know that what am I missing?

  • JohnHJohnH GBMember ✭✭✭✭✭

    @Paras12 said:
    yes in my localhost service successfully run in my postman application

    Yes, localhost will work with postman because postman is running on your pc, right?

    and I also tried to use Machine name and ip address but no luck

    Ah fair enough. Is the device on the same wifi network?
    Can you open a web page in the device web browser hosted on your pc? Without using the app for now, just to prove what does work.

Sign In or Register to comment.