问题描述
我需要知道如何添加授权饼干头的改造。我已经看到了类似的建议使用请求截取器等下面是什么,我想,但是这是正确的?首先,我已经需要一个RequestAdatper在第一时间获取会话ID左右。这只能通过请求适配器构建器来设置。但是,我需要做的请求只是为了获取会话ID在首位。我是否需要其余两个适配器之一获得的SessionID,我已经得着了一个又一个。我真正需要的是在接口的方法来设置cookie后,我得到它,但它似乎并没有出现这样的方法。这是越来越尴尬。这个问题很简单,普通不过。如何设置在改造授权cookie?我没有看到这个FAQ或教程。
RequestInterceptor requestInterceptor =新RequestInterceptor()
{
@覆盖
公共无效拦截(RequestFacade要求){
request.addHeader(设置Cookie,的sessionId =+ sessionIdentifier);
}
};
RestAdapter.Builder()setServer(serverURL使用).. setRequestIntercepter(requestIntercepter).build()。
//但是我没有的sessionId时,这是首次发布?????
请引用拦截器,并把它作为一个单身,就像您是 RestAdapter
本身。
公共类ApiHeaders实现RequestInterceptor {
私人字符串的sessionId;
公共无效setSessionId(字符串的sessionId){
this.sessionId =的sessionId;
}
公共无效clearSessionId(){
的sessionId = NULL;
}
@覆盖公共无效拦截(RequestFacade要求){
如果(的sessionId!= NULL){
request.setHeader(...);
}
}
}
现在,只要您的验证通话后称 setSessionId
。所有后续请求将包含头。
I need to know how to add an authorization cookie header in retrofit. I have seen advice like using request intercepter etc. Below is what I am trying, but is this correct? First of all I already needed a RequestAdatper to get the session id the first time around. This can only be set by the builder of the request adapter. But I needed to make a request just to get the session id in the first place. Do I need two rest adapters one to get the sessionId and another one after I have obtained it. What I really need is a method on adapter to set the cookie after I get it but it does not appear to be such a method. This is getting awkward. The question is simple and common enough. How do I set authorization cookie in retrofit? I don't see this in FAQ or tutorials.
RequestInterceptor requestInterceptor = new RequestInterceptor()
{
@Override
public void intercept(RequestFacade request) {
request.addHeader("Set-Cookie", "sessionId="+sessionIdentifier);
}
};
RestAdapter.Builder().setServer(serverURL)..setRequestIntercepter(requestIntercepter).build();
// but I don't have sessionId when this is first issued ?????
Keep a reference to the interceptor and treat it as a singleton like you would be RestAdapter
itself.
public class ApiHeaders implements RequestInterceptor {
private String sessionId;
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
public void clearSessionId() {
sessionId = null;
}
@Override public void intercept(RequestFacade request) {
if (sessionId != null) {
request.setHeader(...);
}
}
}
Now, simply call setSessionId
after your authentication call. All subsequent requests will include the header.
这篇关于翻新与放大器; AUTH COOKIE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!