none
HttpClient PostAsync not responding when use in GenericHandler RRS feed

  • Question

  • Hi, 

    I have a problem about using HttpClient PostAsync. Below sample code, I try to call restful service  from another one restful service. Both create in same asp.net site, the result is no responding but i can call target service manually(use browser) and i can use same code call target service from window console application. Any help?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Threading.Tasks;
    using Newtonsoft.Json;
    using System.Net.Http;
    using System.Net;
    using System.IO;
    
    namespace Api
    {
        /// <summary>
        /// Summary description for Gateway
        /// </summary>
        public class Gateway : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                try
                {
                    RunAsync(context).Wait();
                }
                catch (Exception ex)
                {
                    context.Response.Write(ex.Message);
                }
            }
    
            protected async Task RunAsync(HttpContext context)
            {
                SearchParameter parm = new SearchParameter();
    
                ApiResponse resp = await CallApi<ApiResponse>("http://localhost:50125/api/Test.ashx", parm);
    
                context.Response.ContentType = "text/javascript";
                context.Response.Write(JsonConvert.SerializeObject(resp));
            }
    
            public static async Task<T> CallApi<T>(string url, object reqData)
            {
                object ret = null;
                var jsonString = JsonConvert.SerializeObject(reqData);
    
                StringContent content = new StringContent(jsonString);
                HttpClient client = new HttpClient();
    
                HttpResponseMessage resp = await client.PostAsync(url, content);
    
                if (resp.StatusCode == HttpStatusCode.OK)
                {
                    Stream stream = await resp.Content.ReadAsStreamAsync();
                    StreamReader sr = new StreamReader(stream);
                    string responseText = sr.ReadToEnd();
                    sr.Dispose();
    
                    try
                    {
                        ret = JsonConvert.DeserializeObject<T>(responseText);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Invalid API response object.", ex);
                    }
                }
    
                return (T)ret;
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    
        public class ApiResponse
        {
            public string Code = "";
            public string Message = "";
            public string StackTrace = "";
            public string Api = "";
        }
    
        public class SearchParameter
        {
            public string SessionID = "0";
            public string Keyword = "";
        }
    }

    and this is the code that i call target service by console application

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.IO;
    using Newtonsoft.Json;
    
    namespace TestCallWebApi
    {
        public class ApiResponse
        {
            public string Code = "";
            public string Message = "";
            public string StackTrace = "";
            public string Api = "";
        }
    
        public class SearchParameter
        {
            public string SessionID = "0";
            public string Keyword = "";
        }
    
        class Program
        {
            
            static void Main()
            {
                RunAsync().Wait();
            }
    
            static async Task RunAsync()
            {
    
                SearchParameter p = new SearchParameter();
                ApiResponse r = await CallApi<ApiResponse>("http://localhost:50125/api/Test.ashx", p);
                Console.WriteLine($"Response Message:{r.Message}");
    
                Console.ReadLine();
            }
    
            static async Task<T> CallApi<T>(string url, object reqData)
            {
                object ret = null;
                var jsonString = JsonConvert.SerializeObject(reqData);
    
                StringContent content = new StringContent(jsonString);
                HttpClient client = new HttpClient();
                HttpResponseMessage resp = await client.PostAsync(url, content);
    
                if (resp.StatusCode == HttpStatusCode.OK)
                {
                    Stream stream = await resp.Content.ReadAsStreamAsync();
                    StreamReader sr = new StreamReader(stream);
                    string responseText = sr.ReadToEnd();
                    sr.Close();
    
                    try
                    {
                        ret = JsonConvert.DeserializeObject<T>(responseText);
                    }
                    catch
                    {
                    }
                }
    
                return (T)ret;
            }
    
        }
    }


    Tuesday, July 3, 2018 9:27 AM

Answers

All replies

  • I'd ask for help over here.

    https://forums.asp.net/

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Tuesday, July 3, 2018 12:30 PM
    Moderator
  • i can't use that forums T_T
    Tuesday, July 3, 2018 1:38 PM
  • i can't use that forums T_T

    There may be other asp.net forums you can search for. That is the only microsoft official forums supporting asp.net

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Tuesday, July 3, 2018 1:40 PM
    Moderator