URLDecode.decode方法在Java中无法按预期工作

时间:2018-08-08 18:48:45

标签: java url urlencode url-encoding

我正在尝试解码URL编码的帖子正文,并遇到了这个问题。

我正在使用这种方法进行解码(它也可以解码多个编码的网址):

/*
   Microsoft SQL Server Integration Services Script Task
   Write scripts using Microsoft Visual C# 2008.
   The ScriptMain is the entry point class of the script.
*/

using System;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Tasks.ScriptTask;
using System.AddIn;
using WinSCP;

namespace ST_3a1cf75114b64e778bd035dd91edb5a1.csproj
{
    [AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : VSTARTScriptObjectModelBase
    {
        public void Main()
        {
            // Setup session options
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Ftp,
                HostName = (string)Dts.Variables["User::FTPServerName"].Value,
                UserName = (string)Dts.Variables["User::UserName"].Value,
                Password = (string)Dts.Variables["User::Password"].Value
            };

            try
            {
                using (Session session = new Session())
                {
                    // Will continuously report progress of transfer
                    session.FileTransferProgress += SessionFileTransferProgress;

                    session.ExecutablePath = (string)Dts.Variables["User::PathToWinSCP"].Value;

                    // Connect
                    session.Open(sessionOptions);

                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;


                    TransferOperationResult transferResult = session.GetFiles(
                                                                                (string)Dts.Variables["User::ExportPath"].Value
                                                                                , (string)Dts.Variables["User::ImportPath"].Value
                                                                                , false
                                                                                , transferOptions
                                                                            );

                    // Throw on any error
                    transferResult.Check();

                    // Print results
                    bool fireAgain = false;
                    foreach (TransferEventArgs transfer in transferResult.Transfers)
                    {
                        Dts.Events.FireInformation(0, null,
                            string.Format("Download of {0} succeeded", transfer.FileName),
                            null, 0, ref fireAgain);
                    }
                }

                Dts.TaskResult = (int)DTSExecResult.Success;
            }

            catch (Exception e)
            {
                Dts.Events.FireError(0, null,
                    string.Format("Error downloading file: {0}", e),
                    null, 0);

                Dts.TaskResult = (int)DTSExecResult.Failure;
            }
        }

        private static void SessionFileTransferProgress(object sender, FileTransferProgressEventArgs e)
        {
            //bool fireAgain = false;


            // Print transfer progress
            Console.Write("\r{0} ({1:P0})", e.FileName, e.FileProgress);

            /*Dts.Events.FireInformation(0, null,
                            string.Format("\r{0} ({1:P0})", e.FileName, e.FileProgress),
                            null, 0, ref fireAgain);*/

            // Remember a name of the last file reported
            _lastFileName = e.FileName;
        }

        private static string _lastFileName;
    }
}

当输入网址为“ a%20%2B%20b%20%3D%3D%2013%25!”时,则在调试时,控件在第public static String decodeUrl(String url) { try { String prevURL=""; String decodeURL=url; while(!prevURL.equals(decodeURL)) { prevURL=decodeURL; decodeURL= URLDecoder.decode( decodeURL, "UTF-8" ); } return decodeURL; } catch (UnsupportedEncodingException e) { return "Issue while decoding" +e.getMessage(); } } 行之后不会出现。也没有例外。

问题在于控件不会超出“ decodeURL”行。

可能是什么原因导致此问题?请使用调试器来模拟该问题。

1 个答案:

答案 0 :(得分:2)

只需在Java 8u151上对其进行测试。这在循环的第二次旋转时抛出IllegalArgumentException:“ URLDecoder:不完整的尾随转义(%)模式”。那是因为在第一次解码之后,您有“ a + b == 13%!”,而在第二次解码期间,%应该被引入了一个编码序列,但是没有。我认为这是预期的行为,即使其他语言的标准库不同意。以Python 3.6为例:

auto q1 = std::make_shared<Question>("whats 2+2", "four");
auto q2 = std::make_shared<NumericQuestion> q2("2+2", 4);

auto q3 = std::make_shared<MultipleChoice>("The Right Answer is C", "answer A", "thisisB", "thats C", "Wrong", 'c');

// even though q1...q3 are shared_ptrs to derived classes, they can be safely cast to shared_ptrs to the base class
std::vector<std::shared_ptr<Question>> questions { q1, q2, q3 };

for (const auto& question: questions) {
    question->displayQuestion();
    cin >> ans;
    question->userAnswer(ans);
    question->isCorrect();
}