HttpURLConnection ifModifiedSince生成UTC时间而不是GMT

时间:2013-01-22 09:09:40

标签: java android

我有以下方法:

public void GetBackground(final int width, final int height, final Long ifModifiedSince,
        final GetBackgroundListener listener)
{
    new Thread(new Runnable()
    {
        public void run()
        {
            try {
                String urlString = serviceUri + "/DownloadBackground?width=" + width
                        + "&height=" + height;
                URL url = new URL(urlString);
                HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                if (ifModifiedSince != null) {
                    Log.d(TAG, "Background last modified: " + ifModifiedSince);
                    connection.setIfModifiedSince(ifModifiedSince);
                }
                InputStream stream = (InputStream)connection.getContent();
                if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
                    listener.onNotModified();
                    Log.d(TAG, "Background not modified");
                } else {
                    Bitmap background = BitmapFactory.decodeStream(stream);
                    listener.onSuccess(background, connection.getLastModified());
                    Log.d(TAG, "New background");
                }
            } catch (Exception e) {
                listener.onError(e);
            }
        }
    }).start();
}

在Android 2.2上,它会生成以下包:

GET /QurrentService.svc/DownloadBackground?width=480&height=480 HTTP/1.1
if-modified-since: Thu, 13 Dec 2012 09:11:23 GMT
User-Agent: Dalvik/1.2.0 (Linux; U; Android 2.2; sdk Build/FRF91)
Host: qurrentfeed1.1.aliencms.nl
Connection: Keep-Alive

在Android 4.1上:

GET /QurrentService.svc/DownloadBackground?width=800&height=800 HTTP/1.1
User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.1.2; sdk Build/MASTER)
Host: qurrentfeed1.1.aliencms.nl
Connection: Keep-Alive
Accept-Encoding: gzip
If-Modified-Since: Thu, 13 Dec 2012 09:11:23 UTC

我不确定是否允许将UTC修改后的日期作为UTC时间发送,但服务器无法解析日期。 有没有办法强制HttpURLConnection生成GMT格式?或者我应该像这样手动设置标题?

String ifModifiedSinceString = DateUtils.formatDate(new Date(ifModifiedSince), "EEE, d MMM yyyy HH:mm:ss 'GMT'");
connection.addRequestProperty("If-Modified-Since", ifModifiedSinceString);

0 个答案:

没有答案