HTC One error? Parts of the HTTP header displayed in the URLConnection InputStream

I have code to download an XML file that has been sent in a paid application for several years - there was never any problem until I recently saw that it happened with HTC One.

I wonder if there is something that I will miss, or if it should be reported somewhere.

Here is sample code that causes the problem:

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 a stream, you get only the contents of the file.

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

 onnection: close Last-Modified: ??? Content-Type: text/html 

This is with Android 4.1.2.

Is there a way to configure URLConnection to avoid this?

+4
source share
1 answer

Wow.

Delving into this problem, we realized that this happened only on certain pages, and confirmed that when this phone goes to any pages served by them in its browser, these HTTP headers still appear in the body causing the errors.

Today I had a short HTTP session and noticed that these headers are considered part of the body!

All line breaks at the HTTP level were \ n \ r or LF CR.

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 

This seems like a big problem - however, the only way the application could exist so far shows the assumption (19.3) that applications MUST carry one LF.

So, I think HTC ignored this offer on HTC One !! :-P

To get around this, I tried using HttpClient (the version included in the Android SDK), but it also had the same problem. I wanted to try using jar files from Apache HTTP Components, but of course they have a package name conflict with the version in the Android platform, so I repackaged them with Jar Jar Links. Once the import has been replaced by the fact that it will work!

+1
source

Source: https://habr.com/ru/post/1484290/


All Articles