Here you can find the source of query(URL host, String endpoint, String customer, String name, OutputStream out)
static int query(URL host, String endpoint, String customer, String name, OutputStream out) throws IOException
//package com.java2s; /*//from w w w.ja v a 2 s.co 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.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class Main { private static final int COPY_BUFFER_SIZE = 4096; static int query(URL host, String endpoint, String customer, String name, OutputStream out) throws IOException { String f1 = (customer == null) ? null : "customer=" + customer; String f2 = (name == null) ? null : "name=" + name; String filter = ((f1 == null) ? "?" : "?" + f1 + "&") + ((f2 == null) ? "" : f2); URL url = new URL(host, endpoint + filter); int responseCode; HttpURLConnection connection = null; InputStream input = null; try { connection = (HttpURLConnection) url.openConnection(); responseCode = connection.getResponseCode(); input = connection.getInputStream(); copy(input, out); out.flush(); } catch (IOException e) { responseCode = handleIOException(connection); } finally { closeSilently(input); closeSilently(out); closeSilently(connection); } return responseCode; } static void copy(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[COPY_BUFFER_SIZE]; int bytes = in.read(buffer); while (bytes != -1) { out.write(buffer, 0, bytes); bytes = in.read(buffer); } } /** * @see http://docs.oracle.com/javase/6/docs/technotes/guides/net/http-keepalive.html */ static int handleIOException(HttpURLConnection conn) { int respCode = -1; try { respCode = conn.getResponseCode(); flushStream(conn.getErrorStream()); } catch (IOException ex) { // deal with the exception } return respCode; } static void closeSilently(Closeable resource) { if (resource != null) { try { resource.close(); } catch (IOException exception) { // Ignore... } } } static void closeSilently(HttpURLConnection resource) { if (resource != null) { resource.disconnect(); } } static void flushStream(InputStream is) { byte[] buf = new byte[COPY_BUFFER_SIZE]; try { while (is.read(buf) > 0) { // Ignore... } } catch (IOException ex) { // deal with the exception } finally { closeSilently(is); } } }