List of usage examples for org.apache.http.util EntityUtils toByteArray
public static byte[] toByteArray(HttpEntity httpEntity) throws IOException
From source file:cn.jumper.study.http.ClientFormLogin.java
public static void main(String[] args) throws Exception { BasicCookieStore cookieStore = new BasicCookieStore(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); HttpHost proxy = new HttpHost("192.168.10.3", 8080, "http"); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); try {//from w w w .ja va2 s. c o m HttpGet httpget = new HttpGet("http://www.ksf-food.com/admin/Login.asp"); httpget.setConfig(config); CloseableHttpResponse response1 = httpclient.execute(httpget); try { HttpEntity entity = response1.getEntity(); System.out.println("Login form get: " + response1.getStatusLine()); EntityUtils.consume(entity); System.out.println("Initial set of cookies:"); List<Cookie> cookies = cookieStore.getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } } finally { response1.close(); } String code = ""; try { HttpUriRequest httpgetCode = RequestBuilder.get() .setUri("http://www.ksf-food.com/admin/inc/checkcode.asp").setConfig(config).build(); /* * HttpGet httpgetCode = new HttpGet( * "http://www.qufuev.com/admin/inc/checkcode.asp"); * httpgetCode.setConfig(config); */ System.out.println("Executing request " + httpgetCode.getRequestLine()); System.out.println("========================================================"); System.out.println("==httpget header =="); for (Header header : httpgetCode.getAllHeaders()) { System.out.println(header.getName() + ":" + header.getValue()); } System.out.println("==httpget header =="); ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); System.out.println("==respons header =="); for (Header header : response.getAllHeaders()) { System.out.println(header.getName() + ":" + header.getValue()); } System.out.println("==respons header =="); String fileName = System.currentTimeMillis() + ""; DataOutputStream dataOutputStream = new DataOutputStream( new FileOutputStream("d://test//e3//" + fileName + ".jpg")); dataOutputStream.write(EntityUtils.toByteArray(entity)); dataOutputStream.close(); return ImageTest.getAllOcr("d://test//e3//" + fileName + ".jpg"); } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; code = httpclient.execute(httpgetCode, responseHandler); System.out.println("ClientFormLogin.main()-CheckCode:" + code); System.out.println("----------------------------------------"); } catch (IOException e) { e.printStackTrace(); } HttpUriRequest login = RequestBuilder.post() .setUri(new URI("http://www.ksf-food.com/admin/Admin_ChkLogin.asp")) .addParameter("UserName", "username").addParameter("Password", "password") .addParameter("CheckCode", code).setConfig(config).build(); System.out.println("========================================================"); System.out.println("==httpget header =="); for (Header header : login.getAllHeaders()) { System.out.println(header.getName() + ":" + header.getValue()); } CloseableHttpResponse response2 = httpclient.execute(login); try { HttpEntity entity = response2.getEntity(); System.out.println("Login form post: " + response2.getStatusLine()); // EntityUtils.consume(entity); System.out.println("ClientFormLogin.main():\\n" + EntityUtils.toString(entity, "GBK")); System.out.println("Post logon cookies:"); List<Cookie> cookies = cookieStore.getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } } finally { response2.close(); } } finally { httpclient.close(); } }
From source file:cache.reverseproxy.CachedResponseParser.java
public static void main(String[] args) throws Exception { final HttpResponse httpresponse = getResponseFromParsedFile("address-validation-response.txt", false); System.out.println("PARSED RESPONSE ========>"); final StatusLine statusline = httpresponse.getStatusLine(); System.out.println(statusline.toString()); final Header[] headers = httpresponse.getAllHeaders(); for (Header header : headers) { System.out.println(header); }//from w w w. j av a2s . co m HttpEntity entity = httpresponse.getEntity(); if (entity != null) { byte[] entityContent = EntityUtils.toByteArray(entity); System.out.println(new String(entityContent)); } }
From source file:Main.java
public static byte[] loadByteFromURL(String url) { HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse response;/*from ww w . j a v a 2s . c o m*/ try { response = httpClient.execute(httpGet); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity httpEntity = response.getEntity(); return EntityUtils.toByteArray(httpEntity); } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
private static String reverseGeocode(String url) { try {//from ww w.j a v a2s .c o m HttpGet httpGet = new HttpGet(url); BasicHttpParams httpParams = new BasicHttpParams(); DefaultHttpClient defaultHttpClient = new DefaultHttpClient(httpParams); HttpResponse httpResponse = defaultHttpClient.execute(httpGet); int responseCode = httpResponse.getStatusLine().getStatusCode(); if (responseCode == HttpStatus.SC_OK) { String charSet = EntityUtils.getContentCharSet(httpResponse.getEntity()); if (null == charSet) { charSet = "UTF-8"; } String str = new String(EntityUtils.toByteArray(httpResponse.getEntity()), charSet); defaultHttpClient.getConnectionManager().shutdown(); return str; } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String reverseGeocode(String url) { try {/*from w w w .j a v a 2 s . c om*/ HttpGet httpGet = new HttpGet(url); BasicHttpParams httpParams = new BasicHttpParams(); DefaultHttpClient defaultHttpClient = new DefaultHttpClient(httpParams); HttpResponse httpResponse = defaultHttpClient.execute(httpGet); int responseCode = httpResponse.getStatusLine().getStatusCode(); if (responseCode == HttpStatus.SC_OK) { String charSet = EntityUtils.getContentCharSet(httpResponse.getEntity()); if (null == charSet) { charSet = "UTF-8"; } String str = new String(EntityUtils.toByteArray(httpResponse.getEntity()), charSet); defaultHttpClient.getConnectionManager().shutdown(); Log.i("-----------", "str = " + str); return str; } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static byte[] doPostSubmit(String url, List<NameValuePair> params) { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(); try {// ww w .j av a2 s. com httpPost.setEntity(new UrlEncodedFormEntity(params, "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:com.github.tomakehurst.wiremock.common.HttpClientUtils.java
public static byte[] getEntityAsByteArrayAndCloseStream(HttpResponse httpResponse) { HttpEntity entity = httpResponse.getEntity(); if (entity != null) { try {//from w w w . j av a 2 s . c o m byte[] content = EntityUtils.toByteArray(entity); entity.getContent().close(); return content; } catch (IOException ioe) { throw new RuntimeException(ioe); } } return null; }
From source file:org.springframework.cloud.netflix.ribbon.apache.HttpClientUtils.java
/** * Creates an new {@link HttpEntity} by copying the {@link HttpEntity} from the {@link HttpResponse}. * This method will close the response after copying the entity. * @param response The response to create the {@link HttpEntity} from * @return A new {@link HttpEntity}/*from www . ja va 2 s . c o m*/ * @throws IOException thrown if there is a problem closing the response. */ public static HttpEntity createEntity(HttpResponse response) throws IOException { ByteArrayInputStream is = new ByteArrayInputStream(EntityUtils.toByteArray(response.getEntity())); BasicHttpEntity entity = new BasicHttpEntity(); entity.setContent(is); entity.setContentLength(response.getEntity().getContentLength()); if (CloseableHttpResponse.class.isInstance(response)) { ((CloseableHttpResponse) response).close(); } return entity; }
From source file:com.gozap.chouti.service.HttpService.java
/** * /*from w w w .j a v a2 s. c o m*/ * @param uri uri * @return * @author saint * @date 2013-4-17 */ public static byte[] get(String uri) { HttpClient httpClient = HttpClientFactory.getInstance(); HttpGet httpGet = new HttpGet(uri); try { HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); byte[] bytes = EntityUtils.toByteArray(httpEntity); return bytes; } catch (ClientProtocolException e) { LOGGER.error("get" + uri + "error", e); } catch (IOException e) { LOGGER.error("get" + uri + "error", e); } finally { httpGet.releaseConnection(); } return null; }
From source file:org.elasticsearch.test.rest.yaml.ObjectPath.java
public static ObjectPath createFromResponse(Response response) throws IOException { byte[] bytes = EntityUtils.toByteArray(response.getEntity()); String contentType = response.getHeader("Content-Type"); XContentType xContentType = XContentType.fromMediaTypeOrFormat(contentType); return ObjectPath.createFromXContent(xContentType.xContent(), new BytesArray(bytes)); }