URL也有长度限制?

微软说:"Maximum URL length is 2,083 characters in Internet Explorer"
我们在Pricing系统开发中挑战了一下这个长度,结果以失败告终!......

问题描述:系统中通过一个QLookUp选取Vendor相关信息,包括vendor code, vendor name, vendor site, currency, trade term, ship way, tax七项信息。把处理这些信息的SQL语句和JS语句通过URL传到QLookUPSearchPage.aspx页面来查找数据时,因为URL太长而出错!

QLookUp:
简单来说,它是一个用户控件,通过URL把SQL语句和页面要执行的JS语句传到QLookUPSearchPage.aspx这个页面来查到相应的数据,并返回给调用页面。

Pricing 系统广泛并深入的应用了QLookUp控件。如下图Vendor Code。
[转]浏览器URL长度限制
点出来的页面如下:
[转]浏览器URL长度限制
因为当点选Vendor Code时还会把QLookUp查出来的数据赋给旁边的其它五个控件(Vendor Name....Ship Way),所以它的CustomerFunction属性就会很长:

<uc1:QLookUp ID="QLookUpVendorNew" runat="server" AllowOnBlurEvent="true" OnQLookUPSelected="QLookUpVendorNew_Selected" ServiceMethod="GetVendorCodeList" CustomerFunction="if(clear!=1){objTextBoxText.value=arrayValue[1];document.getElementById('HiddenVendorIDNew').value=arrayValue[0];document.getElementById('QTextBoxVendorNameNew').value=arrayValue[2];document.getElementById('QTextBoxVendorSiteNew').value=arrayValue[3];if(arrayValue[6].length > 0){var DropDownListCurrencyNew = document.getElementById('DropDownListCurrencyNew'); for(i=0;i<DropDownListCurrencyNew.options.length;i++){if(DropDownListCurrencyNew.options[i].text==arrayValue[6]){DropDownListCurrencyNew.options[i].selected = true;}}}  if(arrayValue[4].length > 0){document.getElementById('DropDownListTradeTermNew').value=arrayValue[4];} else{document.getElementById('DropDownListTradeTermNew').options[0].selected=true;}  if(arrayValue[5].length > 0){document.getElementById('DropDownListShipWayNew').value=arrayValue[5];} else{document.getElementById('DropDownListShipWayNew').options[0].selected=true;} } else {document.getElementById('HiddenVendorIDNew').value='';document.getElementById('QTextBoxVendorNameNew').value='';document.getElementById('QTextBoxVendorSiteNew').value='';document.getElementById('DropDownListCurrencyNew').options[0].selected=true;document.getElementById('DropDownListTradeTermNew').options[0].selected=true;document.getElementById('DropDownListShipWayNew').options[0].selected=true;}" alt="required::MSG500014::QButtonAdd" TextBoxCss="QTextBox" Width="160px" WindowTitle="Select Vendor" />

为了看起来清晰点,我把CustomerFunction提出来,格式化后如下

