I'm trying to find a way to mimic the behavior of stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding from the iPhone, using C# (for Windows Phone).
I don't know anything about iPhone programming, and a web search for that doesn't seem to provide any document pages that might explain what it actually does to help me figure out how to mimic it. I mean, I can see that it "percent escapes" a string using an encoding, but I can't find any examples of what it does to confirm that the output I would be getting is correct. Is it just a simple URL Encode?
I'm not pretty sure if I understood your question, but HttpUtility.UrlEncode might be what you're looking for. At MSDN you'll find it's definition and examples.
Update: this is the official doc from Apple, regarding the iOS method you mentioned.
try this for urls
var uri = new Uri (url);
var nsurl = new NSUrl (uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.UriEscaped));
UIApplication.SharedApplication.OpenUrl (nsurl);
Though this question is very old, but for those still looking for an answer: HttpUtility.UrlPathEncode is the C# equivalent of stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] in Objective C. This is what one should use while composing email etc. in mobile apps / iOS / iPhone app (Xamarin). The difference between HttpUtility.UrlEncode and HttpUtility.UrlPathEncode is -
The UrlEncode method converts each space character to a plus character (+). The UrlPathEncode method converts each space character into the string "%20", which represents a space in hexadecimal notation. Use the UrlPathEncode method when you encode the path portion of a URL in order to guarantee a consistent decoded URL, regardless of which platform or browser performs the decoding.
Also, though the MSDN docs recommend to use UrlEncode instead of UrlPathEncode, only latter is what will work in the referenced scenario.
[Update] Doesn't work same in Android (Xamarin), if you have encoded strings, you'd need to decode them back using HttpUtility.UrlDecode.
Related
I need to validate user input for an href on the server side and need to make sure only http:// and https:// are allowed as a protocol (if specified at all.) The objective is to eliminate possible malicious code like javascript:... or anything alike.
What makes it difficult is the number of ways the colon could be encoded in such string e.g. :, :, :, : , :. I'd like to transform the value and see it as the browsers do before they render the page.
One option could be building a DOM document using AngleSharp as it does the perfect job when parsing attributes. Then I could retrieve the value and validate it but it seems somewhat of an overkill to build the whole DOM tree just to parse one value. Is there a way to use AngleSharp to parse just an attribute value? Or is there a lib which I could use just for this task?
I also found this question, but the method used in there does not really parse the URIs the way browsers do.
You want the HtmlDecode() method. You may need to add a reference to the project to use it.
I am trying to build an ASP.NET MVC application, which will use Thumbor as a photo resizing backend, but have been running into problems with the security. Thumbor uses a SHA1 HMAC hash as a security system, which is based on the URL. So, the URL may look like:
http://thumbor-server/1234567890123456789012345678/300x200/smart/path/to/image.jpg
1234567890123456789012345678 being the hmac made up of a secret key and the 300...image.jpg section...
anyway, i can create the HMAC value alright, or at least i think i can, but when generating the URL, Thumbor sugests using the urlsafe_base64encode function from Python. I have tried System.Convert.ToBase64String, but thats not working, and url encoding the string does not work either. By "not working", i mean Thumbor is telling me the URL is malformed. There is not much to go by...
So, is there an equivalent? and if not, how would one go about generating a string the way it does?
I have managed to get this working, by taking the Base64 string and replacing the + char with - and the / char with a _. This seems to be the way that Python is doing its urlsafe_b64encode
I'm trying to find a way to give my application a YouTube URL (as copied from the address bar in a browser) and extract the unique video ID from that URL. I want to stay away from regex or any other string manipulation as a solution since not all YouTube URLs are the same and may continue to change.
There has to be some way to use the YouTube API for .NET (specifically C# for my solution) to simply call a method/function, pass the method the URL for the video, and finally the method would return the video ID as a string.
I've been trying to find documentation on this and so far I can only find info on using methods to retrieve data about a video based on already having and providing the video ID - which I do not have at this point.
I recognize which part of the YouTube URLs identify the video, but the users of my application should not have to be concerned with that.
It would be greatly appreciated if anybody could help me find a solution here.
Thanks!
Unfortunately I do not think this will work. But then again, I don´t understand the problem you have with using the URL. The URL in itself is a kind of an ID and the v property of the URL specifies which video it is. It seems you already know this.
Regarding your problems with using an URL:
Not all YouTube URLs are the same
R: No, but it doesn´t matter because using RegEx you would only read the v property (v=-l6P7VFKnW8), alternatively the short be variant
YouTube URLs may continue to change
R: Yes, they may. However it is unlikely that YouTube would change the identifier anytime soon because of the effects it would have on API's and other infrastructure. Besides, if you have a pure and modular generic solution you wouldn´t have to change more than one RegEx to comply with the new Id.
Related: Youtube .NET Data API: Retrieve only videoID?
Related: C# regex to get video id from youtube and vimeo by url
i integrating with an software, where they sending their document to my url with too large query string value. i.e. more than 75000 char for a parameter. i am in a R&D phase to check whether integration is works. i came to know that browsers will limit the query string. i want to get their document into to my server. i google but not get the answer. the url is in following fashion
Http:\\myurl?document=thierdocument in base64 encoded format
guide me to overcome the problem
This is not going to work. The query string is limited to a few thousand characters depending on the browser (i.e. 2083 characters for IE). Use a HTTP POST instead and put the document in binary format in the body of the request.
The main idea of a URL was to be a Uniform Resource Locator, not to pass all the data as part of the URL itself. You cannot work around the browser limits on URLs (neither should you arguably) - an alternative could be passing the document id in form of a number or Guid, then looking up that document to process as part of your page.
My suggestion is to move the data from query string, to post form.
My suggestion is to move the data from query string, to post form.
Why ?
one reason is the the url data, including your big string, is used to know if the page is going to cached by the browser or not. So I think that browser him self have a problem remeber this big string.
other reason is that this url is travel as it is, a big one, and is very possible to not reach the target.
The 2083 charatercs in IE I think that is refered only on URL, not on the included data.
You will have to do it using POST query.
Taken from What is the limit on QueryString / GET / URL parameters?
The spec for URL length does not dictate a minimum or maximum URL
length, but implementation varies by browser. On Windows: Opera
supports ~4050 characters, IE 4.0+ supports exactly 2083 characters,
Netscape 3 -> 4.78 support up to 8192 characters before causing errors
on shut-down, and Netscape 6 supports ~2000 before causing errors on
start-up.
I have a Silverlight application that is building a URL. This URL is a call to a REST-based service. This service expects a single parameter that represents a location. The location is in the form of "city, state". To build this URL, I'm calling the following code:
string url = "http://www.example.com/myService.svc/";
url += HttpUtility.UrlEncode(locationTextBox.Text);
If a user enters "chicago, il" into locationTextBox, the result looks like this:
http://www.example.com/myService.svc/chicago%2c+il
In reality though, I was kind of expecting the URL to look like;
http://www.example.com/myService.svc/chicago,%20il
When testing my service via the browser URL, the one I am expecting works. However, the URL that is being generated is not working. What am I doing wrong?
I would recommend Uri.EscapeDataString instead of using HttpUtility functions. See discussion in Server.UrlEncode vs. HttpUtility.UrlEncode.
Try to use the UrlPathEncode() method.
View the remarks at:
http://msdn.microsoft.com/en-us/library/h10z5byc.aspx
Quote:
You can encode a URL using with the
UrlEncode() method or the
UrlPathEncode() method. However, the
methods return different results. The
UrlEncode() method converts each space
character to a plus character (+). The
UrlPathEncode() method converts each
space character into the string "%20",
which represents a space in
hexadecimal notation. Use the
UrlPathEncode() method when you encode
the path portion of a URL in order to
guarantee a consistent decoded URL,
regardless of which platform or
browser performs the decoding.
The safest bet is to use the AntiXss library. It has more standard (and secure) versions for encoding contents to various purposes (like Url encodes, Html and HtmlAttribute encodes, and more).
there's the old 3.1 version available for download from MS site (http://www.microsoft.com/downloads/details.aspx?FamilyId=051ee83c-5ccf-48ed-8463-02f56a6bfc09), which will work with older .NET versions, and the new one at http://wpl.codeplex.com/