简体   繁体   English

如何知道 URL 是否已解码/编码?

[英]How to know if a URL is decoded/encoded?

I am using Javascript method decodeURIComponent to decode an encoded URL.我正在使用 Javascript 方法decodeURIComponent来解码编码的 URL。 Now I am having an issue, that sometimes the URL is get encoded twice during redirection between servers, sometimes it is encoded only once.现在我遇到了一个问题,有时 URL 在服务器之间重定向期间被编码两次,有时它只被编码一次。

I want to check that if the URL is still encoded after calling the method decodeURIComponent .我想检查在调用方法decodeURIComponent后 URL 是否仍然被编码。 How can I do that?我怎样才能做到这一点? Any pointer would be very helpful to me.任何指针都会对我很有帮助。

Update - 1更新 - 1

If I recursively call a method and check that if the given URL still contains "%", if it contains "%" then decode it and call the method again;如果我递归调用一个方法并检查给定的 URL 是否仍然包含“%”,如果它包含“%”,则对其进行解码并再次调用该方法; and if not return it to the caller, will that work?如果不将其返回给调用者,那行得通吗?

Update - 2更新 - 2

For my case I have:对于我的情况,我有:

callBackUrl=http%253A%252F%252Fadbc.com%252FPOSM%252Fapp%252Fpages%252Fadf.task-flow%253Fadf.tfDoc%253D%25252FWEB-INF%25252Ftask-flows%25252Fcatalog-edit-task-flow.xml%2526adf.tfId%253Dcatalog%2526_adf.ctrl-state%253Db9akorh22_9%2526articleReference%253D10C00135%2526previousView%253Dcatalog-home%2526fromUCM%253Dtrue%2526articleType%253Dposm%2526developer%253Dcentral

Now I am taking the value of the callBackUrl in my js method, then decoding it and firing window.open() with that decoded URL.现在我在我的 js 方法中获取 callBackUrl 的值,然后解码它并使用该解码的 URL 触发window.open() the parameters are same and it has:参数是相同的,它有:

  • adf.tfDoc文档
  • adf.tfId文件名
  • articleReference文章参考
  • previousView上一个视图
  • fromUCM来自UCM
  • articleType文章类型
  • developer开发商

Parameters into it.参数进去。 So I know there is no query string like value="%.." .所以我知道没有像value="%.."这样的查询字符串。

Update - 3更新 - 3

I have written the following method:我写了以下方法:

var decodeURLRecursively = function(url) {
    if(url.indexOf('%') != -1) {
        return decodeURLRecursively(decodeURIComponent(url));
    }

    return url;
}

There is a simple way to konw if a URL string is encoded.有一种简单的方法可以知道 URL 字符串是否已编码。

Take the initial string and compare it with the result of decoding it.取初始字符串并将其与解码结果进行比较。 If the result is the same, the string is not encoded;如果结果相同,则不对该字符串进行编码; if the result is different then it is encoded.如果结果不同,则对其进行编码。

I had this issue with my urls and I used this functions:我的网址有这个问题,我使用了这个功能:

function isEncoded(uri) {
  uri = uri || '';

  return uri !== decodeURIComponent(uri);
}

So now I can use isEncoded as the discriminant in a while loop (or with recursion) to know if I need to keep calling decodeURIComponent on the string:所以现在我可以在 while 循环(或递归)中使用isEncoded作为判别式来知道我是否需要继续在字符串上调用decodeURIComponent

function fullyDecodeURI(uri){

  while (isEncoded(uri)){
    uri = decodeURIComponent(uri);
  }

  return uri;
}

This solved the problem for me of decoding urls encoded multiple times.这为我解决了解码多次编码的 url 的问题。 Hope this helps.希望这可以帮助。

Repeatedly decoding until you find no % signs will work over 99% of the time.重复解码直到你发现没有%符号将在 99% 的时间内工作。 It'll work even better if you repeatedly call so long as a match for /%[0-9a-f]{2}/i can be found.只要能找到/%[0-9a-f]{2}/i的匹配项,如果您反复调用它,效果会更好。

However, if I were (for some bizarre reason) to name a file 100%achieved , that would cause a problem because %ac would be decoded to ¬ , causing the decode to fail.但是,如果我(出于某种奇怪的原因)将文件命名为100%achieved ,则会导致问题,因为%ac将被解码为¬ ,从而导致解码失败。 Unfortunately there's no way to detect this case.不幸的是,没有办法检测到这种情况。

Ideally you should know if something is encoded more than once, and optimally you shouldn't let it happen in the first place.理想情况下,您应该知道某些内容是否被多次编码,并且最好一开始就不应该让它发生。

you can keep decode until the string did not change:您可以继续解码,直到字符串没有改变:

str = "xxxxxxx"
dec_str = decode(str)
while(dec_str != str)
     str = dec_str;
     dec_str = decode(str);
function checkEncodeURI(str) {
 return /\%/i.test(str)
}

Test :测试:

let url1 = "https://quora.com/unanswered/I’m-looking-for-a-Freaks-And-Friends-Fox-Glitter-Face-Hinge-Wallet-for-a-Christmas-gift-I’ve-searched-for-hours-online-but-no-one-seemed-to-have-it-does-anyone-know-where-I-can-find-one"
let url2 = 'https://www.quora.com/unanswered/I%E2%80%99m-looking-for-a-Freaks-And-Friends-Fox-Glitter-Face-Hinge-Wallet-for-a-Christmas-gift-I%E2%80%99ve-searched-for-hours-online-but-no-one-seemed-to-have-it-does-anyone-know-where-I-can-find-one'
let a = checkEncodeURI(url1)
console.log(a)
let b = checkEncodeURI(url2)
console.log(b)

There is really simple and fast and trusted way to know if a URL string is encoded or Not.有一种非常简单、快速且值得信赖的方法来了解 URL 字符串是否已编码。

For Example: decodeURIComponent(decodeURIComponent(encodeURIComponent("SWQgPSA0NjI%3D"))) <> "SWQgPSA0NjI=" ----> Not equal to orginal value then Already encoded例如: decodeURIComponent(decodeURIComponent(encodeURIComponent("SWQgPSA0NjI%3D"))) <> "SWQgPSA0NjI=" ----> 不等于原始值然后已经编码

just this code:只是这个代码:

  var encValue = encodeURIComponent(Value);
    try {
         if (decodeURIComponent(decodeURIComponent(encValue)) === Value) {
          //not encodec yet...so return encoded of val
          return encValue;
         }
       } catch (err) {
           //not encodec yet...so return encoded of val
           return encValue;
       }

 return Value  //same value returned

                        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM