1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| package com.example.SipTest;
import javax.microedition.io.Connector;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.sip.SipClientConnection;
import javax.microedition.sip.SipConnectionNotifier;
import javax.microedition.sip.SipHeader;
public class Register extends MIDlet {
//send the REGISTER and handle the authentication responses
public void doRegister(String username, String password, String realm) {
SipClientConnection scc = null;
SipConnectionNotifier scn = null;
String contact = null;
try {
// open listener in application specific port 5080
scn = (SipConnectionNotifier) Connector.open("sip:5080");
// build the contact URI
contact =
new String("sip:user@"+scn.getLocalAddress()+":"+scn.getLocalPort());
System.out.println("contact:"+contact);
// open client connection to the SIP registrar
scc = (SipClientConnection) Connector.open("sip:ippi.fr");
// initialize REGISTER with appropriate headers
scc.initRequest("REGISTER", scn);
scc.setHeader("From", "sip:taoufik109@ippi.fr");
scc.setHeader("To", "sip:ouahbi1986@ippi.fr");
scc.setHeader("Contact", contact);
scc.send();
boolean handled = false;
int scode = 0;
while(!handled) {
SipHeader sh;
// wait max 15 secs for response
scc.receive(15000);
scode = scc.getStatusCode();
switch(scode)
{
case 401:
sh = new SipHeader("WWW-Authenticate",
scc.getHeader("WWW-Authenticate"));
realm = sh.getParameter("realm");
// here for example, prompt user for password for this realm
// set credentials to initiate re-REGISTER
scc.setCredentials("mon username","mon password","ippi.fr");
System.out.println("401");
break;
case 407:
sh = new SipHeader("Proxy-Authenticate",
scc.getHeader("Proxy-Authenticate"));
realm = sh.getParameter("realm");
// here for example, prompt user for password for this realm
// set credentials to initiate re-REGISTER
scc.setCredentials("mon username","mon password","ippi.fr");
System.out.println("407");
break;
case 200:
// handle OK response
handled = true;
System.out.println("200");
break;
default:
// handle other responses
handled = true;
System.out.println("000");
}
}
scc.close();
} catch(Exception ex) {
// handle Exceptions
}
}
public Register() {
// TODO Auto-generated constructor stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
doRegister("taoufik109", "taoufik2010", "ippi.fr");
}
} |
Partager