TCP connection not reused for HTTP requests with HttpURLConnection

I created an application that sends GET requests to a URL and then downloads the full content of this page.

The client sends a GET, for example, stackoverflow.com, and redirects the response to the parser, which has the ability to find all the sources from the page that need to be downloaded with subsequent GET requests.

The method below is used to send these GET requests. It is called many times in a row, with URLs returned by the parser. Most of these URLs are located on the same host and must be able to use a TCP connection.

public static void sendGetRequestToSubObject(String RecUrl) { URL url = new URL(recUrl.toString()); URLConnection connection = url.openConnection (); InputStreamReader isr = new InputStreamReader(connection.getInputStream()); } 

Each time this method is called, a new TCP connection is created (using a three-way TCP connection), and then a GET is sent over that connection. But I want to reuse TCP connections to improve performance.

I assume that since I create a new URL object each time the method is called, that way it will work ...

Can someone help me make this better?

Thanks!

+3
source share
2 answers

HttpURLConnection will reuse connections if possible !

To do this, several prerequisites must be met, mainly on the server side. These premises are described in the related article.

+5
source

Found a problem! I did not read the input stream properly. This caused input stream objects to hang and could not be reused.

I just defined it, for example:

 InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 

but I never read from it :-)

I also changed the reading method. Instead of a buffered reader, I stole this:

 InputStream in = null; String queryResult = ""; try { URL url = new URL(archiveQuery); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); HttpURLConnection httpConn = (HttpURLConnection) urlConn; httpConn.setAllowUserInteraction(false); httpConn.connect(); in = httpConn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(in); ByteArrayBuffer baf = new ByteArrayBuffer(50); int read = 0; int bufSize = 512; byte[] buffer = new byte[bufSize]; while(true){ read = bis.read(buffer); if(read==-1){ break; } baf.append(buffer, 0, read); } queryResult = new String(baf.toByteArray()); } catch (MalformedURLException e) { // DEBUG Log.e("DEBUG: ", e.toString()); } catch (IOException e) { // DEBUG Log.e("DEBUG: ", e.toString()); } } 

From here: Reading HttpURLConnection InputStream - manual buffer or BufferedInputStream?

+1
source

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


All Articles