// Instantiate the custom HttpClient DefaultHttpClient client = new MyHttpClient(getApplicationContext()); HttpGet get = new HttpGet("https://www.mydomain.ch/rest/contacts/23"); // Execute the GET call and obtain the response HttpResponse getResponse = client.execute(get); HttpEntity responseEntity = getResponse.getEntity();
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 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); }
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()); } }
HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http"); DefaultHttpClient httpclient = new DefaultHttpClient(); try { httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); HttpHost target = new HttpHost("issues.apache.org", 443, "https"); HttpGet req = new HttpGet("/"); System.out.println("executing request to " + target + " via " + proxy); HttpResponse rsp = httpclient.execute(target, req); ... } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); }
try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); output = EntityUtils.toString(httpEntity); }
public void wipeCache(String entity, String event, String cacheKey) throws IOException { HttpPut request; String url; if (cacheKey.contains("/")) { url = baseUrl + "/cache/" + entity + "/" + event; request = new HttpPut(url); request.setEntity(new StringEntity(cacheKey, ContentType.create(APPLICATION_JSON, UTF_8))); } else { url = baseUrl + "/cache/" + entity + "/" + cacheKey + "/" + event; request = new HttpPut(url); } HttpResponse response = null; try { response = client.execute(request); if (response.getStatusLine().getStatusCode() != 200) { String msg = EntityUtils.toString(response.getEntity()); throw new IOException(INVALID_RESPONSE + response.getStatusLine().getStatusCode() + " with cache wipe url " + url + "\n" + msg); } } finally { cleanup(request, response); } }
public String getLookupSnapshotCacheState(String lookupTableName, String snapshotID) throws IOException { String url = baseUrl + "/tables/" + lookupTableName + "/" + snapshotID + "/snapshotLocalCache/state"; HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); String content = getContent(response); if (response.getStatusLine().getStatusCode() != 200) { throw new IOException(INVALID_RESPONSE + response.getStatusLine().getStatusCode() + WITH_URL + url + "\n"); } return content; }
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); HttpPost httppost = new HttpPost(http://someJSONUrl/jsonWebService); // Depends on your web service httppost.setHeader("Content-type", "application/json"); InputStream inputStream = null; String result = null; try { HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); inputStream = entity.getContent(); // json is UTF-8 by default BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } result = sb.toString(); } catch (Exception e) { // Oops } finally { try{if(inputStream != null)inputStream.close();}catch(Exception squish){} }
public String postJson(String url, Object object) { try { httpClient.getParams().setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost post = new HttpPost(url); StringEntity entity = new StringEntity( new ObjectMapper().writeValueAsString(object), "utf-8"); entity.setContentType("application/json; charset=utf-8"); post.setEntity(entity); post.releaseConnection(); // Here we go! return EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void shouldFindLastRequestMade() throws Exception { FakeHttp.addPendingHttpResponse(200, "a happy response body"); FakeHttp.addPendingHttpResponse(200, "a happy response body"); FakeHttp.addPendingHttpResponse(200, "a happy response body"); DefaultHttpClient client = new DefaultHttpClient(); client.execute(new HttpGet("http://www.first.org")); client.execute(new HttpGet("http://www.second.org")); client.execute(new HttpGet("http://www.third.org")); assertThat(((HttpUriRequest) FakeHttp.getLatestSentHttpRequest()).getURI()) .isEqualTo(URI.create("http://www.third.org")); }
public String getKylinProperties() throws IOException { String url = baseUrl + "/admin/config"; HttpGet request = new HttpGet(url); HttpResponse response = null; try { response = client.execute(request); String msg = EntityUtils.toString(response.getEntity()); Map<String, String> map = JsonUtil.readValueAsMap(msg); msg = map.get("config"); if (response.getStatusLine().getStatusCode() != 200) throw new IOException(INVALID_RESPONSE + response.getStatusLine().getStatusCode() + " with cache wipe url " + url + "\n" + msg); return msg; } finally { cleanup(request, response); } }
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; DefaultHttpClient client = new DefaultHttpClient(); SchemeRegistry registry = new SchemeRegistry(); SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory(); socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier); registry.register(new Scheme("https", socketFactory, 443)); SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry); DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams()); // Set verifier HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); // Example send http request final String url = "https://encrypted.google.com/"; HttpPost httpPost = new HttpPost(url); HttpResponse response = httpClient.execute(httpPost);
DefaultHttpClient httpclient = new DefaultHttpClient(); // register ntlm auth scheme httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory()); httpclient.getCredentialsProvider().setCredentials( // Limit the credentials only to the specified domain and port new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), // Specify credentials, most of the time only user/pass is needed new NTCredentials(username, password, "", "") ); HttpUriRequest httpget = new HttpGet(your_URL); HttpResponse response = httpclient.execute(httpget); String responseBody = EntityUtils.toString(response.getEntity()); Log.i(tag,"responseBody =>>>>>>>>>>"+responseBody);
private boolean setCache(boolean flag) throws IOException { String url = baseUrl + "/admin/config"; HttpPut put = newPut(url); HttpResponse response = null; try { HashMap<String, String> paraMap = new HashMap<String, String>(); paraMap.put("key", "kylin.query.cache-enabled"); paraMap.put("value", flag + ""); put.setEntity(new StringEntity(new ObjectMapper().writeValueAsString(paraMap), UTF_8)); response = client.execute(put); EntityUtils.consume(response.getEntity()); if (response.getStatusLine().getStatusCode() != 200) { return false; } else { return true; } } finally { cleanup(put, response); } }
public HttpResponse makeRequestAndGetResponse(String url, Map<String, String> headers) { HttpResponse response = null; try { HttpGet getRequest = new HttpGet(url); // add all headers for (Entry<String, String> header : headers.entrySet()) { getRequest.addHeader(header.getKey(), header.getValue()); } response = httpClient.execute(getRequest); getRequest.reset(); } catch (Exception e) { throw new RuntimeException(e); } return response; }
HttpHost proxy = new HttpHost("ip address",port number); DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy); HttpPost httpost = new HttpPost(url); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("param name", param)); httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.ISO_8859_1)); HttpResponse response = httpclient.execute(httpost); HttpEntity entity = response.getEntity(); System.out.println("Request Handled?: " + response.getStatusLine()); InputStream in = entity.getContent(); httpclient.getConnectionManager().shutdown();
public String postXml(String url, Object object) { try { httpClient.getParams().setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost post = new HttpPost(url); StringEntity entity = new StringEntity( new XmlMapper().writeValueAsString(object), "utf-8"); entity.setContentType("application/xml; charset=utf-8"); post.setEntity(entity); post.releaseConnection(); // Here we go! return EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } }
HttpParams httpClientParams = new BasicHttpParams(); httpClientParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false); DefaultHttpClient client = new DefaultHttpClient(httpClientParams); url += "/?activationRegion=" + activationRegionName; HttpGet method = new HttpGet(url); HttpResponse response = client.execute(method); int statusCode = response.getStatusLine().getStatusCode();