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:biz.webgate.tools.urlfetcher.URLFetcher.java

public static PageAnalyse analyseURL(String url, int maxPictureHeight) throws FetcherException {
    PageAnalyse analyse = new PageAnalyse(url);
    HttpClient httpClient = null;//from w  w  w. ja v  a2s  .c  o  m
    try {
        httpClient = new DefaultHttpClient();
        ((DefaultHttpClient) httpClient).setRedirectStrategy(new DefaultRedirectStrategy());
        HttpGet httpGet = new HttpGet(url);
        httpGet.addHeader("Content-Type", "text/html; charset=utf-8");
        HttpResponse response = httpClient.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode == STATUS_OK) {
            HttpEntity entity = response.getEntity();
            parseContent(maxPictureHeight, analyse, entity.getContent());

        } else {
            throw new FetcherException("Response from WebSite is :" + statusCode);
        }
    } catch (IllegalStateException e) {
        throw new FetcherException(e);
    } catch (FetcherException e) {
        throw e;
    } catch (Exception e) {
        throw new FetcherException(e);
    } finally {
        if (httpClient != null) {
            httpClient.getConnectionManager().shutdown();
        }
    }
    return analyse;
}

From source file:pj.rozkladWKD.HttpClient.java

public static JSONObject SendHttpPost(List<NameValuePair> nameValuePairs)
        throws JSONException, ClientProtocolException, IOException {

    // Create a new HttpClient and Post Header  
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.mmaj.nazwa.pl/rozkladwkd/listener2.php");
    nameValuePairs.add(new BasicNameValuePair("app_ver", RozkladWKD.APP_VERSION));

    // Add your data  
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF_8"));

    // Execute HTTP Post Request  
    HttpResponse response = httpclient.execute(httppost);

    HttpEntity entity = response.getEntity();

    if (entity != null) {
        // Read the content stream

        InputStream instream = entity.getContent();
        Header contentEncoding = response.getFirstHeader("Content-Encoding");
        if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
            instream = new GZIPInputStream(instream);
        }/*from w  w  w  .  j  a  v a2 s .c o m*/

        // convert content stream to a String
        String resultString = convertStreamToString(instream);

        instream.close();
        //resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"

        // Transform the String into a JSONObject
        if (RozkladWKD.DEBUG_LOG) {
            Log.i(TAG, "result: " + resultString);
        }
        JSONObject jsonObjRecv = new JSONObject(resultString);
        // Raw DEBUG output of our received JSON object:
        if (RozkladWKD.DEBUG_LOG) {
            Log.i(TAG, "<jsonobject>\n" + jsonObjRecv.toString() + "\n</jsonobject>");
        }

        return jsonObjRecv;
    }

    return null;
}

From source file:org.liberty.android.fantastischmemo.downloader.DownloaderUtils.java

public static String downloadJSONString(String url) throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response;/*from   w ww . j  a v a 2  s.  c o m*/
    response = httpclient.execute(httpget);
    Log.i(TAG, "Response: " + response.getStatusLine().toString());
    HttpEntity entity = response.getEntity();

    if (entity == null) {
        throw new NullPointerException("Null entity error");
    }

    InputStream instream = entity.getContent();
    // Now convert stream to string 
    BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
    StringBuilder sb = new StringBuilder();
    String line = null;
    String result = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    result = sb.toString();

    return result;
}

From source file:com.zyf.bike.suzhou.utils.HttpOperation.java

/**
 * Http Get??//  ww  w.  j a v  a  2 s  . c  o m
 * @param url
 * @param testnum ?
 * @return
 */
