79683564

Date: 2025-06-29 07:33:07
Score: 1
Natty:
Report link

ok, by the comments, i have a new version of the code. But it still doesnt working


 private async void SendPhonebookToFBoxAction(object obj)
 {
     

     string fritzBoxUrl = "http://fritz.box"; // Default FritzBox URL
     string username1 = settingsModel.FBoxUsername; // Default FritzBox username
     string password1 = AESHelper.DecryptWithDPAPI(Convert.FromBase64String(settingsModel.FBoxPassword.EncryptedPassword)); // Decrypt the password using DPAPI


     var url = "http://fritz.box:49000/upnp/control/x_contact";
     var soapAction = "urn:dslforum-org:service:X_AVM-DE_OnTel:1#SetPhonebookEntry";


     //var handler = new HttpClientHandler();
     //var client1 = new HttpClient(handler);
     var byteArray = Encoding.ASCII.GetBytes($"{username1}:{password1}"); // Create a base64 encoded string for Basic Authentication
     //client1.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); // Set the Authorization header for Basic Authentication

     var handler = new HttpClientHandler
     {
         Credentials = new NetworkCredential(username1, password1)
     };

     using (var client = new HttpClient(handler))
     {

         var numbers = new List<(string, string, int)>
         {
             ("0123456789", "home", 1),
             ("01701234567", "mobile", 2)
         };

         string xml = CreatePhonebookEntryXml("Max Mustermann", numbers);

         //Debug.WriteLine(xml);

         // Implementation for sending phonebook to FritzBox
         /*string newPhonebookEntryData = "<phonebook>" +
                                        "<contact>" +
                                        "<category>0</category" +
                                        "<person>" +
                                        "<realName>Max Mustermann</realName>" +
                                        "</person>" +
                                        "<telephony nid=\"1\">" +
                                        "<number type=\"home\" prio=\"1\" id=\"0\">0123456789</number>" +
                                        "</telephony>" +
                                        "<services/>" +
                                        "<setup/>" +
                                        "<features doorphone=\"0\"/>" +
                                        "</contact>" +
                                        "</phonebook>";*/

         string url1 = "http://fritz.box:49000/upnp/control/x_contact";
         string service = "urn:dslforum-org:service:X_AVM-DE_OnTel:1";
         string action = "SetPhonebookEntry";
         string soapBody = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                           "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"" +
                           " s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
                           "<s:Body>" +
                           "<u:SetPhonebookEntry xmlns:u=\"urn:dslforum-org:service:X_AVM-DE_OnTel:1\">" +
                           "<NewPhonebookID>1</NewPhonebookID>" +
                           //$"<NewPhonebookEntryID>{string.Empty}</NewPhonebookEntryID>" +
                           $"<NewPhonebookEntryData><![CDATA[{xml}]]></NewPhonebookEntryData>" +
                           "</u:SetPhonebookEntry>" +
                           "</s:Body>" +
                           "</s:Envelope>";

         //Debug.WriteLine($"SOAP Body: {soapBody}"); // Log the SOAP body for debugging

         var content = new StringContent(soapBody, Encoding.UTF8, "text/xml");
         content.Headers.Add("SOAPAction", $"\"{soapAction}\""); // Set the SOAP action header

         var response = await client.PostAsync(url, content);
         var result = await response.Content.ReadAsStringAsync();

         Debug.WriteLine(result);

         
     }
 }
 #endregion


 private string CreatePhonebookEntryXml(
     string name, 
     List<(string number, string type, int prio)> numbers,
     string? email = null,
     int? ringtone = null)
 {
     var telephony = new XElement("telephony");
     int id = 0;
     foreach (var (number, type, prio) in numbers)
     {
         telephony.Add(new XElement("number",
         new XAttribute("type", type),
         new XAttribute("prio", prio),
         new XAttribute("id", id++),
         number
         ));
     }

     var contact = new XElement("contact",
     new XElement("category", "0"),
     new XElement("person",
     new XElement("realName", name)
     ),
     telephony,
     new XElement("services",
     email != null
     ? new XElement("email",
     new XAttribute("classifier", "private"),
     new XAttribute("id", "0"),
     email)
     : null
     ),
     new XElement("setup",
     ringtone.HasValue
     ? new XElement("ringtone", ringtone.Value)
     : null
     ),
     new XElement("mod_time", DateTimeOffset.UtcNow.ToUnixTimeSeconds())
     );

     var phonebook = new XElement("phonebook", contact);
     return phonebook.ToString(SaveOptions.DisableFormatting);
 }
Reasons:
  • Blacklisted phrase (1): doesnt work
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Marko Petsch