Search in sources :

Example 31 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project ABPlayer by winkstu.

the class HttpUtil method httpGet.

public static String httpGet(String url) {
    System.out.println("httpGet" + url);
    HttpGet httpget = new HttpGet(url);
    String strResult = null;
    BasicHttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
    HttpConnectionParams.setSoTimeout(httpParams, 10000);
    HttpClient httpclient;
    try {
        httpclient = new DefaultHttpClient(httpParams);
        httpget.setHeader("Cookie", cookieName + "=" + cookieValue);
        HttpResponse response = httpclient.execute(httpget);
        System.out.println(response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() == 200) {
            strResult = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            // System.out.println(strResult);
            System.out.println("getFinish");
        }
    } catch (ConnectException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        System.out.println("Client");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("IO");
        e.printStackTrace();
    }
    return strResult;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ConnectException(java.net.ConnectException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 32 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project ABPlayer by winkstu.

the class HttpUtil method httpGetHost.

public static String httpGetHost(String url) {
    HttpGet httpget = new HttpGet(url);
    String strResult = "";
    BasicHttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
    HttpConnectionParams.setSoTimeout(httpParams, 5000);
    HttpClient httpclient;
    try {
        httpclient = new DefaultHttpClient(httpParams);
        HttpResponse response = httpclient.execute(httpget);
        System.out.println(response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() == 200) {
            strResult = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
        }
    } catch (ConnectException e) {
        e.printStackTrace();
        System.out.println("hosterror");
    } catch (ClientProtocolException e) {
        System.out.println("Client");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("IO");
        e.printStackTrace();
    }
    return strResult;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ConnectException(java.net.ConnectException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 33 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project Diaspora-Webclient by voidcode.

the class getPodlistTask method doInBackground.

@Override
protected String[] doInBackground(Void... params) {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    List<String> list = null;
    try {
        HttpGet httpGet = new HttpGet("http://podupti.me/api.php?key=4r45tg&format=json");
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int 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);
            }
        } else {
        //TODO  Notify User about failure
        //Log.e("Diaspora-WebClient", "Failed to download file");
        }
    } catch (ClientProtocolException e) {
        //TODO handle network unreachable exception here
        e.printStackTrace();
    } catch (IOException e) {
        //TODO handle json buggy feed
        e.printStackTrace();
    }
    //Parse the JSON Data
    try {
        JSONObject j = new JSONObject(builder.toString());
        JSONArray jr = j.getJSONArray("pods");
        //Log.i("Diaspora-WebClient","Number of entries " + jr.length());
        list = new ArrayList<String>();
        for (int i = 0; i < jr.length(); i++) {
            JSONObject jo = jr.getJSONObject(i);
            //Log.i("Diaspora-WebClient", jo.getString("domain"));
            String secure = jo.getString("secure");
            if (secure.equals("true"))
                list.add(jo.getString("domain"));
        }
    } catch (Exception e) {
        //TODO Handle Parsing errors here	
        e.printStackTrace();
    }
    return list.toArray(new String[list.size()]);
}
Also used : HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) JSONArray(org.json.JSONArray) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) StatusLine(org.apache.http.StatusLine) JSONObject(org.json.JSONObject) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) BufferedReader(java.io.BufferedReader)

Example 34 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project platform_external_apache-http by android.

the class AbstractHttpClient method execute.

