HTC一个错误? HTTP标头的部分出现在URLConnection InputStream中 [英] HTC One bug? Parts of HTTP header appearing in URLConnection InputStream

查看:208
本文介绍了HTC一个错误? HTTP标头的部分出现在URLConnection InputStream中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以下载已经在付费应用程序中运行了几年的XML文件-直到我最近看到HTC One发生这种情况之前,它再也没有遇到任何麻烦.

I have some code to download an XML file that has been shipping in a paid app for a few years - never had any trouble until I recently saw this happen with an HTC One.

想知道我是否缺少某些东西,或者是否应该在某处得到报告.

Wondering if there is something I am missing or if this should get reported somewhere.

以下是强制执行此问题的示例代码:

Here is example code that forces the issue:

package com.lutron.davetest;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView mainCenterText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainCenterText = (TextView) findViewById(R.id.mainCenterText);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Enter the IP address");
        final EditText edit = new EditText(this);
        builder.setView(edit);
        builder.setPositiveButton("OK", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mainCenterText.setText("");
                String ip = edit.getText().toString();
                new DownloadTask().execute(ip);
            }
        });
        builder.create().show();
    }

    private class DownloadTask extends AsyncTask<String, Void, Void>{

        @Override
        protected Void doInBackground(String... params) {
            try{
                URL url = new URL("http", params[0], 80, "/file.xml");
                URLConnection connection = url.openConnection();
                InputStream in = new BufferedInputStream(connection.getInputStream());
                byte buf[] = new byte[4096];
                int len;
                while((len = in.read(buf)) != -1 ) {
                    final String data = new String(buf, 0, len);
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            mainCenterText.append(data);
                        }
                    });
                }

            }catch(Exception e){
                e.printStackTrace();
            }
            return null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

发生的情况是,在其他所有设备上,从流中读取内容时,您只会得到文件内容.

What happens is that on every other device when reading from the stream you only get the file contents.

在HTC One上,您将获得THIS,然后得到文件内容:

On the HTC One you get THIS and then the file contents:

onnection: close

Last-Modified: ???

Content-Type: text/html

这是Android 4.1.2.上的版本.

This is with Android 4.1.2.

是否有某种方法可以配置URLConnection来避免这种情况?

Is there some way to configure the URLConnection to avoid this?

推荐答案

哇.

深入研究问题,我们意识到这仅发生在特定的页面上,并验证了当手机在其浏览器中导航到由其服务的任何页面时,这些HTTP标头仍会出现在正文中,从而导致错误.

Digging into the issue, we realized this only occurred on specific pages, and verified that when this phone navigates to any pages served by them in its browser, these HTTP headers still appear in the body causing errors.

我今天在一个小型HTTP会话中进行了有线通讯,发现这些标头被认为是正文的一部分!

I wiresharked a small HTTP session today and noticed that these headers are being considered part of the body!

HTTP层中的所有换行符都是\ n \ r或LF CR.

All the line breaks in the HTTP layer were \n\r, or LF CR.

从HTTP 1.1 RFC 2616开始: http://www.ietf.org/rfc/rfc2616. txt

From HTTP 1.1 RFC 2616: http://www.ietf.org/rfc/rfc2616.txt

HTTP/1.1 defines the sequence CR LF as the end-of-line marker for all
protocol elements except the entity-body (see appendix 19.3 for
tolerant applications). The end-of-line marker within an entity-body
is defined by its associated media type, as described in section 3.7.

   CRLF           = CR LF

...

    […] a bare CR
or LF MUST NOT be substituted for CRLF within any of the HTTP control
structures (such as header fields and multipart boundaries).

...

6 Response

After receiving and interpreting a request message, a server responds
with an HTTP response message.

   Response      = Status-Line               ; Section 6.1
                   *(( general-header        ; Section 4.5
                    | response-header        ; Section 6.2
                    | entity-header ) CRLF)  ; Section 7.1
                   CRLF
                   [ message-body ]          ; Section 7.2

这似乎是一个大问题-但是,应用程序可能一直持续到现在的唯一方式似乎是建议(19.3),即应用程序应该容忍单个LF.

This seems like a big issue - however, the only way the app could have lasted until now appears the suggestion (19.3) that applications SHOULD tolerate a single LF.

所以我想HTC忽略了有关HTC One的建议! :-P

So I guess HTC ignored this suggestion on the HTC One!! :-P

要解决此问题,我尝试使用HttpClient(Android SDK附带的版本),但这也有相同的问题.我想尝试使用来自Apache HTTP Components的jar文件,但是当然这些文件的包名称与Android框架中的版本冲突,因此我使用Jar Jar Links重新打包了它们.一旦将进口商品替换为进口商品,就可以使用!

To work around this, I tried to use HttpClient (version included with Android SDK), but that also had the same problem. I wanted to try using the jar files from Apache HTTP Components, but of course those have a package name conflict with the version in the Android framework, so I repackaged them with Jar Jar Links. Once the imports were replaced with that it worked!

这篇关于HTC一个错误? HTTP标头的部分出现在URLConnection InputStream中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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