locked
HttpClient PostAsync not responding when use in GenericHandler RRS feed

  • Question

  • User1348063978 posted

    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 = "";
        }
    }

    Tuesday, July 3, 2018 1:43 PM

All replies

  • User475983607 posted

    Have you tried setting a breakpoint and simply debugging the code?

    The parm value is always empty.  Is that expected?

    SearchParameter parm = new SearchParameter();
    
    ApiResponse resp = await CallApi<ApiResponse>("http://localhost:50125/api/Test.ashx", parm);
    

    Tuesday, July 3, 2018 2:04 PM
  • User1348063978 posted

    i just try to show sample code. The parameter is not meaning, Test.ashx will return static ApiReponse data.

    Wednesday, July 4, 2018 1:43 AM
  • User1348063978 posted

    i tried to debug program until i found the line of problem, it is "PostAsync". This line will not responding,life time not responding, no error occurs.

    Wednesday, July 4, 2018 1:45 AM