HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpGet httpGet = new HttpGet("http://www.example.com/" + URL); HttpResponse response = httpClient.execute(httpGet, localContext);
HttpClient httpclient = new DefaultHttpClient(); // Create a local instance of cookie store CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet("http://www.google.com/", localContext);
HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); String url = "http://gisdata.usgs.gov/" + "xmlwebservices2/elevation_service.asmx/" HttpGet httpGet = new HttpGet(url); try { HttpResponse response = httpClient.execute(httpGet, localContext); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent();
Header authorizationHeader; try { authorizationHeader = scheme.authenticate(credentials, request, new BasicHttpContext()); } catch (AuthenticationException e) { throw new RuntimeException(e); DefaultHttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); if (entity != null) { String body = EntityUtils.toString(entity);
HttpContext context = new BasicHttpContext(); CookieStore cookieStore = new BasicCookieStore(); context.setAttribute(ClientContext.COOKIE_STORE, cookieStore); String error = null; HttpEntity entity = response.getEntity();
private void performHttpRequest(HttpRequestBase requestToServer, HttpServletResponse responseToClient, ServerHeadersHandler serverHeadersHandler) throws IOException, URISyntaxException { HttpContext context = new BasicHttpContext(); context.setAttribute(HttpRequestHandler.class.getName(), this); HttpResponse responseFromServer = client.execute(requestToServer, context); logger.debug("Performed request: {} --> {}", requestToServer.getRequestLine(), responseFromServer.getStatusLine()); responseToClient.setStatus(responseFromServer.getStatusLine().getStatusCode()); copyHeaders(responseFromServer, responseToClient, serverHeadersHandler); HttpEntity entity = responseFromServer.getEntity(); if (entity != null) { try { entity.writeTo(responseToClient.getOutputStream()); } finally { EntityUtils.consume(entity); } } } }
public static String resolveRedirect(String url) throws ClientProtocolException, IOException { HttpParams httpParameters = new BasicHttpParams(); HttpClientParams.setRedirecting(httpParameters, false); HttpClient httpClient = new DefaultHttpClient(httpParameters); HttpGet httpget = new HttpGet(url); HttpContext context = new BasicHttpContext(); HttpResponse response = httpClient.execute(httpget, context); // If we didn't get a '302 Found' we aren't being redirected. if (response.getStatusLine().getStatusCode() != HttpStatus.SC_MOVED_TEMPORARILY) throw new IOException(response.getStatusLine().toString()); Header loc[] = response.getHeaders("Location"); return loc.length > 0 ? loc[0].getValue() : null; }
public static String fetchThroughHttp(String address, String encoding) { try { org.apache.http.client.HttpClient httpClient = new org.apache.http.impl.client.DefaultHttpClient(); org.apache.http.params.HttpParams params = httpClient.getParams(); org.apache.http.params.HttpConnectionParams.setConnectionTimeout(params, 10000); org.apache.http.params.HttpConnectionParams.setSoTimeout(params, 60000); org.apache.http.protocol.HttpContext localContext = new org.apache.http.protocol.BasicHttpContext(); org.apache.http.client.methods.HttpGet httpGet = new org.apache.http.client.methods.HttpGet(address); org.apache.http.HttpResponse response = httpClient.execute(httpGet, localContext); if (response.getStatusLine().getStatusCode() != 200) { return "FAILED"; } BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), Charset.forName(encoding))); StringBuilder stringBuilder = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } return stringBuilder.toString(); } catch (Exception e) { return "FAILED with exception: " + e.getMessage(); } }
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); // ...
public String httpGet(String url) throws IOException{ HttpGet get = new HttpGet(url); DefaultHttpClient() _httpClient = new DefaultHttpClient(); BasicHttpContext httpContext = new BasicHttpContext(); HttpResponse response=null; response = httpClient .execute(get, httpContext); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); return EntityUtils.toString(response.getEntity()); }
se.setContentType("application/json"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httppostreq.setEntity(se); HttpResponse httpresponse; try { httpresponse = httpclient.execute(httppostreq,new BasicHttpContext()); String response1 = EntityUtils.toString(httpresponse.getEntity()); response1 = response1.replaceAll("\\\"", "\""); Log.i("coooooooooooooooooooooooooooooooooooooood", "Server Response : " + response1); //Log.i("coooooooooooooooooooooooooooooooooooooood", "Server Response : " + response1); }
private void initHttpRestClient() { httpClient = new DefaultHttpClient(); restHost = new HttpHost(kaaRestHost, kaaRestPort, "http"); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); authCache.put(restHost, basicAuth); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(kaaRestHost, kaaRestPort, AuthScope.ANY_REALM), new UsernamePasswordCredentials(kaaRestUser, kaaRestPassword)); httpContext = new BasicHttpContext(); httpContext.setAttribute(ClientContext.AUTH_CACHE, authCache); httpContext.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider); }
HttpParams myParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(myParams, 10000); HttpConnectionParams.setSoTimeout(myParams, 10000); HttpClient httpClient = new DefaultHttpClient(myParams); HttpContext localContext = new BasicHttpContext(); HttpPut httpPut = new HttpPut("http://localhost:8000/api/items/1/"); httpPut.setHeader("Accept", "application/json"); httpPut.setHeader("Content-Type", "application/json"); String data = // put your JSON object here tmp = new StringEntity(data, "UTF-8"); httpPut.setEntity(tmp); response = httpClient.execute(httpPut, localContext); Srting result = EntityUtils.toString(response.getEntity());
HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); String url = "http://maps.googleapis.com/maps/api/elevation/" + "xml?locations=" + String.valueOf(latitude) HttpGet httpGet = new HttpGet(url); try { HttpResponse response = httpClient.execute(httpGet, localContext); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent();
public void post(String url, List<NameValuePair> nameValuePairs) { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); try { MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); for(int index=0; index < nameValuePairs.size(); index++) { if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) { // If the key equals to "image", we use FileBody to transfer the data entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue()))); } else { // Normal string data entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue())); } } httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost, localContext); } catch (IOException e) { e.printStackTrace(); } }
InputStream is = getAssets().open("contacts.csv"); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(is); HttpContext localContext = new BasicHttpContext(); HttpResponse response =httpClient.execute(httpGet, localContext); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
se.setContentType("application/json"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(se); HttpResponse response = client.execute(post); InputStream inputStream = response.getEntity().getContent(); HttpResponse httpresponse = client.execute(post, new BasicHttpContext()); //For thread safe String response1 = EntityUtils.toString(httpresponse.getEntity()); response1 = response1.replaceAll("\\\"", "\""); Log.i("coooooooooooooooooooooooooooooooooooooood", "Server Response : " + response1); Log.i("coooooooooooooooooooooooooooooooooooooood", "Server Response : " + response1); }
httpPost.setHeader("Referer", Constants.SITE_URL); httpPost.setHeader("X-CSRFToken", CSRFTOKEN); DefaultHttpClient client = new DefaultHttpClient(); final BasicCookieStore cookieStore = new BasicCookieStore(); BasicClientCookie csrf_cookie = new BasicClientCookie("csrftoken", CSRFTOKEN); csrf_cookie.setDomain(Constants.CSRF_COOKIE_DOMAIN); cookieStore.addCookie(csrf_cookie); // Create local HTTP context - to store cookies HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpResponse response = client.execute(httpPost, localContext);
Log.d("url", url); try { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); HttpResponse response = httpClient.execute(httpPost, localContext); InputStream in = response.getEntity().getContent(); DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder();
HttpClient httpclient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); // HTTP parameters stores header etc. HttpParams params = new BasicHttpParams(); params.setParameter("http.protocol.handle-redirects",false); // Create a local instance of cookie store CookieStore cookieStore = new BasicCookieStore(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); // connect and receive HttpGet httpget = new HttpGet("http://localhost/web/redirect"); httpget.setParams(params); response = httpclient.execute(httpget, localContext); // obtain redirect target Header locationHeader = response.getFirstHeader("location"); if (locationHeader != null) { redirectLocation = locationHeader.getValue(); System.out.println("loaction: " + redirectLocation); } else { // The response is invalid and did not provide the new location for // the resource. Report an error or possibly handle the response // like a 404 Not Found error. }