SsdpHttpClient.cs 4.7 KB

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