// non-javadoc, see interface HttpClient
public final HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException {
    if (request == null) {
        throw new IllegalArgumentException("Request must not be null.");
    }
    // a null target may be acceptable, this depends on the route planner
    // a null context is acceptable, default context created below
    HttpContext execContext = null;
    RequestDirector director = null;
    // all shared objects that are potentially threading unsafe.
    synchronized (this) {
        HttpContext defaultContext = createHttpContext();
        if (context == null) {
            execContext = defaultContext;
        } else {
            execContext = new DefaultedHttpContext(context, defaultContext);
        }
        // Create a director for this request
        director = createClientRequestDirector(getRequestExecutor(), getConnectionManager(), getConnectionReuseStrategy(), getConnectionKeepAliveStrategy(), getRoutePlanner(), getHttpProcessor().copy(), getHttpRequestRetryHandler(), getRedirectHandler(), getTargetAuthenticationHandler(), getProxyAuthenticationHandler(), getUserTokenHandler(), determineParams(request));
    }
    try {
        return director.execute(target, request, execContext);
    } catch (HttpException httpException) {
        throw new ClientProtocolException(httpException);
    }
}
Also used : RequestDirector(org.apache.http.client.RequestDirector) DefaultedHttpContext(org.apache.http.protocol.DefaultedHttpContext) DefaultedHttpContext(org.apache.http.protocol.DefaultedHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpException(org.apache.http.HttpException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 35 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project lucene-solr by apache.

the class SolrCLI method getJson.

/**
   * Utility function for sending HTTP GET request to Solr and then doing some
   * validation of the response.
   */
@SuppressWarnings({ "unchecked" })
public static Map<String, Object> getJson(HttpClient httpClient, String getUrl) throws Exception {
    try {
        // ensure we're requesting JSON back from Solr
        HttpGet httpGet = new HttpGet(new URIBuilder(getUrl).setParameter(CommonParams.WT, CommonParams.JSON).build());
        // make the request and get back a parsed JSON object
        Map<String, Object> json = httpClient.execute(httpGet, new SolrResponseHandler(), HttpClientUtil.createNewHttpClientRequestContext());
        // check the response JSON from Solr to see if it is an error
        Long statusCode = asLong("/responseHeader/status", json);
        if (statusCode == -1) {
            throw new SolrServerException("Unable to determine outcome of GET request to: " + getUrl + "! Response: " + json);
        } else if (statusCode != 0) {
            String errMsg = asString("/error/msg", json);
            if (errMsg == null)
                errMsg = String.valueOf(json);
            throw new SolrServerException(errMsg);
        } else {
            // make sure no "failure" object in there either
            Object failureObj = json.get("failure");
            if (failureObj != null) {
                if (failureObj instanceof Map) {
                    Object err = ((Map) failureObj).get("");
                    if (err != null)
                        throw new SolrServerException(err.toString());
                }
                throw new SolrServerException(failureObj.toString());
            }
        }
        return json;
    } catch (ClientProtocolException cpe) {
        // Perhaps SolrClient should have thrown an exception itself??
        if (cpe.getMessage().contains("HTTP ERROR 401") || cpe.getMessage().contentEquals("HTTP ERROR 403")) {
            int code = cpe.getMessage().contains("HTTP ERROR 401") ? 401 : 403;
            throw new SolrException(SolrException.ErrorCode.getErrorCode(code), "Solr requires authentication for " + getUrl + ". Please supply valid credentials. HTTP code=" + code);
        } else {
            throw cpe;
        }
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) SolrServerException(org.apache.solr.client.solrj.SolrServerException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) SolrException(org.apache.solr.common.SolrException) URIBuilder(org.apache.http.client.utils.URIBuilder) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Aggregations

ClientProtocolException (org.apache.http.client.ClientProtocolException)92 IOException (java.io.IOException)80 HttpResponse (org.apache.http.HttpResponse)49 HttpPost (org.apache.http.client.methods.HttpPost)37 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)30 HttpClient (org.apache.http.client.HttpClient)29 HttpGet (org.apache.http.client.methods.HttpGet)25 HttpEntity (org.apache.http.HttpEntity)23 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)20 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)19 InputStreamReader (java.io.InputStreamReader)18 ArrayList (java.util.ArrayList)18 UnsupportedEncodingException (java.io.UnsupportedEncodingException)17 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)14 BufferedReader (java.io.BufferedReader)13 InputStream (java.io.InputStream)13 NameValuePair (org.apache.http.NameValuePair)13 StringEntity (org.apache.http.entity.StringEntity)12 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)11 URI (java.net.URI)10