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;
}
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;
}
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()]);
}
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);
}
}
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;
}
}
}
Aggregations