Java URL Read getHttpResponse(HttpURLConnection conn, int expectedReturnCode)

Here you can find the source of getHttpResponse(HttpURLConnection conn, int expectedReturnCode)

Description

get Http Response

License

Apache License

Declaration

private static String getHttpResponse(HttpURLConnection conn, int expectedReturnCode) throws IOException 

Method Source Code


//package com.java2s;
/*//  www .jav a2 s.  c  o  m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF 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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;

public class Main {
    private static String getHttpResponse(HttpURLConnection conn, int expectedReturnCode) throws IOException {
        System.out.println("response code:" + conn.getResponseMessage());
        if (conn.getResponseCode() != expectedReturnCode) {
            System.out.println("Custom Error:" + conn.getResponseCode());
            throw new IOException(conn.getResponseMessage());
        }

        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        rd.close();
        conn.disconnect();
        return sb.toString();
    }
}

Related

  1. getHTML(String pageURL, String encoding)
  2. getHtml(String url)
  3. getHtml(String urlString)
  4. getHTML(String urlToRead)
  5. getHTML(String urlToRead)
  6. getHTTPResponse(String url)
  7. getHttpResponse(String urlStr)
  8. getLastModified(HttpURLConnection conn)
  9. getLastModified(HttpURLConnection connection)