/// <exception cref="JsonLDNet.Core.JsonLdError"></exception> public virtual async Task <RemoteDocument> LoadDocumentAsync(string url) { RemoteDocument doc = new RemoteDocument(url, null); try { using (HttpResponseMessage response = await JsonLD.Util.LDHttpClient.FetchAsync(url).ConfigureAwait(false)) { var code = (int)response.StatusCode; if (code >= 400) { throw new JsonLdError(JsonLdError.Error.LoadingDocumentFailed, $"HTTP {code} {url}"); } var finalUrl = response.RequestMessage.RequestUri.ToString(); var contentType = GetJsonLDContentType(response.Content.Headers.ContentType.MediaType); if (contentType == JsonLDContentType.Other) { throw new JsonLdError(JsonLdError.Error.LoadingDocumentFailed, url); } // For plain JSON, see if there's a context document linked in the HTTP response headers. if (contentType == JsonLDContentType.PlainJson && response.Headers.TryGetValues("Link", out var linkHeaders)) { linkHeaders = linkHeaders.SelectMany((h) => h.Split(",".ToCharArray())) .Select(h => h.Trim()).ToArray(); IEnumerable <string> linkedContexts = linkHeaders.Where(v => v.EndsWith("rel=\"http://www.w3.org/ns/json-ld#context\"")); if (linkedContexts.Count() > 1) { throw new JsonLdError(JsonLdError.Error.MultipleContextLinkHeaders); } string header = linkedContexts.First(); string linkedUrl = header.Substring(1, header.IndexOf(">") - 1); string resolvedUrl = URL.Resolve(finalUrl, linkedUrl); var remoteContext = await this.LoadDocumentAsync(resolvedUrl).ConfigureAwait(false); doc.contextUrl = remoteContext.documentUrl; doc.context = remoteContext.document; } Stream stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); doc.DocumentUrl = finalUrl; doc.Document = JSONUtils.FromInputStream(stream); } } catch (JsonLdError) { throw; } catch (Exception exception) { throw new JsonLdError(JsonLdError.Error.LoadingDocumentFailed, url, exception); } return(doc); }
/// <exception cref="JsonLDNet.Core.JsonLdError"></exception> public virtual RemoteDocument LoadDocument(string url) { #if !PORTABLE RemoteDocument doc = new RemoteDocument(url, null); try { HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.Accept = AcceptHeader; WebResponse resp = req.GetResponse(); bool isJsonld = resp.Headers[HttpResponseHeader.ContentType] == "application/ld+json"; if (!resp.Headers[HttpResponseHeader.ContentType].Contains("json")) { throw new JsonLdError(JsonLdError.Error.LoadingDocumentFailed, url); } string[] linkHeaders = resp.Headers.GetValues("Link"); if (!isJsonld && linkHeaders != null) { linkHeaders = linkHeaders.SelectMany((h) => h.Split(",".ToCharArray())) .Select(h => h.Trim()).ToArray(); IEnumerable <string> linkedContexts = linkHeaders.Where(v => v.EndsWith("rel=\"http://www.w3.org/ns/json-ld#context\"")); if (linkedContexts.Count() > 1) { throw new JsonLdError(JsonLdError.Error.MultipleContextLinkHeaders); } string header = linkedContexts.First(); string linkedUrl = header.Substring(1, header.IndexOf(">") - 1); string resolvedUrl = URL.Resolve(url, linkedUrl); var remoteContext = this.LoadDocument(resolvedUrl); doc.contextUrl = remoteContext.documentUrl; doc.context = remoteContext.document; } Stream stream = resp.GetResponseStream(); doc.DocumentUrl = req.Address.ToString(); doc.Document = JSONUtils.FromInputStream(stream); } catch (JsonLdError) { throw; } catch (Exception) { throw new JsonLdError(JsonLdError.Error.LoadingDocumentFailed, url); } return(doc); #else throw new PlatformNotSupportedException(); #endif }
/// <exception cref="JsonLDNet.Core.JsonLdError"></exception> public virtual RemoteDocument LoadDocument(string url) { #if !PORTABLE && !IS_CORECLR RemoteDocument doc = new RemoteDocument(url, null); HttpWebResponse resp; try { HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.Accept = AcceptHeader; resp = (HttpWebResponse)req.GetResponse(); bool isJsonld = resp.Headers[HttpResponseHeader.ContentType] == "application/ld+json"; if (!resp.Headers[HttpResponseHeader.ContentType].Contains("json")) { throw new JsonLdError(JsonLdError.Error.LoadingDocumentFailed, url); } string[] linkHeaders = resp.Headers.GetValues("Link"); if (!isJsonld && linkHeaders != null) { linkHeaders = linkHeaders.SelectMany((h) => h.Split(",".ToCharArray())) .Select(h => h.Trim()).ToArray(); IEnumerable <string> linkedContexts = linkHeaders.Where(v => v.EndsWith("rel=\"http://www.w3.org/ns/json-ld#context\"")); if (linkedContexts.Count() > 1) { throw new JsonLdError(JsonLdError.Error.MultipleContextLinkHeaders); } string header = linkedContexts.First(); string linkedUrl = header.Substring(1, header.IndexOf(">") - 1); string resolvedUrl = URL.Resolve(url, linkedUrl); var remoteContext = this.LoadDocument(resolvedUrl); doc.contextUrl = remoteContext.documentUrl; doc.context = remoteContext.document; } Stream stream = resp.GetResponseStream(); doc.DocumentUrl = req.Address.ToString(); doc.Document = JSONUtils.FromInputStream(stream); } catch (JsonLdError) { throw; } catch (WebException webException) { try { resp = (HttpWebResponse)webException.Response; int baseStatusCode = (int)(Math.Floor((double)resp.StatusCode / 100)) * 100; if (baseStatusCode == 300) { string location = resp.Headers[HttpResponseHeader.Location]; if (!string.IsNullOrWhiteSpace(location)) { // TODO: Add recursion break or simply switch to HttpClient so we don't have to recurse on HTTP redirects. return(LoadDocument(location)); } } } catch (Exception innerException) { throw new JsonLdError(JsonLdError.Error.LoadingDocumentFailed, url, innerException); } throw new JsonLdError(JsonLdError.Error.LoadingDocumentFailed, url, webException); } catch (Exception exception) { throw new JsonLdError(JsonLdError.Error.LoadingDocumentFailed, url, exception); } return(doc); #else throw new PlatformNotSupportedException(); #endif }