Example usage for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient

List of usage examples for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient.

Prototype

public DefaultHttpClient() 

Source Link

Usage

From source file:Ahmedabad.SendingNum.java

public SendingNum() throws ClientProtocolException, IOException {

    HttpClient http = new DefaultHttpClient();
    HttpPost post = new HttpPost(
            "http://gmail.com/index.php?option=com_docman&task=license_result&gid=3933&bid=3933&Itemid=429");

    List<NameValuePair> lot = new ArrayList<NameValuePair>();
    lot.add(new BasicNameValuePair("option", "com_docman"));
    lot.add(new BasicNameValuePair("task", "license_result"));
    lot.add(new BasicNameValuePair("gid", "3933"));
    lot.add(new BasicNameValuePair("bid", "3933"));
    lot.add(new BasicNameValuePair("Itemid", "429"));

    post.setEntity(new UrlEncodedFormEntity(lot));
    HttpResponse str = null;/*from  w  ww  .  ja  v a 2 s .com*/

    try {
        str = http.execute(post);

        InputStream in = str.getEntity().getContent();

        OutputStream outputStream = new FileOutputStream(
                new File("C:\\Users\\AbhinavPatluri\\Desktop\\Excelsheet.csv"));

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = in.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }

    } catch (Exception e) {

        e.printStackTrace();
    }

}

From source file:com.datatorrent.demos.mrmonitor.MRUtil.java

/**
 * This method returns the response content for a given url
 * @param url//w  w w.jav a2s  .  c o  m
 * @return
 */
public static String getJsonForURL(String url) {
    HttpClient httpclient = new DefaultHttpClient();
    logger.debug(url);
    try {

        HttpGet httpget = new HttpGet(url);

        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody;
        try {
            responseBody = httpclient.execute(httpget, responseHandler);

        } catch (ClientProtocolException e) {
            logger.debug(e.getMessage());
            return null;

        } catch (IOException e) {
            logger.debug(e.getMessage());
            return null;
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return null;
        }
        return responseBody.trim();
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:de.hybris.platform.integration.cis.payment.strategies.impl.CisPaymentIntegrationTestHelper.java

public static Map<String, String> createNewProfile(final String cybersourceUrl,
        final List<BasicNameValuePair> formData) throws Exception // NO PMD
{
    final DefaultHttpClient client = new DefaultHttpClient();
    final HttpPost postRequest = new HttpPost(cybersourceUrl);
    postRequest.getParams().setBooleanParameter("http.protocol.handle-redirects", true);
    postRequest.setEntity(new UrlEncodedFormEntity(formData, "UTF-8"));
    // Execute HTTP Post Request
    final HttpResponse response = client.execute(postRequest);
    final Map<String, String> responseParams = new HashMap<String, String>();
    BufferedReader bufferedReader = null;
    try {//from w  ww.ja  v  a 2s  . c o m
        bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));

        while (bufferedReader.ready()) {
            final String currentLine = bufferedReader.readLine();
            if (currentLine.contains("value=\"") && currentLine.contains("name=\"")) {
                final String[] splittedLine = currentLine.split("name=\"");
                final String key = splittedLine[1].split("value=\"")[0].replace("\"", "").replace(">", "")
                        .trim();
                final String value = splittedLine[1].split("value=\"")[1].replace("\"", "").replace(">", "")
                        .trim();
                responseParams.put(key, value);
            }
        }
    } finally {
        IOUtils.closeQuietly(bufferedReader);
    }

    return responseParams;
}

From source file:org.ojbc.util.helper.HttpUtils.java

/**
 * Send the specified payload to the specified http endpoint via POST.
 * @param payload/*from  w ww .  ja v  a2  s.c o m*/
 * @param url
 * @return the http response
 * @throws Exception
 */
public static String post(String payload, String url) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    post.setEntity(new StringEntity(payload, Consts.UTF_8));
    HttpResponse response = client.execute(post);
    HttpEntity reply = response.getEntity();
    StringWriter sw = new StringWriter();
    BufferedReader isr = new BufferedReader(new InputStreamReader(reply.getContent()));
    String line;
    while ((line = isr.readLine()) != null) {
        sw.append(line);
    }
    sw.close();
    return sw.toString();
}

