Wednesday, February 22, 2017

How read HTTP response through sockets?

Leave a Comment

I'm trying create http proxy server on android device. When i trying read response from HTTP server (example1.com) ( example1.com contains content-length in header) If HTTP server contains content-length, then i'm read bytes from content-length else i'm read all bytes of response

byte[] bytes = IOUtils.toByteArray(inFromServer);

The problem is that, when response contains content-length the response reads quickly. If response not contains content-length the response read slowly.

this my code

DataInputStream in = new DataInputStream(inFromServer);         //BufferedReader br = new BufferedReader(new InputStreamReader(in));         String line = "";         String str = "";         Integer len = 0;         while(true) {             line = in.readLine();             if  (line.indexOf("Content-Length") != -1)             {                 len = Integer.parseInt( line.split("\\D+")[1] );                 //System.out.println("LINEE="+len);             }              out.println(line);             str = str + line + '\n';             if(line.isEmpty())  break;         }         int i = Integer.valueOf(len);         String body= "";         System.out.println("i="+i);         if (i>0) {             byte[] buf = new byte[i];             in.readFully(buf);             out.write(buf);             for (byte b:buf) {                 body = body + (char)b;             }          }else{              byte[] bytes = IOUtils.toByteArray(inFromServer);             out.write(bytes);         } 

out - outStream to browser

2 Answers

Answers 1

Try the following code:

// Get server response int responseCode = connection.getResponseCode(); if (responseCode == 200) {     BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));     String line;     StringBuilder builder = new StringBuilder();     while ((line = reader.readLine()) != null) {         builder.append(line);     }     String response = builder.toString()     // Handle response... } 

Answers 2

Firstly, you should know what http protocal and how it work.

and every http Request like:

  • Request Line
  • Request Header
  • Blank Line
  • Request Body.

and every http Response like:

  • Status Line
  • Response Header
  • Blank Line
  • Response Body.

However we can read the InputStream from http server, we can split every line we read from inputstream end with '/r/n'.

and your code:

while(true) {     **line = in.readLine();**     if  (line.indexOf("Content-Length") != -1)     {         len = Integer.parseInt( line.split("\\D+")[1] );             //System.out.println("LINEE="+len);         }          out.println(line);         str = str + line + '\n';         if(line.isEmpty())  break;     } } 

and in.readLine() return the every line not end with '/r/n', it return the line end with '/r' or '/n' . Maybe read from the inputstream block in here.

Here is a IOStreamUtils to read line end with '/r/n'.

https://github.com/mayubao/KuaiChuan/blob/master/app/src/main/java/io/github/mayubao/kuaichuan/micro_server/IOStreamUtils.java

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment