服务器向另一台服务器发出长http请求时出现HTTP 400错误

时间:2012-04-05 19:56:03

标签: http urlconnection http-status-code-400 large-data

我有一个场景,我从HTTP客户端点击了cacheRefresh.do URL。 它到达App Server A,A刷新自己的缓存,然后向App Server B发送请求(我正在使用URLConnection),以刷新其缓存。 (我知道这是一个糟糕的设计,但我们没有选择)

现在当我的请求刷新小缓存(响应时间很短)时,一切看起来都很好,我得到了200。

但是当我的请求是刷新大缓存时,我得到400.

服务器A和B也会在这种情况下刷新,但为什么我会得到400回复作为响应?有什么想法吗?

以下是控制器代码:

@SuppressWarnings("unused")
public ModelAndView handleRequest(final HttpServletRequest request, final HttpServletResponse response)
        throws Exception {

    final long cacheRefreshStartTime = System.currentTimeMillis();

    final String action = request.getParameter("action");
    // Init to 74 since this is the static length that will be appended.
    final StringBuffer result = new StringBuffer(74);
    final String[] cacheKeys = request.getParameterValues("cacheKeys");
    String[] cacheElement = request.getParameterValues("cacheElement");
    final String refreshByKeyRegion = request.getParameter("refreshByKeyRegion");
    final String refreshByKeyRegionKeys = request.getParameter("refreshByKeyRegionKeys");
    final String refreshPartnerInstanceCache = request.getParameter("refreshPartnerInstanceCache");
    LOG.debug(" cacheKeys for refresh " + Arrays.toString(cacheKeys));

    try {
        if (action.equalsIgnoreCase("ALL")) {
            performancLogger.info("Cache Refresh requested action=" + action);
            this.refreshAllCache();
        } else if (action.equalsIgnoreCase("SPECIFIC")) {
            performancLogger.info("Cache Refresh requested action=" + action + " keys="
                    + Arrays.toString(cacheKeys));
            this.refreshSpecificCache(cacheKeys);
        } else if (action.equalsIgnoreCase("cacheElement")) {
            if (refreshByKeyRegion != null && refreshByKeyRegion.length() > 0 && refreshByKeyRegionKeys != null
                    && refreshByKeyRegionKeys.length() > 0) {
                cacheElement = new String[] { refreshByKeyRegion + "," + refreshByKeyRegionKeys };
            }
            performancLogger.info("Cache Refresh requested action=" + action + " element="
                    + Arrays.toString(cacheElement));
            this.refreshCacheElements(cacheElement);
        }
        if (!request.getServerName().contains("localhost") && refreshPartnerInstanceCache != null
                && refreshPartnerInstanceCache.equals("true")) {
            refreshPartnerInstanceCache(request);
        }
        result.append("Cache refresh completed successfully.");

        if (cacheKeys != null) {
            result.append(" Keys - ");
            result.append(this.formatArrayAsString(cacheKeys));
        }
    } catch (final Exception e) {
        result.append("Cache refresh failed.");
        if (cacheKeys != null) {
            result.append(" Keys - ");
            result.append(this.formatArrayAsString(cacheKeys));
        }
    }

    if (action != null) {
        performancLogger.info("Cache Refresh competed total refresh time = "
                + formatElapsedTime(System.currentTimeMillis() - cacheRefreshStartTime));
    }

    return new ModelAndView(IVRControllerNames.CACHE_REFRESH_STANDARD_VIEW, "displayInfo", this
            .getDisplayInfo(result));
}

请求标题:

POST xxxxx.do HTTP/1.1
Host: xxxxx
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://xxx/yyy/zzz/cacheView.do
Cookie: JSESSIONID=xxxxx.x.x

请求正文:

Content-Type: application/x-www-form-urlencoded 
Content-Length: 134 
action=SPECIFIC&refreshPartnerInstanceCache=true&cacheKeys=xxxx&cacheKeys=xxx&Refresh=yyyy

谢谢!

1 个答案:

答案 0 :(得分:0)

在你的行动中,你似乎有几个同名的钥匙。

action=SPECIFIC&refreshPartnerInstanceCache=true&**cacheKeys**=xxxx&**cacheKeys**=xxx&Refresh=yyyy

清理它并为它们提供唯一的ID。

此外,它们似乎不是urlencoded,如果您的密钥包含有趣的字符可能会导致麻烦。见How to escape URL-encoded data in POST with HttpWebRequest