CustomerFunction = "[转]浏览器URL长度限制
if(clear!=1)
[转]浏览器URL长度限制{
[转]浏览器URL长度限制    objTextBoxText.value
=arrayValue[1];
[转]浏览器URL长度限制    document.getElementById('HiddenVendorIDNew').value
=arrayValue[0];
[转]浏览器URL长度限制    document.getElementById('QTextBoxVendorNameNew').value
=arrayValue[2];
[转]浏览器URL长度限制    document.getElementById('QTextBoxVendorSiteNew').value
=arrayValue[3];
[转]浏览器URL长度限制    
if(arrayValue[6].length>0)
[转]浏览器URL长度限制    {
[转]浏览器URL长度限制        
var DropDownListCurrencyNew=document.getElementById('DropDownListCurrencyNew');
[转]浏览器URL长度限制        
for(i=0;i<DropDownListCurrencyNew.options.length;i++)
[转]浏览器URL长度限制        {
[转]浏览器URL长度限制            
if(DropDownListCurrencyNew.options[i].text==arrayValue[6])
[转]浏览器URL长度限制            {
[转]浏览器URL长度限制                DropDownListCurrencyNew.options[i].selected
=true;
[转]浏览器URL长度限制                
[转]浏览器URL长度限制            }
[转]浏览器URL长度限制        }
[转]浏览器URL长度限制    }
if(arrayValue[4].length>0)
[转]浏览器URL长度限制    {
[转]浏览器URL长度限制        document.getElementById('DropDownListTradeTermNew').value
=arrayValue[4];
[转]浏览器URL长度限制        
[转]浏览器URL长度限制    }
else 
[转]浏览器URL长度限制    {
[转]浏览器URL长度限制        document.getElementById('DropDownListTradeTermNew').options[
0].selected=true;
[转]浏览器URL长度限制        
[转]浏览器URL长度限制    }
if(arrayValue[5].length>0)
[转]浏览器URL长度限制    {
[转]浏览器URL长度限制        document.getElementById('DropDownListShipWayNew').value
=arrayValue[5];
[转]浏览器URL长度限制        
[转]浏览器URL长度限制    }
else 
[转]浏览器URL长度限制    {
[转]浏览器URL长度限制        document.getElementById('DropDownListShipWayNew').options[
0].selected=true;
[转]浏览器URL长度限制        
[转]浏览器URL长度限制    }
[转]浏览器URL长度限制}
else 
[转]浏览器URL长度限制{
[转]浏览器URL长度限制    document.getElementById('HiddenVendorIDNew').value
='';
[转]浏览器URL长度限制    document.getElementById('QTextBoxVendorNameNew').value
='';
[转]浏览器URL长度限制    document.getElementById('QTextBoxVendorSiteNew').value
='';
[转]浏览器URL长度限制    document.getElementById('DropDownListCurrencyNew').options[
0].selected=true;
[转]浏览器URL长度限制    document.getElementById('DropDownListTradeTermNew').options[
0].selected=true;
[转]浏览器URL长度限制    document.getElementById('DropDownListShipWayNew').options[
0].selected=true;
[转]浏览器URL长度限制    
[转]浏览器URL长度限制}"

我们来看看点击QLookUp的Search img后,弹出来的QLookUPSearchPage.aspx页面的URL是什么:

http://localhost/Pricing/Web/Public/QLookUPSearchPage.aspx?SQL=+WOeqS2Mk/H6E9ICwIOpVbfgAjIPH2onkkRF7fO6kaqZMiq7gRww2LfgAjIPH2onb0G0aFlDKnaVkj5ZYTna9yDKAZJIz8t1GzKRU95Ah7BZ9czLL/8qv94OjoxTQh3qpAIlrIFYcgI/yWSRJXF+z0WkMLZGBidybjtuO86fXa/6zGD5BBrbqWLysS4RIoiQ/Oq2RKy/Ajodoh7HD8ZKV1oFf7uPqCGstAVtzlVyUgjRZefLedBftM8UdZ8W+v33hsuJ6UTHjFEt8ANGdu0TZwypvrgOaoLbUqMWW37vSb1h4RbPScczZKhU88sr+56rVETs2B/XEur5ykVwvKf+1Q==&DataSource=9lDQbyO73rk=&ReturnClientID=QLookUpVendorNew&DataSourceType=EPS&WindowTitle=Select Vendor&CustomerFunction=if(clear!=1){objTextBoxText.value=arrayValue[1];document.getElementById('HiddenVendorIDNew').value=arrayValue[0];document.getElementById('QTextBoxVendorNameNew').value=arrayValue[2];document.getElementById('QTextBoxVendorSiteNew').value=arrayValue[3];if(arrayValue[6].length > 0){var DropDownListCurrencyNew = document.getElementById('DropDownListCurrencyNew'); for(i=0;i<DropDownListCurrencyNew.options.length;i!!!lionel!!!!!!lionel!!!){if(DropDownListCurrencyNew.options[i].text==arrayValue[6]){DropDownListCurrencyNew.options[i].selected = true;}}}  if(arrayValue[4].length > 0){document.getElementById('DropDownListTradeTermNew').value=arrayValue[4];} else{document.getElementById('DropDownListTradeTermNew').options[0].selected=true;}  if(arrayValue[5].length > 0){document.getElementById('DropDownListShipWayNew').value=arrayValue[5];} else{document.getElementById('DropDownListShipWayNew').options[0].selected=true;} } else {document.getElementById('HiddenVendorIDNew').value='';document.getElementById('QTextBoxVendorNameNew').value='';document.getElementById('QTextBoxVendorSiteNew').value='';document.getElementById('DropDownListCurrencyNew').options[0].selected=true;document.getElementById('DropDownListTradeTermNew').options[0].selected=true;document.getElementById('DropDownListShipWayNew').options[0].selected=true;}

