Prova così allora
codice:
public static void main(String[] args) {
String s = "SIP/2.0 401 Unauthorized"+
"From: <sip:test@192.168.1.107>"+
"To: <sip:test@192.168.1.107>;tag=as1a02a93a"+
"Call-ID: 2537450899"+
"CSeq: 1 REGISTER"+
"User-Agent: Asterisk PBX"+
"Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY"+
"WWW-Authenticate: Digest algorithm=MD5, realm=\"asterisk\", nonce=\"2a78aad2\""+
"Content-Length: 0";
String realm = s.split("realm=")[1];
StringBuffer output = new StringBuffer();
int i = 0;
while( i < realm.lastIndexOf("Content-Length") ){
output.append(realm.charAt(i));
i++;
}
System.out.println("realm=" + output);
}
Oppure in modo più semplice
codice:
public static void main(String[] args) {
String s = "SIP/2.0 401 Unauthorized"+
"From: <sip:test@192.168.1.107>"+
"To: <sip:test@192.168.1.107>;tag=as1a02a93a"+
"Call-ID: 2537450899"+
"CSeq: 1 REGISTER"+
"User-Agent: Asterisk PBX"+
"Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY"+
"WWW-Authenticate: Digest algorithm=MD5, realm=\"asterisk\", nonce=\"2a78aad2\""+
"Content-Length: 0";
StringBuffer output = new StringBuffer();
int i = s.lastIndexOf("realm=");
while( i < s.lastIndexOf("Content-Length") ){
output.append(s.charAt(i));
i++;
}
System.out.println(output);
}