Search code examples
c#authenticationxamarinodata

Adding JsessionID in Request Header


I have got the Jsessionid and I would like to add it to the Header, but I do not know where to add it.

Cookie jSessionID = client.ResponseCookies["JSESSIONID"];

      if (jSessionID != null)
       {
         // JSESSIONID
         sessionid = jSessionID.Value;

        var settings = new ODataClientSettings()
        {
            UrlBase = "MyURL" 
        };

        settings.BeforeRequest += delegate(HttpRequestMessage request)
        {
          String aux = String.Join(":", new String[] {"admin", "admin" });
          var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(aux);
          request.Headers.Add("Authorization", "Basic " + System.Convert.ToBase64String(plainTextBytes));
        };
        var client2 = new ODataClient(settings);
     }

Solution

  • The following code shows how to add additional headers to Simple.OData.Client:

    var settings = new ODataClientSettings {UrlBase = "http://localhost/odata"};
    settings.BeforeRequest += x =>
    {
        x.Headers.Add("context", "test");
    };
    
    var client = new ODataClient(settings);
    

    So basically you're doing it right. Is there anything that doesn't work? Can you trace HTTP communication and check that the header is set?