SsdpHttpClient.cs 4.8 KB

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