public static byte[] doGetBase(String url, int testnum) {
    if (testnum < 0) {
        return null;
    }
    try {
        //HttpClient    
        HttpClient httpclient = new DefaultHttpClient();
        //POST  
        HttpGet get = new HttpGet(url);
        HttpResponse response = httpclient.execute(get);
        int code = response.getStatusLine().getStatusCode();
        if (code == HttpStatus.SC_OK) {
            //?
            return EntityUtils.toByteArray(response.getEntity());
        } else if (code == HttpStatus.SC_NOT_FOUND || code == HttpStatus.SC_FORBIDDEN
                || code == HttpStatus.SC_METHOD_NOT_ALLOWED) {
            //403 ?
            //404 ??
            //405 ??
            return null;
        } else {
            //??
            doGetBase(url, testnum--);
        }
    } catch (Exception e) {
        Logger.e(TAG, e.getMessage(), e);
    }
    return null;
}

From source file:WSpatern.uploadFile.java

public void Uploadfile(String token, String dirid, String userid, byte[] file) {
    try {//w  w w. j  av  a  2s.c o  m
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("https://documenta-dms.com/DMSWS/api/v1/file/upload/" + token + "/" + dirid
                + "/" + userid + "/" + Arrays.toString(file));
        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine()) != null) {

            System.out.println(line);
            // parseXML(line);

        }
    } catch (IOException ex) {
        Logger.getLogger(uploadFile.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.graylog2.systeminformation.Sender.java

public static void send(Map<String, Object> info, Core server) {
    HttpClient httpClient = new DefaultHttpClient();

    try {//from www .  j a  v  a 2  s  . co  m
        HttpPost request = new HttpPost(getTarget(server));

        List<NameValuePair> nameValuePairs = Lists.newArrayList();
        nameValuePairs.add(new BasicNameValuePair("systeminfo", JSONValue.toJSONString(info)));
        request.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpClient.execute(request);

        if (response.getStatusLine().getStatusCode() != 201) {
            LOG.warn("Response code for system statistics was not 201, but "
                    + response.getStatusLine().getStatusCode());
        }
    } catch (Exception e) {
        LOG.warn("Could not send system statistics.", e);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:fm.krui.kruifm.JSONFunctions.java

public static JSONObject getJSONObjectFromURL(String url) {
    InputStream is = null;/*from   www .  ja v a 2  s  .  c  om*/
    String result = "";
    JSONObject jArray = null;

    //http post
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httppost = new HttpGet(url);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    } catch (Exception e) {
        Log.e(TAG, "Error in http connection " + e.toString());
    }

    //convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        Log.e(TAG, "Error converting result " + e.toString());
    }

    try {

        jArray = new JSONObject(result);
    } catch (JSONException e) {
        Log.e(TAG, "Error parsing data " + e.toString());
    }

    return jArray;
}

From source file:fr.julienvermet.bugdroid.util.NetworkUtils.java

public static NetworkResult readJson(String url) {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    int statusCode = 0;
    try {// w w w.j  a  v  a2s. c  o m
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            reader.close();
        } else {
            Log.e(NetworkUtils.class.toString(), "Failed to download Json content");
        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {

    }

    return new NetworkResult(statusCode, builder.toString());
}

From source file:me.sandrin.xkcdwidget.GetJsonTask.java

@Override
protected String doInBackground(String... params) {

    String url = params[0];//from w w w. j a va  2 s  .  co  m
    String jsonText = null;
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        jsonText = reader.readLine();
    } catch (IOException e) {
        Log.e("XKCD", "Unable to get XKCD Json.", e);
    }

    return jsonText;
}

From source file:com.shafiq.mytwittle.urlservice.tweetmarker.TweetMarkerAPI.java

public static HttpResponse getRequest(String url, String debugName) {
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.addHeader("X-Auth-Service-Provider", TwitterApi.TWITTER_VERIFY_CREDENTIALS_JSON);
    request.addHeader("X-Verify-Credentials-Authorization",
            TwitterManager.get().generateTwitterVerifyCredentialsAuthorizationHeader());
    HttpResponse response = null;/*w w  w  . j  a va 2 s.c  o m*/
    try {
        request.setURI(new URI(url));
        // Log.d("tweetlanes url fetch", url);
        response = client.execute(request);
        // Log.d(TAG, debugName + " complete");
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return response;
}