package no.fsat.reb; import java.io.File; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.AuthCache; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.auth.BasicScheme; import org.apache.http.impl.client.BasicAuthCache; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; public class Poster { private static final String CHARSET = "UTF-8"; private static String theUrl = "http://jboss-test.uio.no/fsrest/rest/bas/oppdater/bilde/form"; public static void main(String[] args) { HttpHost target = new HttpHost("jboss-test.uio.no", 443, "https"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials("", "")); CloseableHttpClient client = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider).build(); File file = new File("C:/temp/richard.jpg"); try { AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local // auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(target, basicAuth); // Add AuthCache to the execution context HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authCache); HttpPost post = new HttpPost(theUrl); FileBody fileBody = new FileBody(file); StringBody stringBody1 = new StringBody("15075528253", ContentType.MULTIPART_FORM_DATA); StringBody stringBody2 = new StringBody("jpg", ContentType.MULTIPART_FORM_DATA); // MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.addPart("fnr", stringBody1); builder.addPart("type", stringBody2); builder.addPart("bilde", fileBody); HttpEntity entity = builder.build(); // post.setEntity(entity); HttpResponse response = client.execute(post); System.out.println(response.toString()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }