ProgramTo serialize an object and pass it as a parameter to another page. This page gets the parameter and returns it after deserialization, but sometimes it works normally at runtime, sometimes the error message "invalid base-64 character array length" appears.
Searching for information on the Internet means convert is used. when the tobase64string () method is used to encode a string using base64, the parameter length must be equal to an even multiple of 4 or 4. Otherwise, a "formatexception" exception is thrown. However, the parameters I used here are generated using the convert. tobase64string () method, which is theoretically correct. Therefore, convert is used for comparison. string a generated by tobase64string () and convert before deserialization. the parameter string B used by tobase64string () is found to be different between A and B, and the plus sign in a is changed to a space. This is because when a webpage passes a parameter, the plus sign is encoded into a space, but the space is not decoded during decoding. As a result, string B is incorrect and cannot be encoded.
Once the problem is confirmed, the serialization string is used. replace ("+", "% 2B") First encodes the space and then passes it as a parameter to another page for transmission, in this way, the page decodes "% 2B" as the plus sign only when extracting parameters. There are no differences in parameters, and the deserialization succeeds.