public void postData() { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php"); try { // Add your data List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("id", "12345")); nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } }
HttpPost request = new HttpPost(); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("key", "value")); request.setEntity(new UrlEncodedFormEntity(params)); httpClient.execute(request);
String response = new String(); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("j", request)); HttpEntity requestEntity = new UrlEncodedFormEntity(nameValuePairs); HttpPost httpPost = new HttpPost(mURI); httpPost.setEntity(requestEntity); HttpResponse httpResponse = mHttpClient.execute(httpPost); HttpEntity responseEntity = httpResponse.getEntity(); if(responseEntity!=null) { response = EntityUtils.toString(responseEntity); }
// Build the JSON object to pass parameters JSONObject jsonObj = new JSONObject(); jsonObj.put("username", username); jsonObj.put("apikey", apikey); // Create the POST object and add the parameters HttpPost httpPost = new HttpPost(url); StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8); entity.setContentType("application/json"); httpPost.setEntity(entity); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(httpPost);
String postUrl = "www.site.com";// put in your url Gson gson = new Gson(); HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(postUrl); StringEntity postingString = new StringEntity(gson.toJson(pojo1));//gson.tojson() converts your pojo to json post.setEntity(postingString); post.setHeader("Content-type", "application/json"); HttpResponse response = httpClient.execute(post);
HttpEntity entity = MultipartEntityBuilder .create() .addTextBody("number", "5555555555") .addTextBody("clip", "rickroll") .addBinaryBody("upload_file", new File(filePath), ContentType.create("application/octet-stream"), "filename") .addTextBody("tos", "agree") .build(); HttpPost httpPost = new HttpPost("http://some-web-site"); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); HttpEntity result = response.getEntity();
<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 }
private DataInputStream executeRequest(HttpPost method, ByteArrayOutputStream output) { HttpResponse response = null; try { method.setEntity(new ByteArrayEntity(output.toByteArray())); response = httpClient.execute(method); int statusCode = response.getStatusLine().getStatusCode(); if(statusCode != HttpURLConnection.HTTP_OK) { String message = response.getStatusLine().getReasonPhrase(); VoldemortIOUtils.closeQuietly(response); throw new UnreachableStoreException("HTTP request to store " + getName() + " returned status code " + statusCode + " " + message); } return new DataInputStream(response.getEntity().getContent()); } catch(IOException e) { VoldemortIOUtils.closeQuietly(response); throw new UnreachableStoreException("Could not connect to " + storeUrl + " for " + getName(), e); } }
public void post() throws Exception{ HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://www.baidu.com"); String xml = "<xml>xxxx</xml>"; HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8")); post.setEntity(entity); HttpResponse response = client.execute(post); String result = EntityUtils.toString(response.getEntity()); }
private boolean httpPost(String url, String msg) { boolean ret = false; CloseableHttpClient httpClient = HttpClientBuilder.create().build(); CloseableHttpResponse response = null; try { HttpPost request = new HttpPost(url); List<NameValuePair> nvps = new ArrayList<>(); nvps.add(new BasicNameValuePair("name", monitorName)); nvps.add(new BasicNameValuePair("msg", msg)); request.setEntity(new UrlEncodedFormEntity(nvps)); response = httpClient.execute(request); HttpEntity entity = response.getEntity(); if (entity != null) { LOG.info(EntityUtils.toString(entity)); } EntityUtils.consume(entity); ret = true; } catch (Exception e) { LOG.error("Exception when sending http request to ali monitor", e); } finally { try { if (response != null) response.close(); httpClient.close(); } catch (Exception e) { LOG.error("Exception when closing httpclient", e); } } return ret; }
HttpPost httppost = new HttpPost(SERVICE_EPR); StringEntity se = new StringEntity(SOAPRequestXML,HTTP.UTF_8); se.setContentType("text/xml"); httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8"); httppost.setEntity(se); HttpClient httpclient = new DefaultHttpClient(); BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(httppost); response.put("HTTPStatus",httpResponse.getStatusLine().toString());
private static HttpResponse post(String url, Object requestObject) throws IOException { String json = objectMapper.writeValueAsString(requestObject); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity(json)); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); return httpClient.execute(httpPost); }
public static HttpResult httpPostLarge(String url, Map<String, String> headers, String content) { try { HttpClientBuilder builder = HttpClients.custom(); builder.setUserAgent(UtilsAndCommons.SERVER_VERSION); builder.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS); CloseableHttpClient httpClient = builder.build(); HttpPost httpost = new HttpPost(url); for (Map.Entry<String, String> entry : headers.entrySet()) { httpost.setHeader(entry.getKey(), entry.getValue()); } httpost.setEntity(new StringEntity(content, ContentType.create("application/json", "UTF-8"))); HttpResponse response = httpClient.execute(httpost); HttpEntity entity = response.getEntity(); HeaderElement[] headerElements = entity.getContentType().getElements(); String charset = headerElements[0].getParameterByName("charset").getValue(); return new HttpResult(response.getStatusLine().getStatusCode(), IOUtils.toString(entity.getContent(), charset), Collections.<String, String>emptyMap()); } catch (Exception e) { return new HttpResult(500, e.toString(), Collections.<String, String>emptyMap()); } }
public static HttpResponse makeRequest(String path, Map params) throws Exception { //instantiates httpclient to make request DefaultHttpClient httpclient = new DefaultHttpClient(); //url with the post data HttpPost httpost = new HttpPost(path); //convert parameters into JSON object JSONObject holder = getJsonObjectFromMap(params); //passes the results to a string builder/entity StringEntity se = new StringEntity(holder.toString()); //sets the post request as the resulting string httpost.setEntity(se); //sets a request header so the page receving the request //will know what to do with it httpost.setHeader("Accept", "application/json"); httpost.setHeader("Content-type", "application/json"); //Handles what is returned from the page ResponseHandler responseHandler = new BasicResponseHandler(); return httpclient.execute(httpost, responseHandler); }
HttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost("http://www.a-domain.com/foo/"); // Request parameters and other properties. List<NameValuePair> params = new ArrayList<NameValuePair>(2); params.add(new BasicNameValuePair("param-1", "12345")); params.add(new BasicNameValuePair("param-2", "Hello!")); httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); //Execute and get the response. HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); try { // do something useful } finally { instream.close(); } }
throws AzkabanClientException { try { HttpPost httpPost = new HttpPost(url); List<NameValuePair> nvps = new ArrayList<>(); nvps.add(new BasicNameValuePair(AzkabanClientParams.ACTION, "login")); nvps.add(new BasicNameValuePair(AzkabanClientParams.USERNAME, username)); nvps.add(new BasicNameValuePair(AzkabanClientParams.PASSWORD, password)); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); CloseableHttpResponse response = httpClient.execute(httpPost);