List of usage examples for org.apache.http.client HttpClient execute
HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException;
From source file:com.nuance.expertassistant.HTTPConnection.java
public static JSONObject sendGet(String getURL) throws Exception { String url = getURL;/*from ww w .j a v a 2 s.c o m*/ HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(url); // add request header request.addHeader("User-Agent", USER_AGENT); System.out.println(" The URL is :" + url); HttpResponse response = client.execute(request); System.out.println("\nSending 'GET' request to URL : " + url); System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder builder = new StringBuilder(); for (String line = null; (line = rd.readLine()) != null;) { builder.append(line).append("\n"); } System.out.print("JSON TEXT : " + builder.toString()); String jsonText = builder.toString(); if (jsonText.startsWith("[")) { jsonText = jsonText.substring(1, jsonText.length()); } if (jsonText.endsWith("]")) { jsonText = jsonText.substring(0, jsonText.length() - 1); } JSONObject object = new JSONObject(jsonText); return object; }
From source file:util.Slack.java
private static void sendMsg(StringEntity jsonMsg, String url) throws IOException { HttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead try {/* ww w . j av a2 s . c om*/ HttpPost request = new HttpPost(url); request.addHeader("content-type", "application/json; charset=UTF-8"); request.setEntity(jsonMsg); HttpResponse response = httpClient.execute(request); System.out.println(response); } finally { httpClient.getConnectionManager().shutdown(); //Deprecated } }
From source file:com.baidu.cafe.local.NetworkUtils.java
/** * download via a url//w w w . ja v a2 s. c om * * @param url * @param outputStream * openFileOutput("networktester.download", * Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE) */ public static void httpDownload(String url, OutputStream outputStream) { try { HttpClient httpClient = new DefaultHttpClient(); HttpClientParams.setCookiePolicy(httpClient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY); httpClient.execute(new HttpGet(url)).getEntity().writeTo(outputStream); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.musevisions.android.SudokuSolver.HttpPostUtils.java
static public String send(HttpPostSettings settings, List<NameValuePair> values) { try {/*from w w w. j a v a2s . com*/ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(settings.getPostTarget()); httppost.setEntity(new UrlEncodedFormEntity(values)); HttpResponse response = httpclient.execute(httppost); return EntityUtils.toString(response.getEntity()); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:com.groupon.odo.tests.HttpUtils.java
public static String doProxyGet(String url, BasicNameValuePair[] data) throws Exception { String fullUrl = url;//from w w w . j ava2s .co m if (data != null) { if (data.length > 0) { fullUrl += "?"; } for (BasicNameValuePair bnvp : data) { fullUrl += bnvp.getName() + "=" + uriEncode(bnvp.getValue()) + "&"; } } HttpGet get = new HttpGet(fullUrl); int port = Utils.getSystemPort(Constants.SYS_HTTP_PORT); HttpHost proxy = new HttpHost("localhost", port); HttpClient client = new org.apache.http.impl.client.DefaultHttpClient(); client.getParams().setParameter(org.apache.http.conn.params.ConnRoutePNames.DEFAULT_PROXY, proxy); HttpResponse response = client.execute(get); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String accumulator = ""; String line = ""; Boolean firstLine = true; while ((line = rd.readLine()) != null) { accumulator += line; if (!firstLine) { accumulator += "\n"; } else { firstLine = false; } } return accumulator; }
From source file:com.websqrd.catbot.scraping.HttpHandler.java
/** * ? .//from ww w .j ava 2 s . c o m */ public static String executeGet2(HttpClient httpclient, String url, String encoding, String[] a) throws IOException { String type = "text"; HttpGet httget = new HttpGet(url); HttpResponse response = httpclient.execute(httget); HttpEntity entity = response.getEntity(); Header header = entity.getContentType(); if (header != null) { type = header.getValue().toLowerCase(); } //InputStream to String InputStream is = entity.getContent(); Writer writer = new StringWriter(); char[] buffer = new char[1024]; try { Reader reader = new BufferedReader(new InputStreamReader(is, encoding)); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } finally { is.close(); } a[0] = writer.toString(); return type; }
From source file:org.roman.findme.RegistrationIntentService.java
public static void makeResponse(List<NameValuePair> nameValuePairs) { String url = "http://task-master.zzz.com.ua/server.php"; Log.d(TAG, "sendRequest"); try {// w ww . ja v a 2s .c om HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); InputStream in = response.getEntity().getContent(); StringBuilder stringbuilder = new StringBuilder(); BufferedReader bfrd = new BufferedReader(new InputStreamReader(in), 1024); String line; while ((line = bfrd.readLine()) != null) stringbuilder.append(line); String downloadedString = stringbuilder.toString(); Log.d(TAG, downloadedString); } catch (Exception e) { Log.d(TAG, e.toString()); } }
From source file:tern.server.nodejs.NodejsTernHelper.java
public static JsonObject makeRequest(String baseURL, TernDoc doc, boolean silent, List<IInterceptor> interceptors, ITernServer server) throws IOException, TernException { TernQuery query = doc.getQuery();//from w ww.ja v a2s . c o m String methodName = query != null ? query.getLabel() : ""; long startTime = 0; if (interceptors != null) { startTime = System.nanoTime(); for (IInterceptor interceptor : interceptors) { interceptor.handleRequest(doc, server, methodName); } } HttpClient httpClient = new DefaultHttpClient(); try { // Post JSON Tern doc HttpPost httpPost = createHttpPost(baseURL, doc); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity entity = httpResponse.getEntity(); InputStream in = entity.getContent(); // Check the status StatusLine statusLine = httpResponse.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode != HttpStatus.SC_OK) { // node.js server throws error String message = IOUtils.toString(in); if (StringUtils.isEmpty(message)) { throw new TernException(statusLine.toString()); } throw TernExceptionFactory.create(message); } try { JsonObject response = (JsonObject) Json.parse(new InputStreamReader(in, UTF_8)); if (interceptors != null) { for (IInterceptor interceptor : interceptors) { interceptor.handleResponse(response, server, methodName, getElapsedTimeInMs(startTime)); } } return response; } catch (ParseException e) { throw new IOException(e); } } catch (Exception e) { if (interceptors != null) { for (IInterceptor interceptor : interceptors) { interceptor.handleError(e, server, methodName, getElapsedTimeInMs(startTime)); } } if (e instanceof IOException) { throw (IOException) e; } if (e instanceof TernException) { throw (TernException) e; } throw new TernException(e); } finally { httpClient.getConnectionManager().shutdown(); } }
From source file:com.rylinaux.plugman.util.BukGetUtil.java
/** * Get the plugin data for a certain version. * * @param slug the plugin slug./*from ww w.j a v a 2s .c om*/ * @return the JSON encoded data. */ public static JSONObject getPluginData(String slug, String version) { HttpClient client = HttpClients.createMinimal(); HttpGet get = new HttpGet(API_BASE_URL + "plugins/bukkit/" + slug + "/" + version); try { HttpResponse response = client.execute(get); String body = IOUtils.toString(response.getEntity().getContent()); return (JSONObject) JSONValue.parse(body); } catch (IOException e) { } return null; }
From source file:org.jboss.as.test.clustering.cluster.ejb.xpc.StatefulWithXPCFailoverTestCase.java
private static String executeUrlWithAnswer(HttpClient client, URI url, String message) throws IOException { HttpResponse response = client.execute(new HttpGet(url)); try {/*from ww w. j av a2 s. c o m*/ assertEquals(200, response.getStatusLine().getStatusCode()); Header header = response.getFirstHeader("answer"); Assert.assertNotNull(message, header); return header.getValue(); } finally { org.apache.http.client.utils.HttpClientUtils.closeQuietly(response); } }