Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 7f1222c

Browse files
committed
Add GetRedirectUrlIfAny()
1 parent 8c70000 commit 7f1222c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/ServiceStack.Text/HttpUtils.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace ServiceStack
1212
{
1313
public static class HttpUtils
1414
{
15+
public static string UserAgent = "ServiceStack.Text";
16+
1517
[ThreadStatic]
1618
public static IHttpResultsFilter ResultsFilter;
1719

@@ -754,6 +756,20 @@ public static string PutXmlToUrl(this string url, object data,
754756
requestFilter: requestFilter, responseFilter: responseFilter);
755757
}
756758
#endif
759+
760+
public static string GetRedirectUrlIfAny(this string url)
761+
{
762+
var finalUrl = url;
763+
try
764+
{
765+
var ignore = url.GetBytesFromUrl(
766+
requestFilter: req => { req.AllowAutoRedirect = false; req.UserAgent = UserAgent; },
767+
responseFilter: res => finalUrl = res.Headers[HttpHeaders.Location] ?? finalUrl);
768+
}
769+
catch { }
770+
771+
return finalUrl;
772+
}
757773
}
758774

759775
public interface IHttpResultsFilter : IDisposable

0 commit comments

Comments
 (0)