数了一下:1857个字符。OK,截止目前,一切正常

但是,新的需求来了,页面上要加Tax, 值也是从QLookUp中和Vendor Code一起选出来。
不用多想,肯定是往SQL和CustomerFunction两个属性里继续加。
但是加上以后就发现QLookUp摆工了,选不过来数据了。反复的Check也没发现SQL和CustomerFunction有什么语法错误。给CustomerFunction中加了一个alert(''), 竟然放到任何地方都不弹出Message。奇怪...?
把新加的所有内容全部去掉,还原到上面那样就没问题。
最后把Lionel同学拉过过一起研究,他说了一句“难道是URL太长了?”。
嗯?URL长度还有限制?先测试一下再说。把所有新加的内容一点点去掉,到某一点时,发现玄机:
给CustomerFunction中加一个alert('B'),      有问题;
把alert('B')改成var a;                                  没问题了
再把var a改成var a; var b;                          有问题

很明显,确实是CustomerFunction的长度导致了URL过长,从而引发了这个问题。

最后来Check一下有问题的URL:

http://localhost/Pricing/Web/Public/QLookUPSearchPage.aspx?SQL=+WOeqS2Mk/H6E9ICwIOpVbfgAjIPH2onkkRF7fO6kaqZMiq7gRww2LfgAjIPH2onb0G0aFlDKnaVkj5ZYTna9yDKAZJIz8t1GzKRU95Ah7BZ9czLL/8qv94OjoxTQh3qpeffefefeAIlrIFYcgI/yWSRJXF+z0WkMLZGBidybjtuO86fXa/6zGD5BBrbqWLysS4RIoiQ/Oq2RKy/Ajodoh7HD8ZKV1oFDFDGEdfwHBDFDFewf7uPqCGstAVtzlVyUgjRZefLedBftM8UdZ8W+v33hsuJ6UTHjFEt8ANGdu0TZwypvrgOaoLbUqMWW37vSb1h4RbPScczZKhU88sr+56rVETs2Bwrwr2324545322dfdf34t5Ggsde56rerFfdfytDFDFF85684hhhfhfghfjfn/XEur5ykVwvKf+1Q==&DataSource=9lDQbyO73rk=&ReturnClientID=QLookUpVendorNew&DataSourceType=EPS&WindowTitle=Select Vendor&CustomerFunction=if(clear!=1){ objTextBoxText.value=arrayValue[1]+' '+arrayValue[2];document.getElementById('HiddenVendorIDNew').value=arrayValue[0];document.getElementById('QTextBoxVendorNameNew').value=arrayValue[2];document.getElementById('QTextBoxVendorSiteNew').value=arrayValue[3]; document.getElementById('QTextBoxVendorTaxNew').value=arrayValue[7];if(arrayValue[6].length > 0){var DropDownListCurrencyNew = document.getElementById('DropDownListCurrencyNew'); for(i=0;i<DropDownListCurrencyNew.options.length;i!!!lionel!!!!!!lionel!!!){if(DropDownListCurrencyNew.options[i].text==arrayValue[6]){DropDownListCurrencyNew.options[i].selected = true;}}}  if(arrayValue[4].length > 0){document.getElementById('DropDownListTradeTermNew').value=arrayValue[4];} else{document.getElementById('DropDownListTradeTermNew').options[0].selected=true;}  if(arrayValue[5].length > 0){document.getElementById('DropDownListShipWayNew').value=arrayValue[5];} else{document.getElementById('DropDownListShipWayNew').options[0].selected=true;} } else {document.getElementById('HiddenVendorIDNew').value='';document.getElementById('QTextBoxVendorNameNew').value=''; document.getElementById('QTextBoxVendorTaxNew').value=’’;document.getElementById('QTextBoxVendorSiteNew').value='';document.getElementById('DropDownListCurrencyNew').options[0].selected=true;document.getElementById('DropDownListTradeTermNew').options[0].selected=true;document.getElementById('DropDownListShipWayNew').options[0].selected=true;}

数了一下:2087个字符。

我们来了解一下微软的说法:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q208427

