List of usage examples for org.apache.http.client.methods HttpPost setEntity
public void setEntity(final HttpEntity entity)
From source file:com.navnorth.learningregistry.LRClient.java
public static HttpResponse executeJsonPost(String url, StringEntity se, String username, String password) throws Exception { BufferedReader in = null;// w w w . j av a2 s .co m try { URI uri = new URI(url); HttpClient client = getHttpClient(uri.getScheme()); HttpPost post = new HttpPost(uri); post.setEntity(se); post.setHeader("Content-Type", "application/json"); if (username != null && password != null) { String userPass = username + ":" + password; byte[] encodedAuth = Base64.encodeBase64(userPass.getBytes()); String encodedAuthStr = new String(encodedAuth); post.addHeader("Authorization", "Basic " + encodedAuthStr); } HttpResponse response = client.execute(post); return response; } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:com.antorofdev.util.Http.java
/** * Performs http POST petition to server. * * @param url URL to perform POST petition. * @param parameters Parameters to include in petition. * * @return Response from the server.//w w w. jav a 2s.c om * @throws IOException If the <tt>parameters</tt> have errors, connection timmed out, * socket timmed out or other error related with the connection occurs. */ public static HttpResponse post(String url, ArrayList<NameValuePair> parameters) throws IOException { HttpParams httpParameters = new BasicHttpParams(); int timeoutConnection = 10000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); int timeoutSocket = 10000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpClient httpclient = new DefaultHttpClient(httpParameters); HttpPost httppost = new HttpPost(url); if (parameters != null) httppost.setEntity(new UrlEncodedFormEntity(parameters)); HttpResponse response = httpclient.execute(httppost); return response; }
From source file:att.jaxrs.client.Content.java
public static String deleteContent(long content_id) { List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("content_id", Long.toString(content_id))); String resultStr = ""; try {//from w w w . ja va 2 s .co m DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost(Constants.DELETE_CONTENT_RESOURCE); post.setEntity(new UrlEncodedFormEntity(urlParameters)); HttpResponse result = httpClient.execute(post); System.out.println(Constants.RESPONSE_STATUS_CODE + result); resultStr = Util.getStringFromInputStream(result.getEntity().getContent()); System.out.println(Constants.RESPONSE_BODY + resultStr); } catch (Exception e) { System.out.println(e.getMessage()); } return resultStr; }
From source file:Main.java
/** * Performs an HTTP Post request to the specified url with the * specified parameters, and return the string from the HttpResponse. * * @param url The web address to post the request to * @param postParameters The parameters to send via the request * @return The result string of the request * @throws Exception/*from w w w .j a v a 2 s. c o m*/ */ public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception { BufferedReader in = null; try { HttpClient client = getHttpClient(); HttpPost request = new HttpPost(url); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); request.setEntity(formEntity); HttpResponse response = client.execute(request); in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer sb = new StringBuffer(""); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL); } in.close(); String result = sb.toString(); return result; } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:jp.co.conit.sss.sn.ex2.util.SNApiUtil.java
/** * POST????<br>//w ww .j av a 2 s . c om * Exception????????<br> * HTTP?SUCCESS???????SNServerResult??????? * * @param url * @param postData * @param result * @return */ private static SNServerResult post(String url, List<NameValuePair> postData, SNServerResult result) { Log.d("SN", "POST url:" + url); for (NameValuePair nvp : postData) { Log.d("SN", "POST NameValuePair:" + "KEY:" + nvp.getName() + " VALUE:" + nvp.getValue()); } try { HttpClient httpCli = new DefaultHttpClient(); HttpPost post = new HttpPost(url); post.setEntity(new UrlEncodedFormEntity(postData, "utf-8")); HttpResponse response = httpCli.execute(post); int status = response.getStatusLine().getStatusCode(); result.mHttpStatus = status; HttpEntity entity = response.getEntity(); if (entity != null) { String responseBodyText = EntityUtils.toString(entity); entity.consumeContent(); httpCli.getConnectionManager().shutdown(); result.mResponseString = responseBodyText; Log.d("SN", "POST response:" + responseBodyText); } } catch (Exception e) { result.mCauseException = e; e.printStackTrace(); } return result; }
From source file:kindleclippings.quizlet.GetAccessToken.java
static JSONObject oauthDance() throws IOException, URISyntaxException, InterruptedException, JSONException { // start HTTP server, so when can get the authorization code InetSocketAddress addr = new InetSocketAddress(7777); HttpServer server = HttpServer.create(addr, 0); AuthCodeHandler handler = new AuthCodeHandler(); server.createContext("/", handler); ExecutorService ex = Executors.newCachedThreadPool(); server.setExecutor(ex);/*from w ww . j a v a 2s .c o m*/ server.start(); String authCode; try { Desktop.getDesktop() .browse(new URI(new StringBuilder("https://quizlet.com/authorize/") .append("?scope=read%20write_set").append("&client_id=" + clientId) .append("&response_type=code").append("&state=" + handler.state).toString())); authCode = handler.result.take(); } finally { server.stop(0); ex.shutdownNow(); } if (authCode == null || authCode.length() == 0) return null; HttpPost post = new HttpPost("https://api.quizlet.com/oauth/token"); post.setHeader("Authorization", authHeader); post.setEntity( new UrlEncodedFormEntity(Arrays.asList(new BasicNameValuePair("grant_type", "authorization_code"), new BasicNameValuePair("code", authCode)))); HttpResponse response = new DefaultHttpClient().execute(post); ByteArrayOutputStream buffer = new ByteArrayOutputStream(1000); response.getEntity().writeTo(buffer); return new JSONObject(new String(buffer.toByteArray(), "UTF-8")); }
From source file:att.jaxrs.client.Content.java
public static String addContent(Content content) { List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("content_id", content.getContent_id().toString())); urlParameters.add(new BasicNameValuePair("level", content.getLevel())); urlParameters.add(new BasicNameValuePair("presenter", content.getPresenter())); urlParameters.add(new BasicNameValuePair("reads", content.getReads())); String resultStr = ""; try {//from ww w . ja v a 2s .c om DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost(Constants.INSERT_CONTENT_RESOURCE); post.setEntity(new UrlEncodedFormEntity(urlParameters)); HttpResponse result = httpClient.execute(post); System.out.println(Constants.RESPONSE_STATUS_CODE + result); resultStr = Util.getStringFromInputStream(result.getEntity().getContent()); System.out.println(Constants.RESPONSE_BODY + resultStr); } catch (Exception e) { System.out.println(e.getMessage()); } return resultStr; }
From source file:att.jaxrs.client.Content.java
public static String updateContent(Content content) { List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("content_id", content.getContent_id().toString())); urlParameters.add(new BasicNameValuePair("level", content.getLevel())); urlParameters.add(new BasicNameValuePair("presenter", content.getPresenter())); urlParameters.add(new BasicNameValuePair("reads", content.getReads())); String resultStr = ""; try {/* ww w . j a v a 2 s.c om*/ DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost(Constants.UPDATE_CONTENT_RESOURCE); post.setEntity(new UrlEncodedFormEntity(urlParameters)); HttpResponse result = httpClient.execute(post); System.out.println(Constants.RESPONSE_STATUS_CODE + result); resultStr = Util.getStringFromInputStream(result.getEntity().getContent()); System.out.println(Constants.RESPONSE_BODY + resultStr); } catch (Exception e) { System.out.println(e.getMessage()); } return resultStr; }
From source file:com.yojiokisoft.globish1500.exception.MyUncaughtExceptionHandler.java
/** * ?????./*from www. j a v a2 s. c om*/ */ private static void postBugReport() { List<NameValuePair> nvps = new ArrayList<NameValuePair>(); String bug = getFileBody(sBugReportFile); nvps.add(new BasicNameValuePair("dev", Build.DEVICE)); nvps.add(new BasicNameValuePair("mod", Build.MODEL)); nvps.add(new BasicNameValuePair("sdk", Build.VERSION.SDK)); nvps.add(new BasicNameValuePair("ver", sVersionName)); nvps.add(new BasicNameValuePair("bug", bug)); try { HttpPost httpPost = new HttpPost("http://mrp-bug-report.appspot.com/bug"); httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.execute(httpPost); } catch (IOException e) { e.printStackTrace(); } sBugReportFile.delete(); }
From source file:org.opencastproject.remotetest.server.resource.CaptureResources.java
public static HttpResponse stopCapturePost(TrustedHttpClient client, String id) throws Exception { HttpPost post = new HttpPost(getServiceUrl() + "stopCapture"); List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); params.add(new BasicNameValuePair("recordingID", id)); post.setEntity(new UrlEncodedFormEntity(params)); return client.execute(post); }