From source file:JsonParser.java

public JSONObject getJSONFromUrl(String url) {
    // make HTTP request
    try {//from w  w w. ja v  a  2  s  .c  o  m
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream inputStream = httpEntity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);

        JSONObject jsonObject = createJsonObject(reader);
        inputStream.close();

        return jsonObject;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:language_engine.HttpUtils.java

public static String doHttpGet(String url, Header[] headers) {
    try {/*  w  w  w  .  j a  va  2 s .co m*/
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeaders(headers);
        HttpResponse resp = httpClient.execute(httpGet);

        return StreamUtil.readText(resp.getEntity().getContent(), "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.unitedcoders.android.gpodroid.tools.Tools.java

public static String getImageUrlFromFeed(Context context, String url) {

    try {// w w w. j  a  v a  2  s .  co  m
        Document doc;
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        HttpResponse response = client.execute(request);
        doc = builder.parse(response.getEntity().getContent());
        //            return doc;

        // get Image
        NodeList item = doc.getElementsByTagName("channel");
        Element el = (Element) item.item(0);

        String imageUrl;
        NodeList imagNode = el.getElementsByTagName("image");
        if (imagNode != null) {
            Element ima = (Element) imagNode.item(0);
            if (ima != null) {
                NodeList urlNode = ima.getElementsByTagName("url");
                if (urlNode == null || urlNode.getLength() < 1)
                    imageUrl = null;
                else
                    imageUrl = urlNode.item(0).getFirstChild().getNodeValue();
            } else
                imageUrl = null;
        } else
            imageUrl = null;

        return imageUrl;

    } catch (IOException e) {
        return null; // The network probably died, just return null
    } catch (SAXException e) {
        // Problem parsing the XML, log and return nothing
        Log.e("NCRSS", "Error parsing XML", e);
        return null;
    } catch (Exception e) {
        // Anything else was probably another network problem, fail silently
        return null;
    }

}

From source file:au.org.ala.bhl.service.WebServiceHelper.java

/**
 * Performs a GET on the specified URI, and expects that the output is well formed JSON. The output is parsed into a JsonNode for consumption
 * /*from   ww  w. j  a va  2 s  . c  om*/
 * @param uri
 * @return
 * @throws IOException
 */
public static JsonNode getJSON(String uri) throws IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(uri);
    httpget.setHeader("Accept", "application/json");
    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        @SuppressWarnings("unchecked")
        List<String> lines = IOUtils.readLines(instream, "utf-8");
        String text = StringUtils.join(lines, "\n");
        try {
            JsonNode root = new ObjectMapper().readValue(text, JsonNode.class);
            return root;
        } catch (Exception ex) {
            log("Error parsing results for request: %s\n%s\n", uri, text);
            ex.printStackTrace();
        }

    }
    return null;
}

From source file:org.eclipse.json.http.HttpHelper.java

public static JsonValue makeRequest(String url) throws IOException {
    long startTime = 0;
    HttpClient httpClient = new DefaultHttpClient();
    try {//from w  w w.ja v a2s  .  co  m
        // Post JSON Tern doc
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = httpClient.execute(httpGet);
        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 new
             * TernException(message);
             */
        }

        try {
            JsonValue response = JsonValue.readFrom(new InputStreamReader(in));
            return response;
        } catch (ParseException e) {
            throw new IOException(e);
        }
    } catch (Exception e) {
        if (e instanceof IOException) {
            throw (IOException) e;
        }
        throw new IOException(e);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:com.alta189.cyborg.commandkit.util.HttpUtil.java

public static String readURL(String url) {
    HttpGet request = new HttpGet(url);
    HttpClient httpClient = new DefaultHttpClient();
    try {//from  w w  w .j  a v  a 2  s  .c  o  m
        HttpResponse response = httpClient.execute(request);
        return EntityUtils.toString(response.getEntity());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}