string PerformAction(UPnPDevice device, string action, string soap) { try { string soapBody = @"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/ "" s:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/ ""> <s:Body> <?=soap?> </s:Body> </s:Envelope>"; soapBody = soapBody.Replace("<?=soap?>", soap); UpdateLog(UpnpLogType.Out, soapBody); byte[] body = UTF8Encoding.ASCII.GetBytes(soapBody); WebRequest wr = WebRequest.Create(device.URL); wr.Timeout = 5000; wr.Method = "POST"; wr.Headers.Add("SOAPAction", "\"urn:schemas-upnp-org:service:" + device.Name + ":1#" + action + "\""); wr.ContentType = "text/xml;charset=\"utf-8\""; wr.ContentLength = body.Length; Stream stream = wr.GetRequestStream(); stream.Write(body, 0, body.Length); stream.Flush(); stream.Close(); WebResponse wres = wr.GetResponse(); StreamReader sr = new StreamReader(wres.GetResponseStream()); string xml = sr.ReadToEnd(); sr.Close(); UpdateLog(UpnpLogType.In, xml); return(xml); } catch (Exception ex) { UpdateLog(UpnpLogType.Error, ex.Message); } return(null); }
public PortEntry GetPortEntry(UPnPDevice device, int index) { UpdateLog(UpnpLogType.Other, "Getting port map index " + index + " on " + device.Name); try { string body = @"<u:GetGenericPortMappingEntry xmlns:u=""urn:schemas-upnp-org:service:<?=DeviceName?>:1""> <NewPortMappingIndex><?=NewPortMappingIndex?></NewPortMappingIndex> </u:GetGenericPortMappingEntry>"; body = body.Replace("<?=DeviceName?>", device.Name); body = body.Replace("<?=NewPortMappingIndex?>", index.ToString()); string ret = PerformAction(device, "GetGenericPortMappingEntry", body); if (ret == null || ret == "") { return(null); } string name = ExtractTag("NewPortMappingDescription", ret); string ip = ExtractTag("NewInternalClient", ret); string port = ExtractTag("NewInternalPort", ret); string protocol = ExtractTag("NewProtocol", ret); PortEntry entry = new PortEntry(); entry.Device = device; entry.Description = index + ": " + name + " - " + ip + ":" + port + " " + protocol; entry.Port = int.Parse(port); entry.Protocol = protocol; return(entry); } catch (Exception ex) { UpdateLog(UpnpLogType.Error, ex.Message); } return(null); }