[转]浏览器URL长度限制[转]浏览器URL长度限制Maximum URL length is 2,083 characters in Internet Explorer
[转]浏览器URL长度限制Microsoft Internet Explorer has a maximum uniform resource locator (URL) length of 2,083 characters. Internet Explorer also has a maximum path length of 2,048 characters. This limit applies to both POST request and GET request URLs. 
[转]浏览器URL长度限制
[转]浏览器URL长度限制If you are using the GET method, you are limited to a maximum of 2,048 characters, minus the number of characters in the actual path.
[转]浏览器URL长度限制
[转]浏览器URL长度限制However, the POST method is not limited by the size of the URL for submitting name/value pairs. These pairs are transferred in the header and not in the URL. 
[转]浏览器URL长度限制
[转]浏览器URL长度限制RFC 2616, "Hypertext Transfer Protocol -- HTTP/1.1," does not specify any requirement for URL length. 

已经明了. 我们最后的做法:

用一个JS function来给CustomerFunction瘦身,如下:

[转]浏览器URL长度限制 <uc1:QLookUp ID="QLookUpVendorNew" runat="server" SelectOnly = "true"
[转]浏览器URL长度限制                            CustomerFunction
="if(clear!=1){objTextBoxText.value=arrayValue[1]+' '+arrayValue[2];SelectNewVendor(arrayValue);}else {document.getElementById('HiddenVendorIDNew').value='';document.getElementById('QTextBoxVendorTaxNew').value='';document.getElementById('QTextBoxVendorCurrencyNew').value='';document.getElementById('QTextBoxVendorSiteNew').value='';document.getElementById('DropDownListCurrencyNew').options[0].selected=true;document.getElementById('DropDownListTradeTermNew').options[0].selected=true;document.getElementById('DropDownListShipWayNew').options[0].selected=true;}"
[转]浏览器URL长度限制                            alt
="required::MSG500014::QButtonAdd" TextBoxCss="QTextBox" Width="160px" WindowTitle="Select Vendor" />


 

