HTTP 404 Error after encoding Url - c#

When I use HttpUtility.UrlEncode to encode a Url I end up getting a server error.
ASP.Net code:
NavigateUrl=<%# HttpUtility.UrlEncode(string.Concat("UpdateMember.aspx","?groupId=", DataBinder.Eval(Container.DataItem, "GroupID").ToString())) %>
Url:
http://localhost/UITest/MM/UpdateMember.aspx%3fgroupId%3d0032409901
which results in "HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
However using:
NavigateUrl=<%# string.Concat("UpdateMember.aspx","?groupId=", DataBinder.Eval(Container.DataItem, "GroupID").ToString()) %>
results in the Url:
http://localhost/UITest/MM/UpdateMember.aspx?groupId=0032409901
which works out fine. Am I doing something incorrectly?

You shouldn't encode the entire URL, atleast not the 1st "?" symbol. If you encode the ? too then your application looks for a file with the name & extension "UpdateMember.aspx%3fgroupId%3d0032409901" which doesn't exist.
Probably, this is what you should do.
http://localhost/UITest/MM/UpdateMember.aspx?groupId%3d0032409901

HttpUtility.UrlEncode() URL-encodes a string
That means that it escapes all special characters from the string so that you can insert it as part of a URL without any characters being parsed as URL modifiers.
You use this kind of escape function when inserting arbitary text as part of a URL.

Related

Problems with aspx getting url with parentheses

I am getting problems with ASP getting the URL when it has parentheses.
My site has URLs like this one: http://example.com/my-amazing-product-%28online%29
So, I am trying to get that URL using the instruction:
var requestURL = HttpContext.Current.Request.RawUrl;
But, that instruction is getting the URL: http://example.com/my-amazing-product-(online)
It automatically converts the parentheses from %28 to ( and from %29 to ), but I don't need that conversion, I need the original URL with %28 and %29
And I can't use replace to fix the URL. I have to get that URL directly from the request or something similar.
Can anyone explain that weird behavior and solve the problem?

Handling an ampersand in a url

I have a hyperlink on a .aspx page
<asp:HyperLink ID="hlTest" runat="server" NavigateUrl="#">Test Link</asp:HyperLink>
On the code behind page I have:
string link = "http://myDoman/myEmailAttachments/1436/" + HttpUtility.HtmlEncode("Picture of Jim&John.jpg");
hlTest.NavigateUrl = link;
This generates a url that looks like:
http://myDomain/myEmailAttachments/1436/Picture%20of%20Jim&John.jpg
This causes a message to be shown: A potentially dangerous Request.Path value was detected from the client (&).
I have tried using Server.Urlencode. This produces a url that looks like ...
http://myDomain/myEmailAttachments/1436/Picture+of+Jim%26John.jpg
This causes the same message to be shown: A potentially dangerous Request.Path value was detected from the client (&).
If I have a file called ...
Picture of Jim&John.jpg
... How can I get it into a hyperlink so it will actually go and get the file? Thank you for any help.
That is because you don't want to HTML encode (HttpUtility.HtmlEncode), but URL encode (HttpUtility.UrlEncode). Then the %26 will be rewritten as & which is the correct format for an URL. That will prevent ASP.NET see it as potentially malicious.
string link = "http://myDoman/myEmailAttachments/1436/"
+ HttpUtility.UrlEncode("Picture of Jim&John.jpg")
;

Weird return url

I am trying to use oauth2.
I make a get request, and then I get redirected at a callback url that I have set up before. The problem lies in the fact that the url parameters get preceded by the # sign and thus php, .net can't read them!
I get redirected in the following url in my browser:
http://localhost:1787/About.aspx?#access_token=f3EToovT2bQNNOQ&token_type=bearer&merchant_id=A6BGD4BH&response_type=token
Request.Params is empty, request.query string is empty. Even when I use php and print the $_REQUEST array still is empty!
How is this possible?
Whatever comes after the # doesn't mark as DATA being sent to the server, but a hash on the client side.
Try redirect using JavaScript only the hash:
window.location = window.location.pathname + '?' + window.location.hash.substring(1);

How to control colon (:) URL Query String?

I need to pass the Query String of ID in which colon (:) is included i.e. ABC_PD:123456.
When I am using this ID in query String session and when its redirect to another page in URL it give 404 no error found error on webpage.
So can any one provide the solution for this so that I can pass the colon in query string and when Page will be redirect without 404 error.
Solution would be much appreciated.
When you build the URL that you redirect to, you need to encode special characters by using the UrlEncode-method:
var redirectTo = "/mypage.aspx?id=" + HttpUtility.UrlEncode("id123:456");
This will create a query string that looks like this and will be interpreted correctly:
"/mypage.aspx?id=id123%3A456"

USPS Address Validation Fail

When validating an address, I get this error:
ex = {"Error Loading XML: The following tags were not closed: AddressValidateRequest, Address, Address1.\r\n"}
or I get another error saying the address cannot be found. Is there a better way to validate this address?
Here is my URL:
http://production.shippingapis.com/ShippingAPI.dll?API=Verify&XML=<AddressValidateRequest USERID="402JMAWE3481"><Address ID="1"><Address1>123 Main St</Address1><Address2></Address2><City>Watertown</City><State>MA</State><Zip5>02472</Zip5><Zip4></Zip4></Address></AddressValidateRequest>
According what I see on the error description, the problem could be that you need to remove \r\n from the xml before adding it to the url. Don't forget also to url encode it.
You actually need to HtmlEncode it and then UrlEncode it.
This is becasue you're actually sending XML (which requires & instead of &) but its a URL so it needs encoding to make each & into %26
Here's a complete working URL - you just need to put in your USERID.
http://production.shippingapis.com/ShippingAPI.dll?API=Verify&XML=<AddressValidateRequest USERID="123USERID567"><Address ID="1"><Address1></Address1><Address2>10051+Orr+%26amp%3b+Day+Rd</Address2><City>santa+fe+springs</City><State>ca</State><Zip5>90670</Zip5><Zip4></Zip4></Address></AddressValidateRequest>
You'll see it contains this funky looking string:
10051+Orr+%26amp%3b+Day+Rd
Which I got by doing this :
HttpUtility.UrlEncode(HttpUtility.HtmlEncode("10061 Orr & Day Rd"))
[This specific error I got back when I didn't encode properly was Error Loading XML: Whitespace is not allowed at this location]

Categories

Resources