0

i try to access my Fritzbox with soap and java, my first goal is to tell the fritzbox it should reconnect. The java programm dosen´t come up with an exception but the router dosen´t reconnect. I checked the tr64desc.xml if the soap stuff is wrong... but i can´t see the forest for the trees. Is the soap message the issue or the code itself?

This is my code:

public void reconnect() {
    StringBuilder soapXML = new StringBuilder();

    soapXML.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    soapXML.append("<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">");
    soapXML.append("    <s:Body>");
    soapXML.append("        <u:ForceTermination xmlns:u=\"urn:dslforum-org:service:WANIPConnection:1\" />");
    soapXML.append("    </s:Body>");
    soapXML.append("</s:Envelope>");
    
    Socket socket = null;
    BufferedWriter bw = null;

    try {
        socket = new Socket("192.168.178.1", 49000);
        bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
        
        bw.write("POST /upnp/control/wanipconnection1 HTTP/1.1");
        bw.write("HOST: fritz.box:49000" + "\r\n");
        bw.write("CONTENT-LENGTH: " + soapXML.toString().length() + "\r\n");
        bw.write("CONTENT-TYPE: text/xml; charset=\"utf-8\"" + "\r\n");
        bw.write("SOAPACTION: \"urn:dslforum-org:service:WANIPConnection:1#ForceTermination\"" + "\r\n");
        bw.write("USER-AGENT: AVM UPnP/1.0 Client 1.0"+"\r\n");
        bw.write("\r\n");

        bw.write(soapXML.toString());
        bw.flush();
        
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(bw!=null)
            try {
                bw.close();
            } catch (IOException e) {}
        
        if(socket!=null)
            try {
                socket.close();
            } catch (IOException e) {}
    }
}
  • BTW you may avoid the finally block with a try-with-resources https://www.baeldung.com/java-try-with-resources . – PeterMmm Oct 06 '22 at 19:46
  • That's a new connection. I've got to say I'm surprised there's seemingly no authorization/authentication going on. Since there's no reading going on, how do you know Fritz isn't objecting to that? It would be a security no-no to accept an anonymous request like that. – g00se Oct 06 '22 at 21:58
  • @PeterMmm thanks for the advice, i didn't hat that in mind – TryToProgram Oct 07 '22 at 04:23
  • @g00se i came across an example where it should work without authentication, but i checked the documentation and it requires user rights. I had my issues with authentication, so i tried to avoid it. But then i try it again – TryToProgram Oct 07 '22 at 04:33
  • I would assume auth is the same as for admin via the gui – g00se Oct 07 '22 at 08:25
  • I checked the response. It said 400 Bad Request (ERR_INVALID_HOSTHEADER). The documentation says: If a user is not authenticated, 401 (“Unauthorized”). I guess the authorization/authentication is not the issue? If it is not the issue, what is wrong with the requenst? – TryToProgram Oct 07 '22 at 17:08
  • *I checked the response. It said 400 Bad Request (ERR_INVALID_HOSTHEADER).* Actually, now I look at the HOST header, that figures, since 'fritz.box' is normally the domain *suffix*. Yes, not .box, but **.fritz.box** – g00se Oct 08 '22 at 00:06

0 Answers0