前久在用mjsip开发sip客户端的时候遇到401和407问题被卡了几天,所以在这做个记录。401我的解决是在注册失败并收到401错误时,把服务器返回的信息重新组装一次,再请求一次,部分代码如下:
- if (code==401) {
- Message req=transaction.getRequestMessage();
- req.setCSeqHeader(req.getCSeqHeader().incSequenceNumber());
- WwwAuthenticateHeader wah=resp.getWwwAuthenticateHeader();
- String qop_options=wah.getQopOptionsParam();
- printLog("DEBUG: qop-options: "+qop_options,LogLevel.MEDIUM);
- qop=(qop_options!=null)? "auth" : null;
- AuthorizationHeader ah=(new DigestAuthentication(SipMethods.REGISTER,req.getRequestLine().getAddress().toString(),wah,qop,null,username,passwd)).getAuthorizationHeader();
- req.setAuthorizationHeader(ah);
- TransactionClient t=new TransactionClient(sip_provider,req,this);
- t.request();
- }
if (code==401) {
Message req=transaction.getRequestMessage();
req.setCSeqHeader(req.getCSeqHeader().incSequenceNumber());
WwwAuthenticateHeader wah=resp.getWwwAuthenticateHeader();
String qop_options=wah.getQopOptionsParam();
printLog("DEBUG: qop-options: "+qop_options,LogLevel.MEDIUM);
qop=(qop_options!=null)? "auth" : null;
AuthorizationHeader ah=(new DigestAuthentication(SipMethods.REGISTER,req.getRequestLine().getAddress().toString(),wah,qop,null,username,passwd)).getAuthorizationHeader();
req.setAuthorizationHeader(ah);
TransactionClient t=new TransactionClient(sip_provider,req,this);
t.request();
}
同样的方式解决407问题,所不同的是在发出invite后要回应一个ack信息,代码如下:
- if(code==407){
- Message ack = MessageFactory.createRequest(sip_provider, SipMethods.ACK, to, from, null);
- ack.setCallIdHeader(req.getCallIdHeader());
- TransactionClient t=new TransactionClient(sip_provider,ack,this);
- t.request();
- }
- if ((code==407)){
- req.setCSeqHeader(req.getCSeqHeader().incSequenceNumber());
- WwwAuthenticateHeader wah;
- wah=msg.getProxyAuthenticateHeader();
- String qop_options=wah.getQopOptionsParam();
- qop=(qop_options!=null)? "auth" : null;
- RequestLine rl=req.getRequestLine();
- DigestAuthentication digest=new DigestAuthentication(rl.getMethod(),rl.getAddress().toString(),wah,qop,null,username,password);
- AuthorizationHeader ah;
- ah=digest.getProxyAuthorizationHeader();
- req.setAuthorizationHeader(ah);
- transactions.remove(tc.getTransactionId());
- tc=new TransactionClient(sip_provider,req,this);
- transactions.put(tc.getTransactionId(),tc);
- tc.request();
- }
if(code==407){
Message ack = MessageFactory.createRequest(sip_provider, SipMethods.ACK, to, from, null);
ack.setCallIdHeader(req.getCallIdHeader());
TransactionClient t=new TransactionClient(sip_provider,ack,this);
t.request();
}
if ((code==407)){
req.setCSeqHeader(req.getCSeqHeader().incSequenceNumber());
WwwAuthenticateHeader wah;
wah=msg.getProxyAuthenticateHeader();
String qop_options=wah.getQopOptionsParam();
qop=(qop_options!=null)? "auth" : null;
RequestLine rl=req.getRequestLine();
DigestAuthentication digest=new DigestAuthentication(rl.getMethod(),rl.getAddress().toString(),wah,qop,null,username,password);
AuthorizationHeader ah;
ah=digest.getProxyAuthorizationHeader();
req.setAuthorizationHeader(ah);
transactions.remove(tc.getTransactionId());
tc=new TransactionClient(sip_provider,req,this);
transactions.put(tc.getTransactionId(),tc);
tc.request();
}