在请求头中添加 JsessionID
Adding JsessionID in Request Header
我得到了Jsessionid,我想把它加到Header中,但我不知道加在哪里。
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);
}
以下代码显示了如何向 Simple.OData.Client 添加额外的 header:
var settings = new ODataClientSettings {UrlBase = "http://localhost/odata"};
settings.BeforeRequest += x =>
{
x.Headers.Add("context", "test");
};
var client = new ODataClient(settings);
所以基本上你做对了。有什么不起作用的吗?您能否跟踪 HTTP 通信并检查是否设置了 header?
我得到了Jsessionid,我想把它加到Header中,但我不知道加在哪里。
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);
}
以下代码显示了如何向 Simple.OData.Client 添加额外的 header:
var settings = new ODataClientSettings {UrlBase = "http://localhost/odata"};
settings.BeforeRequest += x =>
{
x.Headers.Add("context", "test");
};
var client = new ODataClient(settings);
所以基本上你做对了。有什么不起作用的吗?您能否跟踪 HTTP 通信并检查是否设置了 header?