Reading URL with # instead of ? in it - c#

I am workign with OAuth2 and and I am calling Google API. Google returns results after call is complete and I am supposed to read from the query parameters. Now, the kind of URL that Google Returns is weird and it has anchor # in it exactly where there should be a ?
and URL looks something like
http://localhost.contestfactory.com/enduser/#state=MDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAw&access_token=ya29.AHES6ZTjWwx7hHO4WnmfQ_lwJSpATCqA_DUZCC_ZIjdyPWA96OS0EN0&token_type=Bearer&expires_in=3600
Because of # in the URL my C# code fails to read beyond #. Is there anyway I can deal this problem in C#?

The part after the # is called the URL fragment (or hash).
It is never sent to the server.

what ur saying looks like hash fragment.. and it is never sen tto the server. what u can do instead make a JS script that changes all the "#" to "?" that way they will be treated as query string and will be sent to the server

Related

validate website string c#

I have a form that users enters there website. Problem is some users put their email address in which I do not want. I want a way to check if the url is well structured. e.g. no #, must have a root domain. www subdomains are optional. I am unable to find this anywhere.
I have tried this code
if (!Uri.TryCreate("http://" + websiteurl, UriKind.Absolute, out uri) || null == uri)
returning false on error but my problem is that it still validates without a root domain e.g. I can put in
http://websitename
and validates fine which I do not want. It does return false when I have put in
http://websitename#.
Is there a way I can overcome this problem? also I added
http:// in the passthrough value because the url never validates.
You can use:
Uri.IsWellFormedUriString(inputUrl, UriKind.RelativeOrAbsolute)
Depending on your performance needs, maybe issuing a quick HttpWebRequest for the website url they give and verifying that you get back a success response might be a good option.
You could try with a regular expression.
Uri.IsWellFormattedUriString won't solve the problem here, which includes the ability to distinguish a valid Url from an email address. Both are well formatted Uris.
Use a regular expression. Here's one from the MS forums using C#:
Url validation with Regular Expression
But you should really validate this before it gets sent to the server. If you use the Peter Blum validators, he's already done the work for you.
Peter Blum's Validators
Or if you want to put in your own JavaScript file, check out this StackOverflow thread.
Url Validation using jQuery

Is there any way to check Rewrited URL in browser

In my Project i don't want to show query string values to users. For that case i used URL Rewriting in asp.net. So my URL Looks like below.
http://localhost/test/default.aspx?id=1
to
http://localhost/test/general.aspx
The first URL will be rewrites to second URL, but it will still executes the default.aspx page with that query string value. This is working fine.
But my question is that, is there any way the user can find that original URL in browser?
The answer is no.
The browser can't tell what actual script ended up servicing the request - it only knows what it sent to the server (unless the server issued a redirect, but then the browser would make a new request to the redirect target).
Since URL rewriting takes an incoming request and routes it to a different resource, I believe the answer is yes. Somewhere in your web traffic you are requesting http://localhost/test/default.aspx?id=1 and it is being rewritten as the new request http://localhost/test/general.aspx.
While this may hide the original request from displaying in the browser, at some point it did send that original URL as an HTTP GET.
As suggested, use Firebug or Fiddler to sniff the traffic.
I figured answer for my question. We can easily found the rewritten urls. If we saw the view source of that page in browser then we can see that original url with querystring values.

What is the correct encoding for querystrings?

I am trying to send a request to an url like this "http://mysite.dk/tværs?test=æ" from an asp.net application, and I am having trouble getting the querystring to encode correctly. Or maybe the querystring is encoded correctly, the service I am connecting to just doesn't understand it correctly.
I have tried to send the request with different browsers and logging how they encode the request with Wireshark, and I get these results:
Firefox: http://mysite.dk/tv%C3%A6rs?test=%E6
Ie8: http://mysite.dk/tv%C3%A6rs?test=\xe6
Curl: http://mysite.dk/tv\xe6rs?test=\xe6
Both Firefox, IE and Curl receive the correct results from the service. Note that they encode the danish special character 'æ' differently in the querystring.
When I send the request from my asp.net application using HttpWebRequest, the URL gets encoded this way:
http://mysite.dk/tv%C3%A6rs?test=%C3%A6
It encodes the querystring the same way as the path part of the url. The remote service does not understand this encoding, so I don't get a correct answer.
For the record, 'æ' (U+00E6) is %E6 in ISO-LATIN-1, and %C3%A6 in UTF-8.
I could change the remote service to accept the UTF-8 encoded querystring, but then the service would stop working in browsers and I am not really interested in that. Is there a way to specify to .NET that it shouldn't encode querystrings with UTF-8?
I am creating the webrequest like this:
var req = WebRequest.Create("http://mysite.dk/tværs?test=æ") as HttpWebRequest;
But the problem seems to originate from System.Uri which is apparently used inside WebRequest.Create:
var uri = new Uri("http://mysite.dk/tværs?test=æ");
// now uri.AbsolutePath == "http://mysite.dk/tv%C3%A6rs?test=%C3%A6"
It looks like you're applying UrlEncode over the entire URL - this isn't correct, paths and query strings are encoded differently as you've seen. What is doing the encoding of the URI, WebRequest?
You could manually build the various parts using a UriBuilder, or manually encode using UrlPathEncode for the path and UrlEncode for the query string names and values.
Edit:
If the problem lies in the path, rather than the query string you could try turning on IRI support, via web.config
<configuration>
<uri>
<iriParsing enabled="true" />
</uri>
</configuration>
That should then leave international characters alone in the path.
Have you tried the UrlEncode?
http://msdn.microsoft.com/en-us/library/zttxte6w.aspx
I ended up changing my remote webservice to expect the querystring to be UTF-8 encoded. It solves my immediate problem, the webservice can not be correctly called by both PHP and the .NET framework.
However, the behavior is now strange in browsers. Copy pasting an url like "http://mysite.dk/tv%C3%A6rs?test=%C3%A6" into the browser and then pressing return works, it even corrects the encoded characters and displays the location as "http://mysite.dk/tværs?test=æ". If then reload the page (F5) it still works. But if I click on the location bar and press return again, the querystring will become encoded with latin-1 and fail.
For anyone interested here is an old Firefox bugreport about the problem: https://bugzilla.mozilla.org/show_bug.cgi?id=284474 (thanks to #dtb)
So, it seems there is no good solution.
Thanks to everyone who helped though!

c# get complete URL with "#" [duplicate]

This question already has answers here:
Retrieving anchor link in URL for ASP.NET
(3 answers)
Closed 9 years ago.
I have run into a trivial(?) problem when trying to get the whole URL of a c# page.
The url contains the "#"-link ref char. And i would like that to when I grab the URL
Eg. http://localhost/site/page.aspx?var=1&var=2#link
I have tried Request.URL, Request.Querystring etc, it only returns up to the "#"-char.
Is there any way to grab even the last part?
Thanks in advance
That is not possible using server code only. The part after the # is not sent in the request at all, it never leaves the browser.
If you want the part after the # you have to copy it using Javascript before the request is sent to the server, and put the value in the querystring.
Your problem is that # specified an anchor in the page, so the browser sees:
http://localhost/site/page.aspx?var=1&var=2
And then looks in the page for
<a name="link">anchor</a>
As this is client side you need to escape the # from the URL - you can't get it on the server because the browser's already stripped it off.
Are you sure that the stuff after the # isn't sent to the server. I'm pretty sure i made a test with an ajax-app some years ago where the url could be copied and sent to people without javascript by only modifiying the stuff after the # in the url when browsing around with javascript enabled.
That was in PHP and the browser was probably IE6.

Receiving a 500 internal server error when I have a '%' symbol in the query string

I am trying to diagnose a problem that a client site has come across. Basically when you do an address search on their website you can specify a % symbol for wildcard searches. For example you can search for Be% to return Belfast etc.
This queries the database and then redirects you to the results page, passing the search criteria in the querystring for example results.aspx?criteria=Search%20criteria%20is%20Be%
This caused problems if you searched for something like %Belf as %Be is a reserved character in URL encoding. I therefore coded it to replace % with %25 (URL encoding representation of % symbol). This works fine on my test machine, where the URL is now results.aspx?criteria=Search%20Criteria%20is%20%25Be .
This however doesn't work on our clients website for some reason and I can't work out why. The page keeps error-ing with:
Error Code: 500 Internal Server Error. The request was rejected by the
HTTP filter. Contact the server administrator. (12217)
any time you search for something like %Be %Fa %Fe etc etc
Does anyone know if there is an IIS setting for this or something similar?
You might have URLScan installed on your server. URLScan intercepts requests and reject them if it detects invalid characters. It is meant to protect your website from malicious attacks and SQL injection. If you don't configure it correctly then it will reject perfectly reasonable requests. Take a look at the ISAPI filters on your website and see if URLScan is there.
Could this solve your problems? It is written by Zubair Alexander at http://blog.techgalaxy.net/archives/2521

Categories