Java HTTP Response getResponseStringFromConn(HttpURLConnection conn, boolean isSuccess)

Here you can find the source of getResponseStringFromConn(HttpURLConnection conn, boolean isSuccess)

Description

get Response String From Conn

License

Open Source License

Declaration

public static String getResponseStringFromConn(HttpURLConnection conn, boolean isSuccess) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright ? Microsoft Open Technologies, Inc.
 *
 * All Rights Reserved//w ww.  j  a  va 2s .c om
 *
 * Licensed 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
 *
 * THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
 * ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A
 * PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
 *
 * See the Apache License, Version 2.0 for the specific language
 * governing permissions and limitations under the License.
 ******************************************************************************/

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;

public class Main {
    public static String getResponseStringFromConn(HttpURLConnection conn, boolean isSuccess) throws IOException {

        BufferedReader reader;
        if (isSuccess) {
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        } else {
            reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
        }
        StringBuffer stringBuffer = new StringBuffer();
        String line = "";
        while ((line = reader.readLine()) != null) {
            stringBuffer.append(line);
        }

        return stringBuffer.toString();
    }

    public static String getResponseStringFromConn(HttpURLConnection conn, String payLoad) throws IOException {

        // Send the http message payload to the server.
        if (payLoad != null) {
            conn.setDoOutput(true);
            OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream());
            osw.write(payLoad);
            osw.flush();
            osw.close();
        }

        // Get the message response from the server.
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = "";
        StringBuffer stringBuffer = new StringBuffer();
        while ((line = br.readLine()) != null) {
            stringBuffer.append(line);
        }

        br.close();

        return stringBuffer.toString();
    }
}

Related

  1. getResponseHeader(String headerName, HttpURLConnection urlConnection)
  2. getResponseHeaders(URLConnection conn, HashMap headers)
  3. getResponseMessage(InputStream inputStream, HttpURLConnection connection)
  4. getResponseStream(final HttpURLConnection connection)
  5. getResponseStream(HttpURLConnection conn)
  6. getResponseText(URL constructedUrl, String encoding)
  7. getResposeText(HttpURLConnection connection)
  8. readResponse(HttpURLConnection conn, String encoding)
  9. readResponse(HttpURLConnection connection)