HttpClient GetAsync with a hash in URL
.NET Core 2.2 console application on Windows.
I'm exploring how to use HttpClient GetAsync on a Stackoverflow share style URL eg: https://stackoverflow.com/a/29809054/26086 which returns a 302 redirect URL with a hash in it
static async Task Main()
{
var client = new HttpClient();
// 1. Doesn't work - has a hash in URL
var url = "https://stackoverflow.com/questions/29808915/why-use-async-await-all-the-way-down/29809054#29809054";
HttpResponseMessage rm = await client.GetAsync(url);
Console.WriteLine($"Status code: {(int)rm.StatusCode}"); // 400 Bad Request
// 2. Does work - no hash
url = "https://stackoverflow.com/questions/29808915/why-use-async-await-all-the-way-down/29809054";
rm = await client.GetAsync(url);
Console.WriteLine($"Status code: {(int)rm.StatusCode}"); // 200 Okay
// 3. Doesn't work as the 302 redirect goes to the first URL above with a hash
url = "https://stackoverflow.com/a/29809054/26086";
rm = await client.GetAsync(url);
Console.WriteLine($"Status code: {(int)rm.StatusCode}"); // 400 Bad Request
}
I'm crawling my blog which has many SO short codes in it.
Update/Workaround
With thanks to @rohancragg I found that turning off AutoRedirect then getting the URI from the returned header worked
// as some autoredirects fail due to #fragments in url, handle redirects manually
var handler = new HttpClientHandler { AllowAutoRedirect = false };
var client = new HttpClient(handler);
var url = "https://stackoverflow.com/a/29809054/26086";
HttpResponseMessage rm = await client.GetAsync(url);
// gives the desired new URL which can then GetAsync
Uri u = rm.Headers.Location;
c# http url .net-core
add a comment |
.NET Core 2.2 console application on Windows.
I'm exploring how to use HttpClient GetAsync on a Stackoverflow share style URL eg: https://stackoverflow.com/a/29809054/26086 which returns a 302 redirect URL with a hash in it
static async Task Main()
{
var client = new HttpClient();
// 1. Doesn't work - has a hash in URL
var url = "https://stackoverflow.com/questions/29808915/why-use-async-await-all-the-way-down/29809054#29809054";
HttpResponseMessage rm = await client.GetAsync(url);
Console.WriteLine($"Status code: {(int)rm.StatusCode}"); // 400 Bad Request
// 2. Does work - no hash
url = "https://stackoverflow.com/questions/29808915/why-use-async-await-all-the-way-down/29809054";
rm = await client.GetAsync(url);
Console.WriteLine($"Status code: {(int)rm.StatusCode}"); // 200 Okay
// 3. Doesn't work as the 302 redirect goes to the first URL above with a hash
url = "https://stackoverflow.com/a/29809054/26086";
rm = await client.GetAsync(url);
Console.WriteLine($"Status code: {(int)rm.StatusCode}"); // 400 Bad Request
}
I'm crawling my blog which has many SO short codes in it.
Update/Workaround
With thanks to @rohancragg I found that turning off AutoRedirect then getting the URI from the returned header worked
// as some autoredirects fail due to #fragments in url, handle redirects manually
var handler = new HttpClientHandler { AllowAutoRedirect = false };
var client = new HttpClient(handler);
var url = "https://stackoverflow.com/a/29809054/26086";
HttpResponseMessage rm = await client.GetAsync(url);
// gives the desired new URL which can then GetAsync
Uri u = rm.Headers.Location;
c# http url .net-core
3
URLs sent to servers don't contain the#
fragment. It's only of use in a client such as e.g. a browser.
– Damien_The_Unbeliever
Jan 3 at 15:51
That makes sense thank you. I'm digging into why it is giving a 400 now as I would like for HttpClient to ignore the hash. I've updated the question to highlight the reason I need to do this ie a StackOverflow share url.
– Dave Mateer
Jan 3 at 16:28
As @Damien_The_Unbeliever implies, you'll just need to strip off the hash and everything after it - all that does is tell the browser to jump to that anchor tag in the HTML page (see: w3schools.com/jsref/prop_anchor_hash.asp). So this would mean that your option 2 is your only option in this case...
– rohancragg
Jan 4 at 9:54
1
You could also use the Uri class to parse the Uri and ignore any 'fragments': docs.microsoft.com/en-us/dotnet/api/system.uri.fragment
– rohancragg
Jan 4 at 10:00
Thanks @rohancragg - but what if I'm requesting stackoverflow.com/a/29809054/26086 which then returns the 302, and automatically requests the URL with a hash in it. Maybe I'll have to stop the autoredirect stackoverflow.com/a/10647245/26086 and then strip off the hash, then do another request.
– Dave Mateer
Jan 4 at 10:00
add a comment |
.NET Core 2.2 console application on Windows.
I'm exploring how to use HttpClient GetAsync on a Stackoverflow share style URL eg: https://stackoverflow.com/a/29809054/26086 which returns a 302 redirect URL with a hash in it
static async Task Main()
{
var client = new HttpClient();
// 1. Doesn't work - has a hash in URL
var url = "https://stackoverflow.com/questions/29808915/why-use-async-await-all-the-way-down/29809054#29809054";
HttpResponseMessage rm = await client.GetAsync(url);
Console.WriteLine($"Status code: {(int)rm.StatusCode}"); // 400 Bad Request
// 2. Does work - no hash
url = "https://stackoverflow.com/questions/29808915/why-use-async-await-all-the-way-down/29809054";
rm = await client.GetAsync(url);
Console.WriteLine($"Status code: {(int)rm.StatusCode}"); // 200 Okay
// 3. Doesn't work as the 302 redirect goes to the first URL above with a hash
url = "https://stackoverflow.com/a/29809054/26086";
rm = await client.GetAsync(url);
Console.WriteLine($"Status code: {(int)rm.StatusCode}"); // 400 Bad Request
}
I'm crawling my blog which has many SO short codes in it.
Update/Workaround
With thanks to @rohancragg I found that turning off AutoRedirect then getting the URI from the returned header worked
// as some autoredirects fail due to #fragments in url, handle redirects manually
var handler = new HttpClientHandler { AllowAutoRedirect = false };
var client = new HttpClient(handler);
var url = "https://stackoverflow.com/a/29809054/26086";
HttpResponseMessage rm = await client.GetAsync(url);
// gives the desired new URL which can then GetAsync
Uri u = rm.Headers.Location;
c# http url .net-core
.NET Core 2.2 console application on Windows.
I'm exploring how to use HttpClient GetAsync on a Stackoverflow share style URL eg: https://stackoverflow.com/a/29809054/26086 which returns a 302 redirect URL with a hash in it
static async Task Main()
{
var client = new HttpClient();
// 1. Doesn't work - has a hash in URL
var url = "https://stackoverflow.com/questions/29808915/why-use-async-await-all-the-way-down/29809054#29809054";
HttpResponseMessage rm = await client.GetAsync(url);
Console.WriteLine($"Status code: {(int)rm.StatusCode}"); // 400 Bad Request
// 2. Does work - no hash
url = "https://stackoverflow.com/questions/29808915/why-use-async-await-all-the-way-down/29809054";
rm = await client.GetAsync(url);
Console.WriteLine($"Status code: {(int)rm.StatusCode}"); // 200 Okay
// 3. Doesn't work as the 302 redirect goes to the first URL above with a hash
url = "https://stackoverflow.com/a/29809054/26086";
rm = await client.GetAsync(url);
Console.WriteLine($"Status code: {(int)rm.StatusCode}"); // 400 Bad Request
}
I'm crawling my blog which has many SO short codes in it.
Update/Workaround
With thanks to @rohancragg I found that turning off AutoRedirect then getting the URI from the returned header worked
// as some autoredirects fail due to #fragments in url, handle redirects manually
var handler = new HttpClientHandler { AllowAutoRedirect = false };
var client = new HttpClient(handler);
var url = "https://stackoverflow.com/a/29809054/26086";
HttpResponseMessage rm = await client.GetAsync(url);
// gives the desired new URL which can then GetAsync
Uri u = rm.Headers.Location;
c# http url .net-core
c# http url .net-core
edited Jan 5 at 7:42
Erik Philips
41.6k694126
41.6k694126
asked Jan 3 at 15:47
Dave MateerDave Mateer
3,404135890
3,404135890
3
URLs sent to servers don't contain the#
fragment. It's only of use in a client such as e.g. a browser.
– Damien_The_Unbeliever
Jan 3 at 15:51
That makes sense thank you. I'm digging into why it is giving a 400 now as I would like for HttpClient to ignore the hash. I've updated the question to highlight the reason I need to do this ie a StackOverflow share url.
– Dave Mateer
Jan 3 at 16:28
As @Damien_The_Unbeliever implies, you'll just need to strip off the hash and everything after it - all that does is tell the browser to jump to that anchor tag in the HTML page (see: w3schools.com/jsref/prop_anchor_hash.asp). So this would mean that your option 2 is your only option in this case...
– rohancragg
Jan 4 at 9:54
1
You could also use the Uri class to parse the Uri and ignore any 'fragments': docs.microsoft.com/en-us/dotnet/api/system.uri.fragment
– rohancragg
Jan 4 at 10:00
Thanks @rohancragg - but what if I'm requesting stackoverflow.com/a/29809054/26086 which then returns the 302, and automatically requests the URL with a hash in it. Maybe I'll have to stop the autoredirect stackoverflow.com/a/10647245/26086 and then strip off the hash, then do another request.
– Dave Mateer
Jan 4 at 10:00
add a comment |
3
URLs sent to servers don't contain the#
fragment. It's only of use in a client such as e.g. a browser.
– Damien_The_Unbeliever
Jan 3 at 15:51
That makes sense thank you. I'm digging into why it is giving a 400 now as I would like for HttpClient to ignore the hash. I've updated the question to highlight the reason I need to do this ie a StackOverflow share url.
– Dave Mateer
Jan 3 at 16:28
As @Damien_The_Unbeliever implies, you'll just need to strip off the hash and everything after it - all that does is tell the browser to jump to that anchor tag in the HTML page (see: w3schools.com/jsref/prop_anchor_hash.asp). So this would mean that your option 2 is your only option in this case...
– rohancragg
Jan 4 at 9:54
1
You could also use the Uri class to parse the Uri and ignore any 'fragments': docs.microsoft.com/en-us/dotnet/api/system.uri.fragment
– rohancragg
Jan 4 at 10:00
Thanks @rohancragg - but what if I'm requesting stackoverflow.com/a/29809054/26086 which then returns the 302, and automatically requests the URL with a hash in it. Maybe I'll have to stop the autoredirect stackoverflow.com/a/10647245/26086 and then strip off the hash, then do another request.
– Dave Mateer
Jan 4 at 10:00
3
3
URLs sent to servers don't contain the
#
fragment. It's only of use in a client such as e.g. a browser.– Damien_The_Unbeliever
Jan 3 at 15:51
URLs sent to servers don't contain the
#
fragment. It's only of use in a client such as e.g. a browser.– Damien_The_Unbeliever
Jan 3 at 15:51
That makes sense thank you. I'm digging into why it is giving a 400 now as I would like for HttpClient to ignore the hash. I've updated the question to highlight the reason I need to do this ie a StackOverflow share url.
– Dave Mateer
Jan 3 at 16:28
That makes sense thank you. I'm digging into why it is giving a 400 now as I would like for HttpClient to ignore the hash. I've updated the question to highlight the reason I need to do this ie a StackOverflow share url.
– Dave Mateer
Jan 3 at 16:28
As @Damien_The_Unbeliever implies, you'll just need to strip off the hash and everything after it - all that does is tell the browser to jump to that anchor tag in the HTML page (see: w3schools.com/jsref/prop_anchor_hash.asp). So this would mean that your option 2 is your only option in this case...
– rohancragg
Jan 4 at 9:54
As @Damien_The_Unbeliever implies, you'll just need to strip off the hash and everything after it - all that does is tell the browser to jump to that anchor tag in the HTML page (see: w3schools.com/jsref/prop_anchor_hash.asp). So this would mean that your option 2 is your only option in this case...
– rohancragg
Jan 4 at 9:54
1
1
You could also use the Uri class to parse the Uri and ignore any 'fragments': docs.microsoft.com/en-us/dotnet/api/system.uri.fragment
– rohancragg
Jan 4 at 10:00
You could also use the Uri class to parse the Uri and ignore any 'fragments': docs.microsoft.com/en-us/dotnet/api/system.uri.fragment
– rohancragg
Jan 4 at 10:00
Thanks @rohancragg - but what if I'm requesting stackoverflow.com/a/29809054/26086 which then returns the 302, and automatically requests the URL with a hash in it. Maybe I'll have to stop the autoredirect stackoverflow.com/a/10647245/26086 and then strip off the hash, then do another request.
– Dave Mateer
Jan 4 at 10:00
Thanks @rohancragg - but what if I'm requesting stackoverflow.com/a/29809054/26086 which then returns the 302, and automatically requests the URL with a hash in it. Maybe I'll have to stop the autoredirect stackoverflow.com/a/10647245/26086 and then strip off the hash, then do another request.
– Dave Mateer
Jan 4 at 10:00
add a comment |
2 Answers
2
active
oldest
votes
As @Damien_The_Unbeliever implies in a comment, you'll just need to strip off the hash and everything after it - all that does is tell the browser to jump to that anchor tag in the HTML page (see: https://w3schools.com/jsref/prop_anchor_hash.asp).
You could also use the Uri class to parse the Uri and ignore any 'fragments': https://docs.microsoft.com/en-us/dotnet/api/system.uri.fragment
Because the share-style Urls are only ever going to return a 302 then I'd suggest capturing the Uri to which the 302 is referring and do as I suggest above and just get the path and ignore the fragment.
So you need to use some mechanism (which I'm just looking up!) to handle a 302 gracefully followed by option 2
Update: this looks relevant! How can I get System.Net.Http.HttpClient to not follow 302 redirects?
Update 2 Steve Guidi has a very important bit of advice in a comment here: https://stackoverflow.com/a/17758758/5351
In response to the advice that you need to use HttpResponseMessage.RequestMessage.RequestUri
:
it is very important to add
HttpCompletionOption.ResponseHeadersRead
as the second parameter of theGetAsync()
call
Disclaimer - I've not tried the above, this is just based on reading ;-)
add a comment |
Maybe you need to encode your URL before send the request using HttpUtility class, this way any special character will be escaped.
using System.Web;
var url = $"htpps://myurl.com/{HttpUtility.UrlEncode("#1234567")}";
Thank you Vinick - I think I need another tactic to bend HttpClient into what I want it to do ie respond correctly when I give a url such as: stackoverflow.com/a/29809054/26086
– Dave Mateer
Jan 3 at 16:38
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025575%2fhttpclient-getasync-with-a-hash-in-url%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As @Damien_The_Unbeliever implies in a comment, you'll just need to strip off the hash and everything after it - all that does is tell the browser to jump to that anchor tag in the HTML page (see: https://w3schools.com/jsref/prop_anchor_hash.asp).
You could also use the Uri class to parse the Uri and ignore any 'fragments': https://docs.microsoft.com/en-us/dotnet/api/system.uri.fragment
Because the share-style Urls are only ever going to return a 302 then I'd suggest capturing the Uri to which the 302 is referring and do as I suggest above and just get the path and ignore the fragment.
So you need to use some mechanism (which I'm just looking up!) to handle a 302 gracefully followed by option 2
Update: this looks relevant! How can I get System.Net.Http.HttpClient to not follow 302 redirects?
Update 2 Steve Guidi has a very important bit of advice in a comment here: https://stackoverflow.com/a/17758758/5351
In response to the advice that you need to use HttpResponseMessage.RequestMessage.RequestUri
:
it is very important to add
HttpCompletionOption.ResponseHeadersRead
as the second parameter of theGetAsync()
call
Disclaimer - I've not tried the above, this is just based on reading ;-)
add a comment |
As @Damien_The_Unbeliever implies in a comment, you'll just need to strip off the hash and everything after it - all that does is tell the browser to jump to that anchor tag in the HTML page (see: https://w3schools.com/jsref/prop_anchor_hash.asp).
You could also use the Uri class to parse the Uri and ignore any 'fragments': https://docs.microsoft.com/en-us/dotnet/api/system.uri.fragment
Because the share-style Urls are only ever going to return a 302 then I'd suggest capturing the Uri to which the 302 is referring and do as I suggest above and just get the path and ignore the fragment.
So you need to use some mechanism (which I'm just looking up!) to handle a 302 gracefully followed by option 2
Update: this looks relevant! How can I get System.Net.Http.HttpClient to not follow 302 redirects?
Update 2 Steve Guidi has a very important bit of advice in a comment here: https://stackoverflow.com/a/17758758/5351
In response to the advice that you need to use HttpResponseMessage.RequestMessage.RequestUri
:
it is very important to add
HttpCompletionOption.ResponseHeadersRead
as the second parameter of theGetAsync()
call
Disclaimer - I've not tried the above, this is just based on reading ;-)
add a comment |
As @Damien_The_Unbeliever implies in a comment, you'll just need to strip off the hash and everything after it - all that does is tell the browser to jump to that anchor tag in the HTML page (see: https://w3schools.com/jsref/prop_anchor_hash.asp).
You could also use the Uri class to parse the Uri and ignore any 'fragments': https://docs.microsoft.com/en-us/dotnet/api/system.uri.fragment
Because the share-style Urls are only ever going to return a 302 then I'd suggest capturing the Uri to which the 302 is referring and do as I suggest above and just get the path and ignore the fragment.
So you need to use some mechanism (which I'm just looking up!) to handle a 302 gracefully followed by option 2
Update: this looks relevant! How can I get System.Net.Http.HttpClient to not follow 302 redirects?
Update 2 Steve Guidi has a very important bit of advice in a comment here: https://stackoverflow.com/a/17758758/5351
In response to the advice that you need to use HttpResponseMessage.RequestMessage.RequestUri
:
it is very important to add
HttpCompletionOption.ResponseHeadersRead
as the second parameter of theGetAsync()
call
Disclaimer - I've not tried the above, this is just based on reading ;-)
As @Damien_The_Unbeliever implies in a comment, you'll just need to strip off the hash and everything after it - all that does is tell the browser to jump to that anchor tag in the HTML page (see: https://w3schools.com/jsref/prop_anchor_hash.asp).
You could also use the Uri class to parse the Uri and ignore any 'fragments': https://docs.microsoft.com/en-us/dotnet/api/system.uri.fragment
Because the share-style Urls are only ever going to return a 302 then I'd suggest capturing the Uri to which the 302 is referring and do as I suggest above and just get the path and ignore the fragment.
So you need to use some mechanism (which I'm just looking up!) to handle a 302 gracefully followed by option 2
Update: this looks relevant! How can I get System.Net.Http.HttpClient to not follow 302 redirects?
Update 2 Steve Guidi has a very important bit of advice in a comment here: https://stackoverflow.com/a/17758758/5351
In response to the advice that you need to use HttpResponseMessage.RequestMessage.RequestUri
:
it is very important to add
HttpCompletionOption.ResponseHeadersRead
as the second parameter of theGetAsync()
call
Disclaimer - I've not tried the above, this is just based on reading ;-)
edited Jan 4 at 10:21
answered Jan 4 at 10:03
rohancraggrohancragg
4,09242943
4,09242943
add a comment |
add a comment |
Maybe you need to encode your URL before send the request using HttpUtility class, this way any special character will be escaped.
using System.Web;
var url = $"htpps://myurl.com/{HttpUtility.UrlEncode("#1234567")}";
Thank you Vinick - I think I need another tactic to bend HttpClient into what I want it to do ie respond correctly when I give a url such as: stackoverflow.com/a/29809054/26086
– Dave Mateer
Jan 3 at 16:38
add a comment |
Maybe you need to encode your URL before send the request using HttpUtility class, this way any special character will be escaped.
using System.Web;
var url = $"htpps://myurl.com/{HttpUtility.UrlEncode("#1234567")}";
Thank you Vinick - I think I need another tactic to bend HttpClient into what I want it to do ie respond correctly when I give a url such as: stackoverflow.com/a/29809054/26086
– Dave Mateer
Jan 3 at 16:38
add a comment |
Maybe you need to encode your URL before send the request using HttpUtility class, this way any special character will be escaped.
using System.Web;
var url = $"htpps://myurl.com/{HttpUtility.UrlEncode("#1234567")}";
Maybe you need to encode your URL before send the request using HttpUtility class, this way any special character will be escaped.
using System.Web;
var url = $"htpps://myurl.com/{HttpUtility.UrlEncode("#1234567")}";
answered Jan 3 at 16:16
Vinick GoldenbergVinick Goldenberg
93
93
Thank you Vinick - I think I need another tactic to bend HttpClient into what I want it to do ie respond correctly when I give a url such as: stackoverflow.com/a/29809054/26086
– Dave Mateer
Jan 3 at 16:38
add a comment |
Thank you Vinick - I think I need another tactic to bend HttpClient into what I want it to do ie respond correctly when I give a url such as: stackoverflow.com/a/29809054/26086
– Dave Mateer
Jan 3 at 16:38
Thank you Vinick - I think I need another tactic to bend HttpClient into what I want it to do ie respond correctly when I give a url such as: stackoverflow.com/a/29809054/26086
– Dave Mateer
Jan 3 at 16:38
Thank you Vinick - I think I need another tactic to bend HttpClient into what I want it to do ie respond correctly when I give a url such as: stackoverflow.com/a/29809054/26086
– Dave Mateer
Jan 3 at 16:38
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025575%2fhttpclient-getasync-with-a-hash-in-url%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
URLs sent to servers don't contain the
#
fragment. It's only of use in a client such as e.g. a browser.– Damien_The_Unbeliever
Jan 3 at 15:51
That makes sense thank you. I'm digging into why it is giving a 400 now as I would like for HttpClient to ignore the hash. I've updated the question to highlight the reason I need to do this ie a StackOverflow share url.
– Dave Mateer
Jan 3 at 16:28
As @Damien_The_Unbeliever implies, you'll just need to strip off the hash and everything after it - all that does is tell the browser to jump to that anchor tag in the HTML page (see: w3schools.com/jsref/prop_anchor_hash.asp). So this would mean that your option 2 is your only option in this case...
– rohancragg
Jan 4 at 9:54
1
You could also use the Uri class to parse the Uri and ignore any 'fragments': docs.microsoft.com/en-us/dotnet/api/system.uri.fragment
– rohancragg
Jan 4 at 10:00
Thanks @rohancragg - but what if I'm requesting stackoverflow.com/a/29809054/26086 which then returns the 302, and automatically requests the URL with a hash in it. Maybe I'll have to stop the autoredirect stackoverflow.com/a/10647245/26086 and then strip off the hash, then do another request.
– Dave Mateer
Jan 4 at 10:00