Java HTTP Response readResponse(HttpURLConnection httpURLConnection)

Here you can find the source of readResponse(HttpURLConnection httpURLConnection)

Description

read Response

License

Open Source License

Declaration

private static String readResponse(HttpURLConnection httpURLConnection) throws IOException 

Method Source Code


//package com.java2s;
/*/*ww  w  .j a v a2 s  . c  o m*/
 *?Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *WSO2 Inc. licenses this file to you under the Apache License,
 *Version 2.0 (the "License"); you may not use this file except
 *in compliance with the License.
 *You may obtain a copy of the License at
 *
 *http://www.apache.org/licenses/LICENSE-2.0
 *
 *Unless required by applicable law or agreed to in writing,
 *software distributed under the License is distributed on an
 *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *KIND, either express or implied.  See the License for the
 *specific language governing permissions and limitations
 *under the License.
 */

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

public class Main {
    private static String readResponse(HttpURLConnection httpURLConnection) throws IOException {
        InputStream inputStream = null;
        BufferedReader bufferedReader = null;
        try {
            StringBuilder stringBuilder = new StringBuilder();
            int responseCode = httpURLConnection.getResponseCode();
            if (responseCode / 100 == 2) { //For 20X response codes.
                inputStream = httpURLConnection.getInputStream();
            } else {
                inputStream = httpURLConnection.getErrorStream();
            }
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
            return stringBuilder.toString();
        } finally {
            if (bufferedReader != null) {
                bufferedReader.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }
}

Related

  1. getResponseStringFromConn(HttpURLConnection conn, boolean isSuccess)
  2. getResponseText(URL constructedUrl, String encoding)
  3. getResposeText(HttpURLConnection connection)
  4. readResponse(HttpURLConnection conn, String encoding)
  5. readResponse(HttpURLConnection connection)
  6. readResponse(URLConnection conn)