<s> HttpClient httpClient = new DefaultHttpClient(); </s> //Deprecated HttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead try { HttpPost request = new HttpPost("http://yoururl"); StringEntity params =new StringEntity("details={\"name\":\"myname\",\"age\":\"20\"} "); request.addHeader("content-type", "application/x-www-form-urlencoded"); request.setEntity(params); HttpResponse response = httpClient.execute(request); // handle response here... }catch (Exception ex) { // handle exception here } finally { <s> httpClient.getConnectionManager().shutdown(); </s> //Deprecated }
DefaultHttpClient httpclient = new DefaultHttpClient(); String url = "http://localhost"; HttpPost httpPost = new HttpPost(url); httpPost.addHeader("header-name" , "header-value"); HttpResponse response = httpclient.execute(httpPost);
public HttpResponse doPost(String query, String username, String password, String body) { HttpPost httpPost = new HttpPost(context + query); httpPost.addHeader("content-type", "application/json"); httpPost.setEntity(new StringEntity(body, StandardCharsets.UTF_8)); return doRequest(httpPost, username, password); }
StringEntity params = new StringEntity("username=" + username1 + "&password;=" + password1); HttpPost request = new HttpPost(path); HttpClient httpClient = new DefaultHttpClient(); request.addHeader("content-type", "application/x-www-form-urlencoded"); request.setEntity(params);
/** * Create a HttpPost for the given configuration. * <p>The default implementation creates a standard HttpPost with * "application/x-java-serialized-object" as "Content-Type" header. * @param config the HTTP invoker configuration that specifies the * target service * @return the HttpPost instance * @throws java.io.IOException if thrown by I/O methods */ protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException { HttpPost httpPost = new HttpPost(config.getServiceUrl()); RequestConfig requestConfig = createRequestConfig(config); if (requestConfig != null) { httpPost.setConfig(requestConfig); } LocaleContext localeContext = LocaleContextHolder.getLocaleContext(); if (localeContext != null) { Locale locale = localeContext.getLocale(); if (locale != null) { httpPost.addHeader(HTTP_HEADER_ACCEPT_LANGUAGE, locale.toLanguageTag()); } } if (isAcceptGzipEncoding()) { httpPost.addHeader(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } return httpPost; }
HttpPost request = new HttpPost(url); request.addHeader("Content-Type", "application/json"); HttpResponse response; try { request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8"))); response = mHttpClient.execute(request); String jsonString = EntityUtils.toString(response.getEntity()); } catch (Exception e) { e.printStackTrace(); }
private <T extends Serializable, Method extends BotApiMethod<T>> String sendMethodRequest(Method method) throws TelegramApiValidationException, IOException { method.validate(); String url = getBaseUrl() + method.getMethod(); HttpPost httppost = configuredHttpPost(url); httppost.addHeader("charset", StandardCharsets.UTF_8.name()); httppost.setEntity(new StringEntity(objectMapper.writeValueAsString(method), ContentType.APPLICATION_JSON)); return sendHttpPostRequest(httppost); }
String digestqueryURL = serverurl + subsite + "/" + "_api/contextinfo"; HttpPost httpPost = new HttpPost(digestquery); httpPost.addHeader("Accept", "application/json;odata=verbose"); httpPost.addHeader("X-ClientService-ClientTag", "SDK-JAVA"); HttpResponse response = httpClient.execute(httpPost); byte[] content = EntityUtils.toByteArray(response.getEntity()); String jsonString = new String(content, "UTF-8"); JSONObject json = new JSONObject(jsonString); String FormDigestValue = json.getJSONObject("d") .getJSONObject("GetContextWebInformation") .getString("FormDigestValue");
private JSONObject readJSONObjectFromUrlPOST(String urlString, Map<String, String> headers, String payload) throws IOException, JSONException { HttpClient httpClient = openConnection(); HttpPost request = new HttpPost(urlString); for (Map.Entry<String, String> entry : headers.entrySet()) { request.addHeader(entry.getKey(), entry.getValue()); } HttpEntity httpEntity = new StringEntity(payload); request.setEntity(httpEntity); HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK && response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode() + " : " + response.getStatusLine().getReasonPhrase()); } InputStream content = response.getEntity().getContent(); return readAllIntoJSONObject(content); }
JSONObject json = new JSONObject(); json.put("someKey", "someValue"); CloseableHttpClient httpClient = HttpClientBuilder.create().build(); try { HttpPost request = new HttpPost("http://yoururl"); StringEntity params = new StringEntity(json.toString()); request.addHeader("content-type", "application/json"); request.setEntity(params); httpClient.execute(request); // handle response here... } catch (Exception ex) { // handle exception here } finally { httpClient.close(); }
HttpClient httpClient = new DefaultHttpClient(); HttpEntity httpEntity = null; HttpResponse httpResponse = null; // Checking http request method type if (method == POST) { HttpPost httpPost = new HttpPost(url); if (params != null) { httpPost.setHeader("Content-type", "application/json"); httpPost.addHeader("Accept", "application/json"); httpPost.setEntity(new StringEntity(params)); }
/** * Create a HttpPost for the given configuration. * <p>The default implementation creates a standard HttpPost with * "application/x-java-serialized-object" as "Content-Type" header. * @param config the HTTP invoker configuration that specifies the * target service * @return the HttpPost instance * @throws java.io.IOException if thrown by I/O methods */ protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException { HttpPost httpPost = new HttpPost(config.getServiceUrl()); RequestConfig requestConfig = createRequestConfig(config); if (requestConfig != null) { httpPost.setConfig(requestConfig); } LocaleContext localeContext = LocaleContextHolder.getLocaleContext(); if (localeContext != null) { Locale locale = localeContext.getLocale(); if (locale != null) { httpPost.addHeader(HTTP_HEADER_ACCEPT_LANGUAGE, locale.toLanguageTag()); } } if (isAcceptGzipEncoding()) { httpPost.addHeader(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } return httpPost; }
private JSONObject readJSONObjectFromUrlPOST(String urlString, LivySessionService livySessionService, Map<String, String> headers, String payload) throws IOException, JSONException, SessionManagerException { HttpClient httpClient = livySessionService.getConnection(); HttpPost request = new HttpPost(urlString); for (Map.Entry<String, String> entry : headers.entrySet()) { request.addHeader(entry.getKey(), entry.getValue()); } HttpEntity httpEntity = new StringEntity(payload); request.setEntity(httpEntity); HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK && response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode() + " : " + response.getStatusLine().getReasonPhrase()); } InputStream content = response.getEntity().getContent(); return readAllIntoJSONObject(content); }
public void clearCacheForCubeMigration(String cube, String project, String model, Map<String, String> tableToProjects) throws IOException { String url = baseUrl + "/cache/migration"; HttpPost post = new HttpPost(url); post.addHeader("Accept", "application/json, text/plain, */*"); post.addHeader("Content-Type", APPLICATION_JSON); HashMap<String, Object> paraMap = new HashMap<String, Object>(); paraMap.put("cube", cube); paraMap.put("project", project); paraMap.put("model", model); paraMap.put("tableToProjects", tableToProjects); String jsonMsg = JsonUtil.writeValueAsString(paraMap); post.setEntity(new StringEntity(jsonMsg, UTF_8)); HttpResponse response = client.execute(post); if (response.getStatusLine().getStatusCode() != 200) { throw new IOException(INVALID_RESPONSE + response.getStatusLine().getStatusCode()); } }
DefaultHttpClient http = new DefaultHttpClient(); HttpPost post = new HttpPost(strURL); UsernamePasswordCredentials creds = new UsernamePasswordCredentials( Configuration.username, Configuration.developerKey); post.addHeader( BasicScheme.authenticate(creds,"US-ASCII",false) ); StringEntity entity = new StringEntity( ac.toXMLString() ); entity.setContentType("text/xml"); post.setEntity( entity ); org.apache.http.HttpResponse response = http.execute( post );
HttpPost request = new HttpPost(url); StringEntity params = new StringEntity(body); request.addHeader("content-type", "application/json"); request.setEntity(params); HttpResponse result = httpClient.execute(request); String json = EntityUtils.toString(result.getEntity(), "UTF-8"); try { JSONParser parser = new JSONParser();
HttpPost buildSendExportMethod( String type, String load, InputStream is ) throws UnsupportedEncodingException { String serviceUrl = RegisterPackageServlet.CONTEXT_PATH; if ( type != null && load != null ) { serviceUrl += "/?" + RegisterPackageServlet.PARAMETER_TYPE + "=" + type + "&" + RegisterPackageServlet.PARAMETER_LOAD + "=" + URLEncoder.encode( load, "UTF-8" ); } String urlString = constructUrl( serviceUrl ); if ( log.isDebug() ) { log.logDebug( BaseMessages.getString( PKG, "SlaveServer.DEBUG_ConnectingTo", urlString ) ); } HttpPost method = new HttpPost( urlString ); method.setEntity( new InputStreamEntity( is ) ); method.addHeader( new BasicHeader( "Content-Type", "binary/zip" ) ); return method; }
HttpClient http = new DefaultHttpClient(); HttpPost post = new HttpPost(url); post.addHeader("path", file.getAbsolutePath()); MultipartEntity multipart = new MultipartEntity(); ContentBody fileContent = new FileBody(file); //For tar.gz: "application/x-gzip" multipart.addPart("package", fileContent); post.setEntity(multipart);