Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    static byte[] httpRequest(String url, byte[] requestBytes, int connectionTimeOutMs, int readTimeOutMs) {
        byte[] bArr = null;
        if (!(url == null || requestBytes == null)) {
            InputStream inputStream = null;
            HttpURLConnection urlConnection = null;
            try {
                urlConnection = (HttpURLConnection) new URL(url).openConnection();
                urlConnection.setDoOutput(true);
                urlConnection.setConnectTimeout(connectionTimeOutMs);
                urlConnection.setReadTimeout(readTimeOutMs);
                urlConnection.setFixedLengthStreamingMode(requestBytes.length);
                OutputStream outputStream = urlConnection.getOutputStream();
                outputStream.write(requestBytes);
                outputStream.close();
                if (urlConnection.getResponseCode() != 200) {
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                        }
                    }
                    if (urlConnection != null) {
                        urlConnection.disconnect();
                    }
                } else {
                    inputStream = urlConnection.getInputStream();
                    ByteArrayOutputStream result = new ByteArrayOutputStream();
                    byte[] buffer = new byte[16384];
                    while (true) {
                        int chunkSize = inputStream.read(buffer);
                        if (chunkSize < 0) {
                            break;
                        } else if (chunkSize > 0) {
                            result.write(buffer, 0, chunkSize);
                        }
                    }
                    bArr = result.toByteArray();
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e2) {
                        }
                    }
                    if (urlConnection != null) {
                        urlConnection.disconnect();
                    }
                }
            } catch (IOException e3) {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e4) {
                    }
                }
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            } catch (Throwable th) {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e5) {
                    }
                }
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }
        }
        return bArr;
    }
}