URL full code to prevent colon from being encoded

Source: Internet
Author: User

Problem Description: Sometimes we need to encode a whole URL, but if the whole code is direct, the colon will be encoded;

Example: http://192.168.1.85:8080/files/1\\paper\\2015-06-05\\150602 Polymer website Paper submission Bug-20150605164633294.doc This string, if encoded directly, the HTTP link will be invalidated.

Solution: I think of the solution is to use long text in place of the characters that need to be retained, encoded instead of back;

public static string Restoreurl (string url) {
		//modifier backslash
				url=url.replace ("\ \", "/");
				Use long text instead to keep the string
				url=url.replace (":", "_*colon*_")
				. Replace ("/", "_*slash*_")
				. replace ("\ \", "_* Backslash*_ "). Replace ("
				"," _*blank*_ "). Replace (
				"? "," _*question*_ ")
				. Replace (" = "," _*equal*_ ")
				. replace (";", "_*semicolon*_");
				Encode the
				url=encodingutil.urlencode (URL);
				Url=url.replace ("_*colon*_", ":"). Replace ("
				_*slash*_", "/").
				replace ("_*backslash*_", "\ \").
				Replace ("_*blank*_", "%20")
				. Replace ("_*question*_", "?")
				. Replace ("_*equal*_", "=")
				. Replace ("_*semicolon*_", ";");
		
		return URL;
	}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.