[转]浏览器URL长度限制<script type="text/javascript">
[转]浏览器URL长度限制        
function SelectNewVendor(arrayValue)
[转]浏览器URL长度限制        {
[转]浏览器URL长度限制            document.getElementById('HiddenVendorIDNew').value
=arrayValue[0];
[转]浏览器URL长度限制            document.getElementById('QTextBoxVendorSiteNew').value
=arrayValue[3];
[转]浏览器URL长度限制            document.getElementById('QTextBoxVendorTaxNew').value
=arrayValue[7];
[转]浏览器URL长度限制            document.getElementById('QTextBoxVendorCurrencyNew').value
=arrayValue[6];
[转]浏览器URL长度限制            
var dropCurrency=document.getElementById('DropDownListCurrencyNew');
[转]浏览器URL长度限制            
var dropTradeTerm=document.getElementById('DropDownListTradeTermNew');
[转]浏览器URL长度限制            
var dropShipway=document.getElementById('DropDownListShipWayNew');
[转]浏览器URL长度限制             
var neverSelect = true;
[转]浏览器URL长度限制            
//Currency DropDownList 
[转]浏览器URL长度限制
            if(arrayValue[6].length>0)
[转]浏览器URL长度限制            {    
[转]浏览器URL长度限制                neverSelect 
= true;           
[转]浏览器URL长度限制                
for(i=0;i<dropCurrency.options.length;i++)
[转]浏览器URL长度限制                {
[转]浏览器URL长度限制                    
if(dropCurrency.options[i].text==arrayValue[6])
[转]浏览器URL长度限制                    {
[转]浏览器URL长度限制                        dropCurrency.options[i].selected
=true;   
[转]浏览器URL长度限制                        neverSelect 
= false;                        
[转]浏览器URL长度限制                    }
[转]浏览器URL长度限制                }               
[转]浏览器URL长度限制                
if(neverSelect == true
[转]浏览器URL长度限制                {
[转]浏览器URL长度限制                    dropCurrency.options[
0].selected=true;       
[转]浏览器URL长度限制                }   
[转]浏览器URL长度限制            }
[转]浏览器URL长度限制            
else 
[转]浏览器URL长度限制            {
[转]浏览器URL长度限制                dropCurrency.options[
0].selected=true;                
[转]浏览器URL长度限制            }
[转]浏览器URL长度限制            
//Trade Term DropDownList
[转]浏览器URL长度限制
            if(trim(arrayValue[4]).length>0)
[转]浏览器URL长度限制            {
[转]浏览器URL长度限制                neverSelect 
= true;         
[转]浏览器URL长度限制                
for(i=0;i<dropTradeTerm.options.length;i++)
[转]浏览器URL长度限制                {
[转]浏览器URL长度限制                    
if(dropTradeTerm.options[i].text==arrayValue[4])
[转]浏览器URL长度限制                    {
[转]浏览器URL长度限制                        dropTradeTerm.options[i].selected
=true;   
[转]浏览器URL长度限制                        neverSelect 
= false;                     
[转]浏览器URL长度限制                    }
[转]浏览器URL长度限制                }
[转]浏览器URL长度限制                
if(neverSelect == true
[转]浏览器URL长度限制                {
[转]浏览器URL长度限制                    dropTradeTerm.options[
0].selected=true;       
[转]浏览器URL长度限制                }    
[转]浏览器URL长度限制            }
[转]浏览器URL长度限制            
else 
[转]浏览器URL长度限制            {
[转]浏览器URL长度限制                dropTradeTerm.options[
0].selected=true;                
[转]浏览器URL长度限制            }
[转]浏览器URL长度限制            
//Ship Way DropDownList
[转]浏览器URL长度限制
            if(arrayValue[5].length>0)
[转]浏览器URL长度限制            {
[转]浏览器URL长度限制                neverSelect 
= true;         
[转]浏览器URL长度限制                
for(i=0;i<dropShipway.options.length;i++)
[转]浏览器URL长度限制                {
[转]浏览器URL长度限制                    
if(dropShipway.options[i].text==arrayValue[5])
[转]浏览器URL长度限制                    {
[转]浏览器URL长度限制                        dropShipway.options[i].selected
=true;   
[转]浏览器URL长度限制                        neverSelect 
= false;                     
[转]浏览器URL长度限制                    }
[转]浏览器URL长度限制                }
[转]浏览器URL长度限制                
if(neverSelect == true
[转]浏览器URL长度限制                {
[转]浏览器URL长度限制                    dropShipway.options[
0].selected=true;       
[转]浏览器URL长度限制                }
[转]浏览器URL长度限制            }
[转]浏览器URL长度限制            
else
[转]浏览器URL长度限制            {
[转]浏览器URL长度限制                dropShipway.options[
0].selected=true;                
[转]浏览器URL长度限制            }
[转]浏览器URL长度限制        }
[转]浏览器URL长度限制        
</script>

最后的URL:

http://hi4-ibmsv503/Pricing/Public/QLookUPSearchPage.aspx?SQL=+WOeqS2Mk/H6E9ICwIOpVbfgAjIPH2onkkRF7fO6kaqZMiq7gRww2LfgAjIPH2onb0G0aFlDKnaVkj5ZYTna9yDKAZJIz8t1GzKRU95Ah7BZ9czLL/8qv94OjoxTQh3qpAIlrIFYcgI/yWSRJXF+z0WkMLZGBidybjtuO86fXa/6zGD5BBrbqWLysS4RIoiQ/Oq2RKy/Ajodoh7HD8ZKV1oFf7uPqCGsYmBs6Ube9i5JGkg0e8sb/nN7x9b9SP8PqbyXWTXVbt98DyOiAYKN7awxVOfHJWy0OvMz9rA7KFv0p59zuOD8FNbB0jGbe/2Pu3D1GXeh6WxVE+6UVgrd8TRHpTYtNEN3KQhuZPWNhUI=&DataSource=9lDQbyO73rk=&ReturnClientID=QLookUpVendorNew&DataSourceType=EPS&WindowTitle=Select Vendor&CustomerFunction=if(clear!=1){objTextBoxText.value=arrayValue[1]!!!lionel!!!' '!!!lionel!!!arrayValue[2];SelectNewVendor(arrayValue);}else {document.getElementById('HiddenVendorIDNew').value='';document.getElementById('QTextBoxVendorTaxNew').value='';document.getElementById('QTextBoxVendorCurrencyNew').value='';document.getElementById('QTextBoxVendorSiteNew').value='';document.getElementById('DropDownListCurrencyNew').options[0].selected=true;document.getElementById('DropDownListTradeTermNew').options[0].selected=true;document.getElementById('DropDownListShipWayNew').options[0].selected=true;}

长度:1128个字符,OK了。

相关文章: