How to encode IP for GET request in api in .NET - c#

I'm trying to figure out how to send IP in GET request. I want to call GET request like : /api/endpoint/12.12.12.12. I tried to encode it but HttpUtility.UrlEncode won't encode dots for IP alone. When I try use %2E as dot then IIS throws 404.11 - The request filtering module is configured to deny a request that contains a double escape sequence.. How to I make it the right way?

Try to encode it in base 64. You can find how to do it here
/api/endpoint/MTIuMTIuMTIuMTI=

You could just do a string replace.
"12.12.12.12".Replace(".","%2E");

Add a slash at the end of the URL:
/api/endpoint/12.12.12.12/
this should work

Related

Can't get parameter from url via query string

I am implementing google login on my site.
Problem is that when google redirect me back to my site (after confirmation) I can't get access token from query string.
This is URL:
http://localhost/mysite/west/Default.aspx#state=/profile&access_token=ya29.qQDrtcVtgOEbS86Bg10puFG3dksJz74BlrEGulHldlJW2o5qQ6g7ilF17zQsm8iMLG0C82PQyp2Z-g&token_type=Bearer&expires_in=3600
I suspect that this #state=/profile make some issue but can't handle it.
Am I missing something?
If URL is like this , note that there is # after Default.aspx , it is not ?, then there is no direct way to get get querystring ( they are known as URL fragments not querystring), they are meant to be parse at client side and server side don't have access to URL Fragments.
http://localhost/mysite/west/Default.aspx#state=/profile&access_token=ya29.qQDrtcVtgOEbS86Bg10puFG3dksJz74BlrEGulHldlJW2o5qQ6g7ilF17zQsm8iMLG0C82PQyp2Z-g&token_type=Bearer&expires_in=3600
Link contains # ,means an anchor, a position, on a webpage. The browser sends a GET request to the server containing only the address of the entire page, with no anchor, fragment or whatever. When the server returns the page, the browser knows where to position it so the location of the anchor is visible. In clientside or Javascript it is possible as it has access to the anchor.
Read this - How to get Url Hash (#) from server side
You could use
document.URL to get the url.
Then split the url by #state=/profile&
Then the second part of the array split by &.
Then each section split by first =
There may be a more elegant solution but this should work.

HttpWebRequest truncating URL with non-english symbols in it

I'm creating HttpWebRequest with a dynamic URL that can sometimes contain Russian symbols.
And sometimes I'm encounter an issue I not sure how to deal with: While encoding to URL each of RU characters has been converted into URL character-entity equivalents (%20...). Apparently after such conversion URL is becoming too long so HttpWebRequest truncating few last characters so instead of proper HTTP result I'm getting 404 error.
Is there any way to bypass this limitation?
My URL looks like this:
http:\\1.1.1.1\some?page=2&var=тестовое значение строки (this part can be very long)
I'm sending this as a GET since web-app I'm working with is expecting GET request at this point.
The only answer to this is you have to send it in any other way as there is a limit for the length of a query string.
The error 404 or "HTTP Error 414. The request URL is too long." you'll get only because of the length of the query string as the data you are sending is longer than the limit.
refer this: http://forums.asp.net/t/1139751.aspx

ASP.NET: parse url having # (hash) sign

I need to parse url that has something after # (hash) sign in my asp.net application. How to do it easily?
Thank you,
You're looking for the Uri class:
new Uri(someString).Fragment
Note that the hash is not sent to the server in an HTTP request.
url.Substring(url.IndexOf('#') + 1)
...where "url" is a string containing the url in question
This is called "hash sign" URI.
After client gets PAGE responsed including js,
the contents after '#' would be handled by client using responsed js to get "real" URL for redirection.
SEE: https://www.w3.org/2001/tag/2011/01/HashInURI-20110115#References
Omg, I meant fragment (after #) part on the server... Though I've looked thrugh and found that it seems to be impossible......

301 Redirect with unicode characters - C#

I need to do a 301 redirect on a URL that may have Unicode characters in it.
HttpUtility.UrlEncode isn't doing what I need because if I encode the whole URL it encodes any ':' or '/'
HttpUtility.UrlEncode("http://www.हिन्दी.com") = http%3a%2f%2fwww.%e0%a4%b9%e0%a4%bf%e0%a4%a8%e0%a5%8d%e0%a4%a6%e0%a5%80.com
(also: http://www.%e0%a4%b9%e0%a4%bf%e0%a4%a8%e0%a5%8d%e0%a4%a6%e0%a5%80.com doesn't seem to work in firefox or IE, but it does in Chrome)
Only other thing I can think of is to encode the different parts of the URL so that the protocol doesn't get encoded.
You need to take a look at RFC 3490 which details how to correctly encode international domain names -- this is also why when you encode just the domain portion it only works in Chrome)
So I figured out a almost 100% solution to this. Thanks to Rowland Shaw and Rup for pointing me in the direction of IDNs.
I tried using an IdnMapper, whose function GetAscii will convert unicode domain names to punycode, but I didn't have the domain separated from the rest of the URL. I tried putting the url into a Uri object, but I would get a UriFormatException if the url had unicode characters.
That led me to: http://msdn.microsoft.com/en-us/library/system.uri(v=VS.90).aspx
which tells how to enable the Uri class to accept unicode and do the IDN and IRI conversions. It says you have to add something to the .NET 2.0 machine.config file, but you can put the line in web.config and it will work.
After I got the Uri working with unicode, I pieced together the url and did a redirect:
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", uri.Scheme + "://" + uri.DnsSafeHost + uri.PathAndQuery + uri.Fragment);
Response.End();
This works for Chrome and Firefox 3.6, but fails in IE8. I'm still trying to solve that problem and will update here if I find a solution.

C# decodes URL containing %2F on path, is there any way to instruct API to send the URL as it is?

I am having a URL in below format
abcd.com/xyz/pqr%2Fss/abc
I want this to be send to server as it is.
When I build Uri using System.Uri it converts it to abcd.com/xyz/pqr/ss/abc
and it fails as I don't have a URL with the specified path.
When I tried with double encoding
(abcd.com/xyz/pqr%252Fss/abc) it send the Uri as it is but it fails as server side it is converted to (abcd.com/xyz/pqr%2Fss/abc)
If you construct your uri as such:
Uri u = new Uri("http://abcd.com/xyz/pqr%2Fss/abc")
Access the encoded string like this:
u.OriginalString
I had this problem too, but I found the solution: when you use HttpUtility.UrlEncode to be sure that the application will read the url right you have to construct the link this way:
http://www.abcd.com/xyz?val=pqr%2Fss
and not like this
http://www.abcd.com/xyz/pqr%2Fss
where pqr%2Fss is the result of the HttpUtility.UrlEncode("SOME STRING")

Categories