private void setEntityIfGiven(HttpEntityEnclosingRequestBase request, Object entity) { if (entity != null) { if (entity instanceof File) { request.setEntity(new FileEntity((File) entity)); } else { request.setEntity(new StringEntity((String) entity, Charset.defaultCharset())); } } }
public String postPage(String url, File data, boolean returnAddr) { ret = null; httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); httpPost = new HttpPost(url); response = null; FileEntity tmp = null; tmp = new FileEntity(data,"UTF-8"); httpPost.setEntity(tmp); try { response = httpClient.execute(httpPost,localContext); } catch (ClientProtocolException e) { System.out.println("HTTPHelp : ClientProtocolException : "+e); } catch (IOException e) { System.out.println("HTTPHelp : IOException : "+e); } ret = response.getStatusLine().toString(); return ret; }
HttpPost httppost = getUrlForFormat(toFormat); HttpEntity reqEntity = new FileEntity(f, map(fromFormat) );
static AbstractHttpEntity getEntity(ReqEntitySimple bean) throws UnsupportedEncodingException, IOException { AbstractHttpEntity entity = null; ContentType contentType = null; if (bean.getContentType() != null) { org.wiztools.restclient.bean.ContentType ct = bean.getContentType(); contentType = ContentType.create(ct.getContentType(), ct.getCharset()); } if (bean instanceof ReqEntityString) { entity = new StringEntity(((ReqEntityString) bean).getBody(), contentType); } else if (bean instanceof ReqEntityByteArray) { entity = new ByteArrayEntity(((ReqEntityByteArray) bean).getBody(), contentType); } else if (bean instanceof ReqEntityStream) { entity = new InputStreamEntity(((ReqEntityStream) bean).getBody(), ((ReqEntityStream) bean).getLength(), contentType); } else if (bean instanceof ReqEntityFile) { entity = new FileEntity(((ReqEntityFile) bean).getBody(), contentType); } return entity; }
HttpClient http = AndroidHttpClient.newInstance("MyApp"); HttpPost method = new HttpPost("http://url-to-server"); method.setEntity(new FileEntity(new File("path-to-file"), "application/octet-stream")); HttpResponse response = http.execute(method);
}else if(ENTITY_FILE.equals(entry.getKey())){//file if(File.class.isAssignableFrom(entry.getValue().getClass())){ entity = new FileEntity((File)entry.getValue(), ContentType.APPLICATION_OCTET_STREAM); }else if(entry.getValue().getClass()==String.class){ entity = new FileEntity(new File((String) entry.getValue()), ContentType.create("text/plain", "UTF-8"));
private void setEntityIfGiven(HttpEntityEnclosingRequestBase request, Object entity) { if (entity != null) { if (entity instanceof File) { request.setEntity(new FileEntity((File) entity)); } else { request.setEntity(new StringEntity((String) entity, Charset.defaultCharset())); } } }
public void upload(File file) throws ClientProtocolException, IOException { HttpClient client = new DefaultHttpClient(new BasicHttpParams()); HttpPost httpPost = new HttpPost(); httpPost.setEntity(new FileEntity(file, "image/png")); client.execute(httpPost); }
HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(address); // a string entity containing JSON: post.setEntity(new StringEntity("{ \"actually\" : [\"json\", \"this time\"]}"); // or uploading an image file: post.setEntity(new FileEntity(new File("some/local/image.png"), "image/png"); // or some random bytes: byte[] randomBytes = new byte[128]; new Random().nextBytes(randomBytes); post.setEntity(new ByteArrayEntity(randomBytes); HttpResponse response = client.execute(post); ...
try { Credentials credentials = new UsernamePasswordCredentials("admin", "password"); //-u admin:password DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials); HttpPut put = new HttpPut("http://localhost:8080/my-server/file.ext"); //-X PUT put.setEntity(new FileEntity(new File("/absolute/path/file/to/upload/file.ext"), "image/png")); //@ - absolute path httpClient.execute(put); } catch(Exception e) { //-f, fail silently }
@Override public void put(String url, File localFile, String contentType, boolean expectContinue) throws IOException { FileEntity content = new FileEntity(localFile); this.put(url, content, contentType, expectContinue); }
/** * Set the request body using content in a file. * * @param file The file containing the request * @param contentType The content type e.g. 'text/xml' */ @PublicAtsApi public void setRequestBody( File file, String contentType ) { requestBody = new FileEntity(file, ContentType.create(contentType)); }
/** * Set the request body using content in a file. * * @param file The file containing the request * @param contentType The content type, e.g. 'text/xml' * @param charset The charset, e.g. 'UTF-8' */ @PublicAtsApi public void setRequestBody( File file, String contentType, String charset ) { requestBody = new FileEntity(file, ContentType.create(contentType, charset)); }
private int putPayload(URI url, File file) throws IOException { HttpPut put = new HttpPut(url); put.setEntity(new FileEntity(file)); HttpResponse response = httpClient.execute(put); int status = response.getStatusLine().getStatusCode(); EntityUtils.consumeQuietly(response.getEntity()); return status; }
/** * Deploys provided file under specified groupId, artifactId and version with packaging=jar. */ public long deployJar(String groupId, String artifactId, String version, File jar) throws IOException { HttpEntity jarEntity = new FileEntity(jar, ContentType.DEFAULT_BINARY); deploy(jarEntity, groupId, artifactId, version, ".jar"); return jar.length(); }
@Override public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, File bodyContents, OAuthAsyncRequestCallback<T> callback, OAuthRequest.ResponseConverter<T> converter) { final HttpEntity entity = bodyContents == null ? null : new FileEntity(bodyContents); return doExecuteAsync(userAgent, headers, httpVerb, completeUrl, entity, callback, converter); }
protected HttpEntity fileToHttpEntity(String filename, Lang lang) { // Leave RDF/XML to the XML parse, else it's UTF-8. String charset = (lang.equals(Lang.RDFXML) ? null : WebContent.charsetUTF8); // HttpClient Content type. ContentType ct = ContentType.create(lang.getContentType().getContentType(), charset); // Repeatable. return new FileEntity(new File(filename), ct); }
protected HttpEntity fileToHttpEntity(String filename, Lang lang) { // Leave RDF/XML to the XML parse, else it's UTF-8. String charset = (lang.equals(Lang.RDFXML) ? null : WebContent.charsetUTF8); // HttpClient Content type. ContentType ct = ContentType.create(lang.getContentType().getContentType(), charset); // Repeatable. return new FileEntity(new File(filename), ct); }
@Override public HttpEntity createEntity() throws ClientServicesException { FileEntity fileEnt = new FileEntity(content, getContentType()); fileEnt.setContentEncoding(BINARY); // Is that OK? return fileEnt; }
@Override protected File buildResult(final HttpContext context) throws Exception { final FileEntity entity = new FileEntity(this.file, this.contentType); entity.setContentEncoding(this.contentEncoding); this.response.setEntity(entity); return process(this.response, this.file, this.contentType); }