1 package de.j32.avmfritz;
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.OutputStream;
8 import org.xml.sax.SAXException;
10 import de.j32.httplib.HttpGETRequest;
11 import de.j32.httplib.HttpPOSTRequest;
12 import de.j32.httplib.HttpRequest;
13 import de.j32.httplib.HttpResponse;
14 import de.j32.util.Util;
21 public FritzBox(String password, String url)
22 throws SAXException, IOException
26 HttpResponse response = null;
28 response = httpGet("cgi-bin/webcm")
29 .addParameter("getpage", "../html/login_sid.xml")
31 LoginXML loginxml = new LoginXML(response);
35 if (loginxml.iswriteaccess()) {
36 sid_ = loginxml.sid();
40 response = httpPost("cgi-bin/webcm")
41 .addParameter("getpage", "../html/login_sid.xml")
42 .addParameter("var:lang", "de")
43 .addParameter("login:command/response",
44 loginxml.response(password)).execute();
45 loginxml = new LoginXML(response);
49 if (!loginxml.iswriteaccess())
50 throw new RuntimeException("FritzBox login failed");
52 sid_ = loginxml.sid();
55 Util.nothrowClose(response);
60 public InputStream exportAddressbook()
63 return httpPostMultipart("cgi-bin/firmwarecfg")
64 .addParameter("sid", sid_)
65 .addParameter("PhonebookId", "0")
66 .addParameter("PhonebookExportName", "Telefonbuch")
67 .addParameter("PhonebookExport", "").execute();
70 public OutputStream importAddressbook()
73 return new ByteArrayOutputStream() {
77 System.out.println("sending to fritzbox");
78 httpPostMultipart("cgi-bin/firmwarecfg")
79 .addParameter("sid", sid_)
80 .addParameter("PhonebookId", "0")
81 .addParameter("PhonebookImportFile", toByteArray(), "iso-8859-1")
88 HttpRequest httpGet(String path)
90 return new HttpGETRequest(url_ + "/" + path);
93 HttpRequest httpPost(String path)
95 return new HttpPOSTRequest(url_ + "/" + path);
98 HttpRequest httpPostMultipart(String path)
100 return new HttpPOSTRequest(url_ + "/" + path).setMultipart(true);