如何知道URL是否被解码/编码? [英] How to know if a URL is decoded/encoded?

查看:1145
本文介绍了如何知道URL是否被解码/编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Javascript方法 decodeURIComponent 来解码编码的网址。现在我遇到了一个问题,有时在服务器之间重定向期间URL被编码两次,有时它只被编码一次。

I am using Javascript method decodeURIComponent to decode an encoded 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.

我想在调用方法 decodeURIComponent 之后检查URL是否仍然是编码的。我怎样才能做到这一点?任何指针对我都非常有帮助。

I want to check that if the URL is still encoded after calling the method decodeURIComponent. How can I do that? Any pointer would be very helpful to me.

如果我递归调用方法并检查如果给定的URL仍然包含%,如果它包含%,则解码它并再次调用该方法;如果没有将它返回给调用者,那会有效吗?

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; and if not return it to the caller, will that work?

对于我的情况我拥有:

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

现在我在我的js中获取callBackUrl的值方法,然后解码它并使用该解码的URL触发 window.open()。参数相同,它有:

Now I am taking the value of the callBackUrl in my js method, then decoding it and firing window.open() with that decoded URL. the parameters are same and it has:


  • adf.tfDoc

  • adf.tfId

  • articleReference

  • previousView

  • fromUCM

  • articleType

  • 开发人员

  • adf.tfDoc
  • adf.tfId
  • articleReference
  • previousView
  • fromUCM
  • articleType
  • developer

参数。所以我知道没有像 value =%..这样的查询字符串。

Parameters into it. So I know there is no query string like value="%..".

我写了以下方法:

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

    return url;
}


推荐答案

反复解码直到找不到标志将在99%的时间内有效。如果您只要找到 /%[0-9a-f] {2} / i 的匹配就可以重复呼叫,它会更好。

Repeatedly decoding until you find no % signs will work over 99% of the time. It'll work even better if you repeatedly call so long as a match for /%[0-9a-f]{2}/i can be found.

但是,如果我(因为某些奇怪的原因)命名文件 100%达到,那将导致问题,因为%ac 将被解码为¬,导致解码失败。不幸的是,没有办法检测到这种情况。

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. 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.

这篇关于如何知道URL是否被解码/编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