Java HTTP Read readData(HttpURLConnection conn)

Here you can find the source of readData(HttpURLConnection conn)

Description

read Data

License

Apache License

Declaration

private static byte[] readData(HttpURLConnection conn) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;
import java.net.HttpURLConnection;

public class Main {
    private static byte[] readData(HttpURLConnection conn) {
        BufferedInputStream in = null;
        try {//from  w w w .j  a v a  2s  .  c  o m
            conn.connect();
            int code = conn.getResponseCode();
            in = new BufferedInputStream(conn.getInputStream());
            int len;
            byte[] buffer = new byte[1024];
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
                out.flush();
            }
            byte[] data = out.toByteArray();
            out.close();
            //            for (int i = 0;; i++) {
            //                String headerName = conn.getHeaderFieldKey(i);
            //                String headerValue = conn.getHeaderField(i);
            //                if (headerName == null && headerValue == null) {
            //                    break;
            //                }
            //                System.out.println(headerName + ":" + headerValue);
            //            }
            return data;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
        return null;
    }
}

Related

  1. getInputStreamReader(String httpUrl, int timeout)
  2. getJsonFromUrlWithJsonParameter(String url, String jsonRequest)
  3. getJsonString(String url)
  4. getReader(String url)
  5. getResult(String urlStr, String content)
  6. readError(HttpURLConnection conn)
  7. readGetEx(String url)
  8. readHTTPConnection(HttpURLConnection conn)
  9. readHttpConnection(HttpURLConnection h)