HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(new HttpGet(URL)); StatusLine statusLine = response.getStatusLine(); if(statusLine.getStatusCode() == HttpStatus.SC_OK){ ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); String responseString = out.toString(); out.close(); //..more logic } else{ //Closes the connection. response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); }
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()); }
HttpGet get = new HttpGet("http://192.168.1.36/"); get.setHeader("Content-Type", "text/plain; charset=utf-8"); get.setHeader("Expect", "100-continue"); HttpResponse resp = null; try { HttpClient httpClient = new DefaultHttpClient(); resp = httpClient.execute(get); } catch (ClientProtocolException e) { Log.e(getClass().getSimpleName(), "HTTP protocol error", e); } catch (IOException e) { Log.e(getClass().getSimpleName(), "Communication error", e); } if (resp != null) { // got a response, do something with it } else { // there was a problem }
// 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);
@Override protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException { HttpGet httpRequest = new HttpGet(imageUri); HttpResponse response = httpClient.execute(httpRequest); HttpEntity entity = response.getEntity(); BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); return bufHttpEntity.getContent(); } }
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) {
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); } }
HttpClient httpclient; HttpPost httppost; ArrayList<NameValuePair> postParameters; httpclient = new DefaultHttpClient(); httppost = new HttpPost("your login link"); postParameters = new ArrayList<NameValuePair>(); postParameters.add(new BasicNameValuePair("param1", "param1_value")); postParameters.add(new BasicNameValuePair("param2", "param2_value")); httpPost.setEntity(new UrlEncodedFormEntity(postParameters)); HttpResponse response = httpclient.execute(httppost);
HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpGet httpGet = new HttpGet("http://www.example.com/" + URL); HttpResponse response = httpClient.execute(httpGet, localContext);
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(); HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000); //Timeout Limit HttpGet httpGet = new HttpGet("http://example.com/blob/getuploadurl"); response = httpclient.execute(httpGet);
JSONObject resultJson = new JSONObject(resultJsonString); String blobKey = resultJson.getString("blobKey"); String servingUrl = resultJson.getString("servingUrl"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("userId", userId)); nameValuePairs.add(new BasicNameValuePair("blobKey",blobKey)); nameValuePairs.add(new BasicNameValuePair("servingUrl",servingUrl)); HttpClient httpclient = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000); HttpPost httppost = new HttpPost(url); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); // Continue to store the (immediately available) serving url in local storage f.ex
HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(urltofetch); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { long len = entity.getContentLength(); InputStream inputStream = entity.getContent(); // write the file to whether you want it. }
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); }
HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://.../whatever"); List <NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("param1", "value1")); params.add(new BasicNameValuePair("param2", "value2")); ... httpost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse response = httpclient.execute(httppost);
HttpClient httpClient = new DefaultHttpClient(); CookieStore cookieStore = new BasicCookieStore(); HttpContext httpContext = new BasicHttpContext(); httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); // ... HttpResponse response1 = httpClient.execute(method1, httpContext); // ... HttpResponse response2 = httpClient.execute(method2, httpContext); // ...
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(); } }