Example usage for org.apache.commons.httpclient HttpMethod getResponseBodyAsStream

List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBodyAsStream

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod getResponseBodyAsStream.

Prototype

public abstract InputStream getResponseBodyAsStream() throws IOException;

Source Link

Usage

From source file:pukiwikiCommunicator.connector.SaveButtonDebugFrame.java

private String getText(HttpMethod method) {
    String pageText = "";
    try {//w w w  . j ava  2  s . c  om
        InputStream is = method.getResponseBodyAsStream();
        InputStreamReader isr = new InputStreamReader(is, this.charset);
        //            InputStreamReader isr=new InputStreamReader(is,"UTF-8");
        //           InputStreamReader isr=new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line = "";
        pageText = "";
        while (true) {
            line = br.readLine();
            pageText = pageText + line + "\n";
            if (line == null)
                break;
            this.messageTextArea.append(line + "\n");
            //              System.out.println(line);
        }
        method.releaseConnection();
    } catch (Exception e) {
    }
    return pageText;
}

From source file:scoap3withapi.Scoap3withAPI.java

public static int getNumRec(String publickey, String privatekey, String date, int jrec, int num_rec) {

    HttpClient client = new HttpClient();
    HttpMethod method = callAPISCOAP3(publickey, privatekey, date, jrec, num_rec);

    double numRec = 0;
    int numFor = 0;
    String responseXML = null;//from w  w w.j  a v a  2s .  co  m
    BufferedReader br = null;
    try {
        client.executeMethod(method);
    } catch (IOException ex) {
        Logger.getLogger(Scoap3withAPI.class.getName()).log(Level.SEVERE, null, ex);
    }

    if (method.getStatusCode() == HttpStatus.SC_OK) {
        try {
            method.getResponseBody();

            responseXML = convertStreamToString(method.getResponseBodyAsStream());

            System.out.println("RESPONSE XML " + responseXML);
            numRec = Double.parseDouble(responseXML.split("Results:")[1].split("-->")[0].replace(" ", ""));

            System.out.println("NUM REC=>" + numRec / 100);

            numFor = (int) Math.ceil(numRec / 100);

            System.out.println("NUM REC=>" + numFor);

            method.releaseConnection();

        } catch (IOException ex) {
            Logger.getLogger(Scoap3withAPI.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    return numFor;
}

From source file:scoap3withapi.Scoap3withAPI.java

public static void writeFilesScoap3(String publickey, String privatekey, String date, int jrec, int num_rec) {
    String responseXML = null;/*from ww w .  j av a 2  s .  co m*/
    HttpClient client = new HttpClient();
    HttpMethod method = callAPISCOAP3(publickey, privatekey, date, jrec, num_rec);

    try {
        client.executeMethod(method);

        if (method.getStatusCode() == HttpStatus.SC_OK) {

            responseXML = convertStreamToString(method.getResponseBodyAsStream());

            FileWriter fw = new FileWriter("MARCXML_SCOAP3_from_" + startDate + "_to_" + todayDate
                    + "/marcXML_scoap3_" + jrec + "_" + num_rec + ".xml");

            fw.append(responseXML);

            fw.close();

        }
    } catch (IOException e) {
        e.printStackTrace();

    } finally {

        method.releaseConnection();

    }

}

From source file:smilehouse.opensyncro.defaultcomponents.http.HTTPUtils.java

/**
 * Makes a HTTP request to the specified url with a set of parameters,
  * request method, user name and password
 * /*  w ww .jav  a 2 s .c o m*/
 * @param url the <code>URL</code> to make a request to 
 * @param method either "GET", "POST", "PUT" or "SOAP"
 * @param parameters two dimensional string array containing parameters to send in the request
 * @param user user name to submit in the request 
 * @param password password to submit in the request
 * @param charsetName charset name used for message content encoding
  * @param responseCharsetName charset name used to decode HTTP responses,
  *                            or null to use default ("ISO-8859-1")
  * @param contentType Content-Type header value (without charset information)
  *                    for POST (and SOAP) type requests. If null, defaults to
  *                    "text/xml".
 * @return a string array containing the body of the response, the headers of
  *         the response and possible error message
 * @throws Exception 
 */
public static HTTPResponse makeRequest(URL url, String method, String[][] parameters, String user,
        String password, String charsetName, String responseCharsetName, String contentType) throws Exception {

    HttpClient httpclient = new HttpClient();

    HttpMethod request_method = null;
    HTTPResponse responseData = new HTTPResponse();
    NameValuePair[] names_values = null;

    String requestContentType;
    if (contentType != null && contentType.length() > 0) {
        requestContentType = contentType;
    } else {
        requestContentType = DEFAULT_CONTENT_TYPE;
    }

    if (parameters != null && method.equals("PUT") == false) {
        names_values = new NameValuePair[parameters.length];

        for (int i = 0; i < parameters.length; i++) {
            names_values[i] = new NameValuePair(parameters[i][0], parameters[i][1]);
        }

    }
    if (method.equalsIgnoreCase("POST")) {
        request_method = new PostMethod(url.toString());
        if (names_values != null)
            ((PostMethod) request_method).setRequestBody(names_values);
    } else if (method.equalsIgnoreCase("PUT")) {
        if (parameters == null)
            throw new Exception("No data to use in PUT request");
        request_method = new PutMethod(url.toString());
        StringRequestEntity sre = new StringRequestEntity(parameters[0][0]);
        ((PutMethod) request_method).setRequestEntity(sre);
    } else if (method.equalsIgnoreCase("SOAP")) {
        String urlString = url.toString() + "?";
        String message = null;
        String action = null;
        for (int i = 0; i < parameters.length; i++) {

            if (parameters[i][0].equals(SOAPMESSAGE))
                message = parameters[i][1];
            else if (parameters[i][0].equals(SOAP_ACTION_HEADER))
                action = parameters[i][1];
            else
                urlString += parameters[i][0] + "=" + parameters[i][1] + "&";
        }
        urlString = urlString.substring(0, urlString.length() - 1);
        request_method = new PostMethod(urlString);
        // Encoding content with requested charset 
        StringRequestEntity sre = new StringRequestEntity(message, requestContentType, charsetName);
        ((PostMethod) request_method).setRequestEntity(sre);
        if (action != null) {
            request_method.setRequestHeader(SOAP_ACTION_HEADER, action);
        }
        // Adding charset also into header's Content-Type
        request_method.addRequestHeader(CONTENT_TYPE_HEADER, requestContentType + "; charset=" + charsetName);
    } else {
        request_method = new GetMethod(url.toString());
        if (names_values != null)
            ((GetMethod) request_method).setQueryString(names_values);
    }

    user = (user == null || user.length() < 1) ? null : user;
    password = (password == null || password.length() < 1) ? null : password;

    if ((user != null & password == null) || (user == null & password != null)) {
        throw new Exception("Invalid username or password");

    }
    if (user != null && password != null) {
        httpclient.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials(user, password);
        httpclient.getState().setCredentials(new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM),
                defaultcreds);
        request_method.setDoAuthentication(true);
    }

    try {
        httpclient.executeMethod(request_method);

        if (request_method.getStatusCode() != HttpStatus.SC_OK) {
            responseData
                    .setResponseError(request_method.getStatusCode() + " " + request_method.getStatusText());
        }

        //Write response header to the out string array
        Header[] headers = request_method.getResponseHeaders();
        responseData.appendToHeaders("\nHTTP status " + request_method.getStatusCode() + "\n");
        for (int i = 0; i < headers.length; i++) {
            responseData.appendToHeaders(headers[i].getName() + ": " + headers[i].getValue() + "\n");
        }

        /*
         * TODO: By default, the response charset should be read from the Content-Type header of 
         * the response and that should be used in the InputStreamReader constructor: 
         * 
         * <code>new InputStreamReader(request_method.getResponseBodyAsStream(), 
         *                   ((HttpMethodBase)request_method).getResponseCharSet());</code>
         * 
         * But for backwards compatibility, the charset used by default is now the one that the 
         * HttpClient library chooses (ISO-8859-1). An alternative charset can be chosen, but in 
         * no situation is the charset read from the response's Content-Type header.
         */
        BufferedReader br = null;
        if (responseCharsetName != null) {
            br = new BufferedReader(
                    new InputStreamReader(request_method.getResponseBodyAsStream(), responseCharsetName));
        } else {
            br = new BufferedReader(new InputStreamReader(request_method.getResponseBodyAsStream()));
        }

        String responseline;
        //Write response body to the out string array
        while ((responseline = br.readLine()) != null) {
            responseData.appendToBody(responseline + "\n");
        }
    } finally {
        request_method.releaseConnection();
    }
    return responseData;
}

From source file:ws.antonov.config.provider.HttpConfigProvider.java

@Override
public Message.Builder retrieveConfigData(Class<? extends Message> configClass,
        ConfigParamsBuilder.ConfigParamsMap configParams) throws IOException {
    try {/*from   w w  w .  jav  a  2s .  c o  m*/
        HttpMethod get = createHttpMethod(computeUrlDestinationFromParams(configParams));
        int result = httpClient.executeMethod(get);
        if (result == 200) {
            ContentType contentType = determineContentType(get);
            return convertMessage(configClass, contentType, get.getResponseBodyAsStream());
        }
        throw new RuntimeException("Did not receive 200-OK response from config provider at " + get.getURI()
                + ", but got " + result + " instead!");
    } catch (Exception e) {
        throw new IOException("Unable to load requested config", e);
    }
}

From source file:wuit.common.crawler.WebSit.CrawlerAPIBaiDu.java

/**
  * HTTP GET?HTML/* w ww. j  av  a2  s  .c o m*/
  *
  * @param url
  *            URL?
  * @param queryString
  *            ?,?null
  * @param charset
  *            
  * @param pretty
  *            ?
  * @return ?HTML
  */
public static String doGet(String url, String queryString, String charset, boolean pretty) {
    StringBuffer response = new StringBuffer();
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(url);
    try {
        if (StringUtils.isNotBlank(queryString))
            // get??http?????%?
            method.setQueryString(URIUtil.encodeQuery(queryString));
        client.executeMethod(method);
        if (method.getStatusCode() == HttpStatus.SC_OK) {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(method.getResponseBodyAsStream(), charset));
            String line;
            while ((line = reader.readLine()) != null) {
                if (pretty)
                    response.append(line).append(System.getProperty("line.separator"));
                else
                    response.append(line);
            }
            reader.close();
        }
    } catch (URIException e) {
        log.error("HTTP Get?" + queryString + "???", e);
    } catch (IOException ex) {
        Logger.getLogger(CrawlerAPIBaiDu.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        method.releaseConnection();
    }
    return response.toString();
}

From source file:xbl.http.Response.java

public Response(HttpMethod response) {
    this.response = response;
    try {//from  w  w w . j  a  va  2s .  co m
        this.document = new Builder(new org.ccil.cowan.tagsoup.Parser())
                .build(response.getResponseBodyAsStream());
    } catch (ParsingException e) {
        throw new SystemException("Unable to parse response from XBox Live", e);
    } catch (IOException e) {
        throw new NetworkException(e);
    }
}