locked
Header get deleted my HTTPClient GetAsync RRS feed

  • Question

  • User-1641476077 posted

    I am writing an page to display some data to user. The data is retrieve from a server via API call. The API required me to insert a header with key Cookie followed by some value i got calling another login API. When i run the API, i realized that the header with key Cookie keep getting deleted. Need help on how do i prevent from this header from being deleted

    Code.

    protected static async Task<string> GetData()
    {
    
        string responseString = string.Empty;
        string data = string.Empty;
        string host = ConfigurationManager.AppSettings["host"];
        string dataUrl = ConfigurationManager.AppSettings["dataUrl"];
    
        try
        {
            using (HttpClient client = new HttpClient())
            {
    
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Add("Connection", "close");
    
                //this header keep getting deleted
                client.DefaultRequestHeaders.Add("Cookie", cookie);
                
                client.DefaultRequestHeaders.Add("Host", host);
    
                HttpResponseMessage response = client.GetAsync(dataUrl).Result;
    
                if (response.IsSuccessStatusCode)
                {
    
                    byte[] responseContent = await response.Content.ReadAsByteArrayAsync();                   
                    responseString = Encoding.UTF8.GetString(responseContent);                   
    
                    if (!string.IsNullOrEmpty(responseString))
                    {
                        var jo = JObject.Parse(responseString);
    
                        data = jo["data"]["all"].ToString();
                    }
                }               
            }
        }
        catch (HttpRequestException httpE)
        {
            message += "exception: " + httpE.Message + "<br />";
            message += "Stack Trace: " + httpE.StackTrace + "<br />";
    
            cookie = string.Empty;
        }
        catch (Exception exe)
        {
            message += "exception: " + exe.Message + "<br />";
            message += "exception: " + exe.StackTrace + "<br />";
            cookie = string.Empty;
        }
        return responseString;
    }

    Monday, January 14, 2019 8:56 AM

Answers

  • User-1641476077 posted

    Hi, i fixed my issue. seems that using myHttpWebRequest will fixed the error.

    updated code is 

    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(dataUrl);
    myHttpWebRequest.Accept = "application/json";
    myHttpWebRequest.Host = host;
    WebHeaderCollection myWebHeaderCollection = myHttpWebRequest.Headers;
    myWebHeaderCollection.Add("Cookie:"+cookie);
    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 15, 2019 4:39 AM

All replies

  • User-474980206 posted

    cookies are part of the request payload. you will need to send it with every request. not sure what you mean by deleted.

    Monday, January 14, 2019 5:07 PM
  • User753101303 posted

    Hi,

    And cookie comes from where ? Also you do "Empty" them if an exception happens (regardless of which exception happens) and you make hard to see if an exception happened by turning the exception into a test for the "message" string which is easy to forget.

    Monday, January 14, 2019 5:39 PM
  • User-1641476077 posted

    Hi,

    for the request, no exception occur, the cookie is get from a login api. upon authenticate successful, the api will return me a string, i am required to insert this string into the request reader and the key for this header need to be "Cookie".

    Not sure if this cookie refer to our usual cookie thou.

    i wrote another code on one server to test header received, and this is what i got from my above code

    Key: Connection
    Value 0: Close
    Key: Accept
    Value 0: application/json
    Key: Host
    Value 0: www.myserver.com
    

    Tuesday, January 15, 2019 1:35 AM
  • User-1641476077 posted

    Hi, i fixed my issue. seems that using myHttpWebRequest will fixed the error.

    updated code is 

    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(dataUrl);
    myHttpWebRequest.Accept = "application/json";
    myHttpWebRequest.Host = host;
    WebHeaderCollection myWebHeaderCollection = myHttpWebRequest.Headers;
    myWebHeaderCollection.Add("Cookie:"+cookie);
    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 15, 2019 4:39 AM