List of usage examples for org.apache.http.entity ContentType create
private static ContentType create(HeaderElement headerElement)
From source file:org.safegees.safegees.util.HttpUrlConnection.java
public static String performPostFileCall(String requestURL, String userCredentials, File file) { String response = null;//ww w. ja v a2 s. co m MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create(); reqEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); //reqEntity.addPart("avatar", new FileBody(file, ContentType.MULTIPART_FORM_DATA)); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(requestURL); httppost.addHeader(KEY_HEADER_AUTHORIZED, userCredentials); httppost.addHeader("ContentType", "image/png"); httppost.addHeader("Referer", "https://safegees.appspot.com/v1/user/image/upload/"); httppost.addHeader("Origin", "https://safegees.appspot.com"); httppost.addHeader("Upgrade-Insecure-Requests", "1"); httppost.addHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"); reqEntity.addBinaryBody("avatar", file, ContentType.create("image/png"), file.getName()); httppost.setEntity(reqEntity.build()); HttpResponse httpResponse = null; try { httpResponse = httpclient.execute(httppost); Log.e("IMAGE", httpResponse.getStatusLine().getStatusCode() + ":" + httpResponse.getStatusLine().getReasonPhrase()); //response = EntityUtils.toString(httpResponse.getEntity()); response = httpResponse.getStatusLine().getReasonPhrase(); } catch (IOException e) { e.printStackTrace(); } if (httpResponse.getStatusLine().getStatusCode() == 200) return response; return null; }
From source file:org.n52.sir.client.Client.java
/** * @param request/* w w w . j a v a 2s . co m*/ * @return * @throws UnsupportedEncodingException * @throws IOException * @throws HttpException * @throws OwsExceptionReport */ private XmlObject doSend(String request, String requestMethod, URI uri) throws UnsupportedEncodingException, IOException, HttpException, OwsExceptionReport { if (log.isDebugEnabled()) log.debug("Sending request (first 100 characters): " + request.substring(0, Math.min(request.length(), 100))); HttpClient client = new DefaultHttpClient(); // configure timeout to handle really slow servers HttpParams params = client.getParams(); HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(params, CONNECTION_TIMEOUT); HttpRequestBase method = null; if (requestMethod.equals(GET_METHOD)) { log.debug("Client connecting via GET to '{}' with request '{}'", uri, request); String fullUri = null; if (request == null || request.isEmpty()) fullUri = uri.toString(); else fullUri = uri.toString() + "?" + request; log.debug("GET call: {}", fullUri); HttpGet get = new HttpGet(fullUri); method = get; } else if (requestMethod.equals(POST_METHOD)) { if (log.isDebugEnabled()) log.debug("Client connecting via POST to " + uri); HttpPost postMethod = new HttpPost(uri.toString()); postMethod.setEntity(new StringEntity(request, ContentType.create(SirConstants.REQUEST_CONTENT_TYPE))); method = postMethod; } else { throw new IllegalArgumentException("requestMethod not supported!"); } try { HttpResponse httpResponse = client.execute(method); try (InputStream is = httpResponse.getEntity().getContent();) { XmlObject responseObject = XmlObject.Factory.parse(is); return responseObject; } } catch (XmlException e) { log.error("Error parsing response.", e); // TODO add handling to identify HTML response // if (responseString.contains(HTML_TAG_IN_RESPONSE)) { // log.error("Received HTML!\n" + responseString + "\n"); // } String msg = "Could not parse response (received via " + requestMethod + ") to the request\n\n" + request + "\n\n\n" + Tools.getStackTrace(e); // msg = msg + "\n\nRESPONSE STRING:\n<![CDATA[" + responseObject.xmlText() + "]]>"; OwsExceptionReport er = new OwsExceptionReport(ExceptionCode.NoApplicableCode, "Client.doSend()", msg); return er.getDocument(); } catch (Exception e) { log.error("Error executing method on httpClient.", e); return new OwsExceptionReport(ExceptionCode.NoApplicableCode, "service", e.getMessage()).getDocument(); } }
From source file:org.camunda.bpm.engine.rest.standalone.AbstractEmptyBodyFilterTest.java
@Test public void testBodyIsEmpty() throws IOException { evaluatePostRequest(new ByteArrayEntity("".getBytes("UTF-8")), ContentType.create(MediaType.APPLICATION_JSON).toString(), 200, true); }
From source file:org.camunda.bpm.engine.rest.standalone.AbstractEmptyBodyFilterTest.java
@Test public void testBodyIsNull() throws IOException { evaluatePostRequest(null, ContentType.create(MediaType.APPLICATION_JSON).toString(), 200, true); }
From source file:org.opens.urlmanager.it.CreateITCase.java
private HttpResponse doRequest(String url, File file, String mime) throws Exception { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://localhost:8080/urlmanager/" + url); httpPost.addHeader("Accept", mime); httpPost.setEntity(// ww w. jav a 2 s . c o m new InputStreamEntity(new FileInputStream(file), file.length(), ContentType.create(mime))); return httpClient.execute(httpPost); }
From source file:com.aptana.webserver.internal.core.builtin.LocalWebServerHttpRequestHandler.java
private static ContentType getMimeType(String fileName) { return ContentType.create( MimeTypesRegistry.INSTANCE.getMimeType(Path.fromPortableString(fileName).getFileExtension())); }
From source file:io.github.kitarek.elasthttpd.plugins.consumers.file.producer.HttpFileProducer.java
private void transformFileEntityIntoHttpEntity(final FileEntity fileEntity, final HttpResponse response) { final FileEntityMetadata fileEntityMetadata = fileEntity.getFileEntityMetadata(); fileEntityMetadata.getOptionalContentType().dispatch(new OptionalDispatcher<String>() { public void notPresent() { response.setEntity(new InputStreamEntity(fileEntity.getInputStreamFromFile(), fileEntityMetadata.getFileLength())); }/*from ww w. j av a 2 s . com*/ public void present(String contentType) { response.setEntity(new InputStreamEntity(fileEntity.getInputStreamFromFile(), fileEntityMetadata.getFileLength(), ContentType.create(contentType))); } }); }
From source file:test.portlet.service.impl.MolgenisAPIRequestLocalServiceImpl.java
private void test() { HttpClient c = HttpClientBuilder.create().build(); HttpPost p = new HttpPost("http://localhost:8080/api/jsonws/invoke"); //BiBBoxCommonServices-portlet.logapi/getapiversion/data/ //BiBBoxCommonServices-portlet.logapi/testmethode String post = "{\"/BiBBoxCommonServices-portlet.logapi/testmethode\": {\"jason\": abc,\"param2\": 123,\"param3\": test}}"; p.setEntity(new StringEntity(post, ContentType.create("application/json"))); try {/*from w ww . j a v a 2 s. c om*/ HttpResponse r = c.execute(p); BufferedReader rd = new BufferedReader(new InputStreamReader(r.getEntity().getContent())); String line = ""; while ((line = rd.readLine()) != null) { System.out.println("+++++++" + line); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:net.bluemix.newsaggregator.api.AuthenticationServlet.java
private String getAccessTokenFromCodeResponse(String id, String secret, String redirect, String code) { String output = null;/*from w ww.j a va2s . co m*/ try { configureSSL(); org.apache.http.client.fluent.Request req = Request .Post("https://idaas.ng.bluemix.net/sps/oauth20sp/oauth20/token"); String body = "client_secret=" + secret + "&grant_type=authorization_code" + "&redirect_uri=" + redirect + "&code=" + code + "&client_id=" + id; req.bodyString(body, ContentType.create("application/x-www-form-urlencoded")); org.apache.http.client.fluent.Response res = req.execute(); output = res.returnContent().asString(); output = output.substring(output.indexOf("access_token") + 15, output.indexOf("access_token") + 35); } catch (IOException e) { e.printStackTrace(); } return output; }