SsdpHttpClient.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Configuration;
  3. using System;
  4. using System.IO;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml.Linq;
  8. namespace MediaBrowser.Dlna.PlayTo
  9. {
  10. public class SsdpHttpClient
  11. {
  12. private const string USERAGENT = "Microsoft-Windows/6.2 UPnP/1.0 Microsoft-DLNA DLNADOC/1.50";
  13. private const string FriendlyName = "MediaBrowser";
  14. private readonly IHttpClient _httpClient;
  15. private readonly IServerConfigurationManager _config;
  16. public SsdpHttpClient(IHttpClient httpClient, IServerConfigurationManager config)
  17. {
  18. _httpClient = httpClient;
  19. _config = config;
  20. }
  21. public async Task<XDocument> SendCommandAsync(string baseUrl, DeviceService service, string command, string postData, string header = null)
  22. {
  23. var serviceUrl = service.ControlUrl;
  24. if (!serviceUrl.StartsWith("/"))
  25. serviceUrl = "/" + serviceUrl;
  26. var response = await PostSoapDataAsync(baseUrl + serviceUrl, "\"" + service.ServiceType + "#" + command + "\"", postData, header)
  27. .ConfigureAwait(false);
  28. using (var stream = response.Content)
  29. {
  30. using (var reader = new StreamReader(stream, Encoding.UTF8))
  31. {
  32. return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
  33. }
  34. }
  35. }
  36. public async Task SubscribeAsync(string url, string ip, int port, string localIp, int eventport, int timeOut = 3600)
  37. {
  38. var options = new HttpRequestOptions
  39. {
  40. Url = url,
  41. UserAgent = USERAGENT,
  42. LogRequest = _config.Configuration.DlnaOptions.EnableDebugLogging
  43. };
  44. options.RequestHeaders["HOST"] = ip + ":" + port;
  45. options.RequestHeaders["CALLBACK"] = "<" + localIp + ":" + eventport + ">";
  46. options.RequestHeaders["NT"] = "upnp:event";
  47. options.RequestHeaders["TIMEOUT"] = "Second - " + timeOut;
  48. using (await _httpClient.Get(options).ConfigureAwait(false))
  49. {
  50. }
  51. }
  52. public async Task RespondAsync(Uri url, string ip, int port, string localIp, int eventport, int timeOut = 20000)
  53. {
  54. var options = new HttpRequestOptions
  55. {
  56. Url = url.ToString(),
  57. UserAgent = USERAGENT
  58. };
  59. options.RequestHeaders["HOST"] = ip + ":" + port;
  60. options.RequestHeaders["CALLBACK"] = "<" + localIp + ":" + eventport + ">";
  61. options.RequestHeaders["NT"] = "upnp:event";
  62. options.RequestHeaders["TIMEOUT"] = "Second - 3600";
  63. using (await _httpClient.Get(options).ConfigureAwait(false))
  64. {
  65. }
  66. }
  67. public async Task<XDocument> GetDataAsync(string url)
  68. {
  69. var options = new HttpRequestOptions
  70. {
  71. Url = url,
  72. UserAgent = USERAGENT,
  73. LogRequest = _config.Configuration.DlnaOptions.EnableDebugLogging
  74. };
  75. options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
  76. using (var stream = await _httpClient.Get(options).ConfigureAwait(false))
  77. {
  78. using (var reader = new StreamReader(stream, Encoding.UTF8))
  79. {
  80. return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
  81. }
  82. }
  83. }
  84. private Task<HttpResponseInfo> PostSoapDataAsync(string url, string soapAction, string postData, string header = null)
  85. {
  86. if (!soapAction.StartsWith("\""))
  87. soapAction = "\"" + soapAction + "\"";
  88. var options = new HttpRequestOptions
  89. {
  90. Url = url,
  91. UserAgent = USERAGENT,
  92. LogRequest = _config.Configuration.DlnaOptions.EnableDebugLogging
  93. };
  94. options.RequestHeaders["SOAPAction"] = soapAction;
  95. options.RequestHeaders["Pragma"] = "no-cache";
  96. options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
  97. if (!string.IsNullOrWhiteSpace(header))
  98. {
  99. options.RequestHeaders["contentFeatures.dlna.org"] = header;
  100. }
  101. options.RequestContentType = "text/xml; charset=\"utf-8\"";
  102. options.RequestContent = postData;
  103. return _httpClient.Post(options);
  104. }
  105. }
  106. }