An issue with BASE64 encrypted strings added to the URL (java.net.MalformedURLException:Illegal character in URL)

Source: Internet
Author: User
Tags base64



Order
Yesterday in doing a Demo, because it is called third-party interface, the use of HTTP communication protocol, according to the document, the need to encrypt the parameters to add to the URL, but it is this seemingly ordinary operation, but let me really cost a lot of effort.

Background
About BASE64, I don't want to say too much, because it's a very basic encoding, or an encryption method. Do not know can go to my previous blog to see, about encryption, there is a series of articles in front. Let's talk about the situation where the problem arises.
Yesterday get a task, write a Demo, the purpose is to query a series of information, of course, need to call a third-party interface, according to the interface documentation, the interface is the HTTP protocol, Post data need to be BASE64 encryption, because I previously encapsulated a series of cryptographic algorithms, Therefore, it is taken directly to use. Here is a description, in my encapsulated encryption algorithm, the use of sun Base64encoder, that is, I introduced this class:
Import Sun.misc.BASE64Encoder;
There is no problem here, please look below.

Problems that arise
In this way, whenever the test, the program will always report the same error-(Java.net.MalformedURLException:Illegal character in URL), at the beginning, I thought it was my own write the analog Post The code submitted there is a problem, so the online search and check, and finally found no problem. Then, excluding this piece, began to look for other reasons. What baidu, Google have searched all over, the problem also did not solve, originally also did not think is BASE64 encryption problem. Because it has been used before, there is no error.
This question from noon to continue until after work, remember or 5 o'clock in the afternoon, finally found an article from the Internet, it seems to have encountered the same problem, from his article found the reason.

Solution Solutions
The problem arises because in the process of BASE64 encryption, the wrong jar package is not used, and the internal package provided by Sun, which is Sun.misc, is later learned that the Sun.misc package is a dedicated API that Sun provides for internal use, in the Java API documentation We do not see any related BASE64 shadow, is not recommended to use.
After testing, I found that, in addition to using the Sun.misc package BASE64 error, the other two can operate normally, these two include:
    • Com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
    • Org.apache.commons.codec.binary.Base64;

Both of these provided implementations work correctly. As for the two encryption methods, Baidu a bit ...

Conclusion
Through this problem, I found that, many times, we tend to ignore some seemingly useless things, and it is these useless things, from time to time will be on our way to cross a taping, I want to say that, despite the emergence of such a problem, or to resist the temper to solve, do not leave the problem to the future.
And, when we are learning a certain type of knowledge, if there is time, you can think more about, involving the knowledge associated with it, perhaps you will have unexpected gains.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

An issue with BASE64-encrypted strings added to the URL (java.net.MalformedURLException:Illegal character in 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.