@@ -16,11 +16,13 @@ public virtual RemoteDocument LoadDocument(string url)
16
16
{
17
17
#if ! PORTABLE && ! IS_CORECLR
18
18
RemoteDocument doc = new RemoteDocument ( url , null ) ;
19
+ HttpWebResponse resp ;
20
+
19
21
try
20
22
{
21
23
HttpWebRequest req = ( HttpWebRequest ) HttpWebRequest . Create ( url ) ;
22
24
req . Accept = AcceptHeader ;
23
- WebResponse resp = req . GetResponse ( ) ;
25
+ resp = ( HttpWebResponse ) req . GetResponse ( ) ;
24
26
bool isJsonld = resp . Headers [ HttpResponseHeader . ContentType ] == "application/ld+json" ;
25
27
if ( ! resp . Headers [ HttpResponseHeader . ContentType ] . Contains ( "json" ) )
26
28
{
@@ -31,7 +33,7 @@ public virtual RemoteDocument LoadDocument(string url)
31
33
if ( ! isJsonld && linkHeaders != null )
32
34
{
33
35
linkHeaders = linkHeaders . SelectMany ( ( h ) => h . Split ( "," . ToCharArray ( ) ) )
34
- . Select ( h => h . Trim ( ) ) . ToArray ( ) ;
36
+ . Select ( h => h . Trim ( ) ) . ToArray ( ) ;
35
37
IEnumerable < string > linkedContexts = linkHeaders . Where ( v => v . EndsWith ( "rel=\" http://www.w3.org/ns/json-ld#context\" " ) ) ;
36
38
if ( linkedContexts . Count ( ) > 1 )
37
39
{
@@ -54,9 +56,32 @@ public virtual RemoteDocument LoadDocument(string url)
54
56
{
55
57
throw ;
56
58
}
57
- catch ( Exception )
59
+ catch ( WebException webException )
60
+ {
61
+ try
62
+ {
63
+ resp = ( HttpWebResponse ) webException . Response ;
64
+ int baseStatusCode = ( int ) ( Math . Floor ( ( double ) resp . StatusCode / 100 ) ) * 100 ;
65
+ if ( baseStatusCode == 300 )
66
+ {
67
+ string location = resp . Headers [ HttpResponseHeader . Location ] ;
68
+ if ( ! string . IsNullOrWhiteSpace ( location ) )
69
+ {
70
+ // TODO: Add recursion break or simply switch to HttpClient so we don't have to recurse on HTTP redirects.
71
+ return LoadDocument ( location ) ;
72
+ }
73
+ }
74
+ }
75
+ catch ( Exception innerException )
76
+ {
77
+ throw new JsonLdError ( JsonLdError . Error . LoadingDocumentFailed , url , innerException ) ;
78
+ }
79
+
80
+ throw new JsonLdError ( JsonLdError . Error . LoadingDocumentFailed , url , webException ) ;
81
+ }
82
+ catch ( Exception exception )
58
83
{
59
- throw new JsonLdError ( JsonLdError . Error . LoadingDocumentFailed , url ) ;
84
+ throw new JsonLdError ( JsonLdError . Error . LoadingDocumentFailed , url , exception ) ;
60
85
}
61
86
return doc ;
62
87
#else
0 commit comments