List of usage examples for org.apache.http.entity ContentType create
public static ContentType create(String str, String str2) throws UnsupportedCharsetException
From source file:io.kahu.hawaii.util.call.http.SoapRequest.java
public SoapRequest(RequestDispatcher requestDispatcher, RequestContext<T> context, String url, String content, String soapAction, ResponseHandler<HttpResponse, T> responseHandler, CallLogger<T> logger) { super(requestDispatcher, context, responseHandler, new HttpPost(url), logger); HttpEntity httpEntity = new StringEntity(content, ContentType.create("text/xml", "UTF-8")); getHttpRequest().setHeader("Content-Type", "text/xml;charset=UTF-8"); getHttpRequest().setHeader("SOAPAction", '"' + soapAction + '"'); ((HttpPost) getHttpRequest()).setEntity(httpEntity); }
From source file:lucee.commons.net.http.httpclient.entity.ByteArrayHttpEntity.java
public ByteArrayHttpEntity(byte[] barr, ContentType contentType) { super(barr);// ww w.j av a 2s. co m contentLength = barr == null ? 0 : barr.length; if (ct == null) { Header h = getContentType(); if (h != null) { lucee.commons.lang.mimetype.ContentType tmp = HTTPUtil.toContentType(h.getValue(), null); if (tmp != null) ct = ContentType.create(tmp.getMimeType(), tmp.getCharset()); } } else this.ct = contentType; }
From source file:com.anteam.demo.logback.appender.HttpAppender.java
@Override protected void append(E event) { System.out.println(((LoggingEvent) event).getFormattedMessage()); StringEntity stringEntity = new StringEntity(event.toString(), ContentType.create("text/plain", "UTF-8")); HttpPost post = new HttpPost(url); post.setEntity(stringEntity);/* w ww .j a va2 s. c o m*/ HttpResponse response = null; try { response = httpclient.execute(post); } catch (ClientProtocolException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } // Examine the response status System.out.println(response.getStatusLine()); // Get hold of the response entity HttpEntity entity = response.getEntity(); // If the response does not enclose an entity, there is no need // to worry about connection release if (entity != null) { InputStream instream = null; try { instream = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(instream)); // do something useful with the response System.out.println(reader.readLine()); } catch (Exception ex) { post.abort(); } finally { try { instream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:com.nominanuda.web.http.SerializeDeserializeTest.java
@Test public void testRequest() throws IOException, HttpException { HttpPost req = new HttpPost(REQ_URI); req.addHeader("h1", "v1"); req.setEntity(new StringEntity(PAYLOAD, ContentType.create("text/plain", "UTF-8"))); byte[] serialized = HTTP.serialize(req); //System.err.println(new String(serialized, "UTF-8")); HttpPost m = (HttpPost) HTTP.deserialize(new ByteArrayInputStream(serialized)); //System.err.println(new String(HTTP.serialize(m), "UTF-8")); assertEquals(REQ_URI, m.getRequestLine().getUri()); assertEquals(PAYLOAD.getBytes(HttpProtocol.CS_UTF_8).length, ((ByteArrayEntity) m.getEntity()).getContentLength()); assertEquals("v1", m.getFirstHeader("h1").getValue()); }
From source file:de.intevation.irix.PrintClient.java
/** Obtains a Report from mapfish-print service. * * @param printUrl The url to send the request to. * @param json The json spec for the print request. * @param timeout the timeout for the httpconnection. * * @return byte[] with the report.//from ww w. ja v a 2 s.c o m * * @throws IOException if communication with print service failed. * @throws PrintException if the print job failed. */ public static byte[] getReport(String printUrl, String json, int timeout) throws IOException, PrintException { RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout).build(); CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(config).build(); HttpEntity entity = new StringEntity(json, ContentType.create("application/json", Charset.forName("UTF-8"))); HttpPost post = new HttpPost(printUrl); post.setEntity(entity); CloseableHttpResponse resp = client.execute(post); StatusLine status = resp.getStatusLine(); byte[] retval = null; try { HttpEntity respEnt = resp.getEntity(); InputStream in = respEnt.getContent(); if (in != null) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[BYTE_ARRAY_SIZE]; int r; while ((r = in.read(buf)) >= 0) { out.write(buf, 0, r); } retval = out.toByteArray(); } finally { in.close(); EntityUtils.consume(respEnt); } } } finally { resp.close(); } if (status.getStatusCode() < HttpURLConnection.HTTP_OK || status.getStatusCode() >= HttpURLConnection.HTTP_MULT_CHOICE) { if (retval != null) { throw new PrintException(new String(retval)); } else { throw new PrintException("Communication with print service '" + printUrl + "' failed." + "\nNo response from print service."); } } return retval; }
From source file:juzu.impl.bridge.request.RequestFormContentTypeTestCase.java
@Test public void testWithCharsetOnContentType() throws Exception { HttpClient client = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(applicationURL().toString() + "/foo"); ContentType content = ContentType.create("application/x-www-form-urlencoded", "UTF-8"); HttpEntity entity = new StringEntity("param=" + "test" + EURO, content); httpPost.setEntity(entity);// w w w . j a v a 2s. c om client.execute(httpPost); assertEquals("test" + EURO, param); }
From source file:com.nominanuda.hyperapi.HyperApiWsSkeltonTest.java
@Test public void testFoo() throws Exception { HyperApiWsSkelton skelton = new HyperApiWsSkelton(); skelton.setApi(TestHyperApi.class); skelton.setService(new TestHyperApi() { public DataObject putFoo(String bar, String baz, DataObject foo) { return foo; }//from www . j ava 2 s. co m }); skelton.setRequestUriPrefix("/mytest"); HttpPut request = new HttpPut("/mytest/foo/BAR?baz=BAZ"); DataObject foo = new DataObjectImpl(); foo.put("foo", "FOO"); request.setEntity(new StringEntity(new DataStructHelper().toJsonString(foo), ContentType.create(HttpProtocol.CT_APPLICATION_JSON, HttpProtocol.CS_UTF_8))); HttpResponse response = skelton.handle(request); DataStruct result = new JSONParser().parse(new InputStreamReader(response.getEntity().getContent())); Assert.assertEquals("FOO", ((DataObject) result).get("foo")); }
From source file:de.wdilab.coma.gui.extensions.RemoteRepo.java
public static boolean getRemoteConnection(String s, UUID uid) { UUID uuid;//w w w . ja v a 2 s . co m String xslt_text; boolean is_stored = false; xslt_text = s.replaceAll("\"", "\\\""); xslt_text = xslt_text.replaceAll("\'", "\\\'"); uuid = uid; try { File f = new File("helpingFile.csv"); PrintWriter output = new PrintWriter(new FileWriter(f)); output.println("uuid,data"); xslt_text.trim(); xslt_text = xslt_text.replace("\n", "").replace("\r", ""); output.println(uuid + "," + xslt_text); output.close(); HttpClient client = new DefaultHttpClient(); FileEntity entity = new FileEntity(f, ContentType.create("text/plain", "UTF-8")); HttpPost httppost = new HttpPost(url); httppost.setEntity(entity); HttpResponse response = client.execute(httppost); // System.out.println(response.getStatusLine().getStatusCode()); if (response.getStatusLine().getStatusCode() == 200) is_stored = true; f.delete(); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } // System.out.println("xslt stored "+is_stored); return is_stored; }
From source file:de.hybris.platform.mpintgproductcockpit.productcockpit.util.HttpInvoker.java
public String post(final String reqURL, final String reqStr, final String charset, final int timeout) { // Prepare HTTP post final HttpPost post = new HttpPost(reqURL); post.setHeader("Content-Type", "application/json"); // set content type as json and charset final StringEntity reqEntity = new StringEntity(reqStr, ContentType.create("application/json", charset)); post.setEntity(reqEntity);/* w w w . j a va2 s . c om*/ // Create HTTP client final HttpClient httpClient = new DefaultHttpClient(); // set connection over time httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, new Integer(timeout / 2)); // set data loading over time httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer(timeout / 2)); // Execute request try { final HttpResponse response = httpClient.execute(post); // get response and return as String final HttpEntity responseEntity = response.getEntity(); return EntityUtils.toString(responseEntity); } catch (final Exception e) { return null; } finally { post.releaseConnection(); } }
From source file:juzu.impl.bridge.request.AbstractRequestEntityReader.java
@Test public void testPost() throws Exception { driver.get(applicationURL().toString()); WebElement elt = driver.findElement(By.id("post")); String url = elt.getText();//from ww w . ja va 2 s .c o m data = null; HttpPost post = new HttpPost(url); post.setEntity(new ByteArrayEntity("<foo></foo>".getBytes(Tools.ISO_8859_1), ContentType.create("text/foo", Tools.ISO_8859_1))); HttpClient client = HttpClientBuilder.create().build(); client.execute(post); assertNotNull(data); assertEquals("<foo></foo>", new String(data, Tools.ISO_8859_1)); }