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 } }
HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("LINK TO SERVER");
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 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);
URL url=new URL(urlToHit); HttpPost httppost = new HttpPost(url.toString()); HttpResponse response = LoginScreen.httpClient.execute(httppost); // Log.v("response code",""+response.getStatusLine().getStatusCode()); // Get hold of the response entity HttpEntity entity = response.getEntity(); InputStream instream = null; if (entity != null) { instream = entity.getContent(); } xr.parse(new InputSource(instream)); //SAX parsing
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 }
HttpPost method = new HttpPost(String .format("http://%s:%d%s/%s///%s", mWebServer.getBindHost(), mWebServer.getLocalPort(), Constants.REST_API_PREFIX, PathsRestServiceHandler.SERVICE_PREFIX, PathsRestServiceHandler.EXISTS)); try { HttpResponse response = client.execute(method); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { return true; LOG.debug(IOUtils.toString(response.getEntity().getContent())); return false; } catch (IOException e) {
DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent();
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; }
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); }
String encoding = Base64Encoder.encode ("test1:test1"); HttpPost httppost = new HttpPost("http://host:post/test/login"); httppost.setHeader("Authorization", "Basic " + encoding); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity();
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); }
try { final DefaultHttpClient client = new DefaultHttpClient(); final HttpPost httppost = new HttpPost("http://example.location.com/myxml"); final HttpResponse response = client.execute(httppost); final HttpEntity entity = response.getEntity(); final InputStream in = entity.getContent(); final SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); final XmlHandler handler = new XmlHandler(); parser.parse(in, handler); //TODO: get the data from your handler } catch (final Exception e) { Log.e("ParseError", "Error parsing xml", e); }
URL url=new URL(urlToHit); LoginScreen.httpClient = new DefaultHttpClient(); //LoginScreen is the name of the current Activity HttpPost httppost = new HttpPost(url.toString()); HttpResponse response = LoginScreen.httpClient.execute(httppost); xr.parse(new InputSource(url.openStream())); //SAX parsing
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(); } }