Example usage for org.apache.http.util EntityUtils toByteArray

List of usage examples for org.apache.http.util EntityUtils toByteArray

Introduction

In this page you can find the example usage for org.apache.http.util EntityUtils toByteArray.

Prototype

public static byte[] toByteArray(HttpEntity httpEntity) throws IOException 

Source Link

Usage

From source file:de.taimos.httputils.WS.java

/**
 * @param response the {@link HttpResponse}
 * @return String the body as UTF-8 string
 *///from   w  ww. ja  v  a2 s  . c o  m
public static byte[] getResponseAsBytes(final HttpResponse response) {
    try {
        return EntityUtils.toByteArray(response.getEntity());
    } catch (final ParseException e) {
        throw new RuntimeException(e);
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static byte[] doPostSubmit(String url, Map<String, Object> params) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost();
    try {/*from   w  w  w. j  a  v  a  2s.c  o  m*/

        List<NameValuePair> list = new ArrayList<NameValuePair>();

        for (Entry<String, Object> entry : params.entrySet()) {
            NameValuePair nameValuePair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
            list.add(nameValuePair);
        }
        httpPost.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
        HttpResponse response = httpClient.execute(httpPost);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            return EntityUtils.toByteArray(entity);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static final String postData(String url, HashMap<String, String> params) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    String outputString = null;/*from   w w  w . j a  va  2 s .  c om*/

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.size());
        Iterator it = params.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            nameValuePairs.add(new BasicNameValuePair((String) pair.getKey(), (String) pair.getValue()));
            System.out.println(pair.getKey() + " = " + pair.getValue());
            it.remove(); // avoids a ConcurrentModificationException
        }
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpURLConnection.HTTP_OK) {
            byte[] result = EntityUtils.toByteArray(response.getEntity());
            outputString = new String(result, "UTF-8");
        }

    } catch (ClientProtocolException e) {
        Log.i(CLASS_NAME, "Client protocolException happened: " + e.getMessage());
    } catch (IOException e) {
        Log.i(CLASS_NAME, "Client IOException happened: " + e.getMessage());
    } catch (NetworkOnMainThreadException e) {
        Log.i(CLASS_NAME, "Client NetworkOnMainThreadException happened: " + e.getMessage());
        e.printStackTrace();
    } catch (Exception e) {
        Log.i(CLASS_NAME, "Unknown exeception: " + e.getMessage());
    }
    return outputString;
}

From source file:com.zyf.bike.suzhou.utils.HttpOperation.java

/**
 * Http Get??//from   w  ww .j a  v a  2  s  .  co  m
 * @param url
 * @param testnum ?
 * @return
 */
public static byte[] doGetBase(String url, int testnum) {
    if (testnum < 0) {
        return null;
    }
    try {
        //HttpClient    
        HttpClient httpclient = new DefaultHttpClient();
        //POST  
        HttpGet get = new HttpGet(url);
        HttpResponse response = httpclient.execute(get);
        int code = response.getStatusLine().getStatusCode();
        if (code == HttpStatus.SC_OK) {
            //?
            return EntityUtils.toByteArray(response.getEntity());
        } else if (code == HttpStatus.SC_NOT_FOUND || code == HttpStatus.SC_FORBIDDEN
                || code == HttpStatus.SC_METHOD_NOT_ALLOWED) {
            //403 ?
            //404 ??
            //405 ??
            return null;
        } else {
            //??
            doGetBase(url, testnum--);
        }
    } catch (Exception e) {
        Logger.e(TAG, e.getMessage(), e);
    }
    return null;
}

From source file:de.escidoc.core.test.EntityUtil.java

public static byte[] toByteArray(HttpEntity entity) throws IOException {
    if (entity != null) {
        return EntityUtils.toByteArray(entity);
    } else {//ww  w.  ja  va 2  s.  c  om
        return new byte[0];
    }
}

From source file:code.google.restclient.core.ByteArrayResponseHandler.java

public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    statusLine = response.getStatusLine();
    statusCode = statusLine.getStatusCode();
    reasonPhrase = statusLine.getReasonPhrase();
    protocolVersion = statusLine.getProtocolVersion();
    allHeaders = response.getAllHeaders();

    HttpEntity respEntity = response.getEntity();
    try {//from w  ww .  jav  a 2s.  c  o  m
        if (respEntity != null)
            return EntityUtils.toByteArray(respEntity);
        else
            return null;
    } finally {
        respEntity.consumeContent();
    }
}

From source file:com.helger.httpclient.response.ResponseHandlerByteArray.java

@Nullable
public byte[] handleResponse(final HttpResponse aHttpResponse) throws IOException {
    final HttpEntity aEntity = ResponseHandlerHttpEntity.INSTANCE.handleResponse(aHttpResponse);
    if (aEntity == null)
        return null;
    return EntityUtils.toByteArray(aEntity);
}

From source file:com.commonsware.android.tuning.downloader.ByteArrayResponseHandler.java

public byte[] handleResponse(final HttpResponse response) throws IOException, HttpResponseException {
    StatusLine statusLine = response.getStatusLine();

    if (statusLine.getStatusCode() >= 300) {
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }// w ww  . j  a  va  2  s  .  co m

    HttpEntity entity = response.getEntity();

    if (entity == null) {
        return (null);
    }

    return (EntityUtils.toByteArray(entity));
}

From source file:cn.edu.szjm.support.http.IgnitedHttpResponseImpl.java

public byte[] getResponseBodyAsBytes() throws IOException {
    return EntityUtils.toByteArray(entity);
}

From source file:org.angellist.angellistmobile.ApiCalls.java

private static String GetData(String url) {
    byte[] result = null;
    String str = "";
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(url);
    try {//from  www.java2 s  .  co m

        HttpResponse response = client.execute(get);
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpURLConnection.HTTP_OK) {
            result = EntityUtils.toByteArray(response.getEntity());
            str = new String(result, "UTF-8");
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return str;

}