List of usage examples for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient
public DefaultHttpClient()
From source file:com.hat.tools.HttpUtils.java
public static void SendRequest(String url, Handler handler) { HttpGet request = new HttpGet(url); HttpClient httpClient = new DefaultHttpClient(); byte[] data = null; InputStream in = null;/*w ww . j av a 2s . c o m*/ try { HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { ByteArrayOutputStream out = new ByteArrayOutputStream(); in = response.getEntity().getContent(); byte[] buf = new byte[1024]; int len = 0; while ((len = in.read(buf)) != -1) { out.write(buf, 0, len); } data = out.toByteArray(); Message msg = new Message(); msg.what = HTTP_FINISH; Bundle bundle = new Bundle(); bundle.putByteArray("data", data); msg.setData(bundle); handler.sendMessage(msg); out.close(); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (in != null) in.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:xj.property.ums.common.NetworkUitlity.java
public static MyMessage post(String url, String data) { // TODO Auto-generated method stub CommonUtil.printLog("ums", url); String returnContent = ""; MyMessage message = new MyMessage(); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); try {//from w w w . j av a2 s . c o m StringEntity se = new StringEntity(data, HTTP.UTF_8); CommonUtil.printLog("postdata", "content=" + data); se.setContentType("application/x-www-form-urlencoded"); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); int status = response.getStatusLine().getStatusCode(); CommonUtil.printLog("ums", status + ""); String returnXML = EntityUtils.toString(response.getEntity()); returnContent = URLDecoder.decode(returnXML); switch (status) { case 200: message.setFlag(true); message.setMsg(returnContent); break; default: Log.e("error", status + returnContent); message.setFlag(false); message.setMsg(returnContent); break; } } catch (Exception e) { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("err", e.toString()); returnContent = jsonObject.toString(); message.setFlag(false); message.setMsg(returnContent); } catch (JSONException e1) { e1.printStackTrace(); } } CommonUtil.printLog("UMSAGENT", message.getMsg()); return message; }
From source file:com.ecofactor.qa.automation.util.HttpUtil.java
/** * Gets the.// w w w . j a va 2 s . co m * @param url the url * @return the http response */ public static String get(String url, Map<String, String> params, int expectedStatus) { String content = null; HttpClient httpClient = new DefaultHttpClient(); HttpGet request = new HttpGet(url); URIBuilder builder = new URIBuilder(request.getURI()); Set<String> keys = params.keySet(); for (String key : keys) { builder.addParameter(key, params.get(key)); } HttpResponse response = null; try { request.setURI(builder.build()); DriverConfig.setLogString("URL " + request.getURI().toString(), true); response = httpClient.execute(request); content = IOUtils.toString(response.getEntity().getContent()); DriverConfig.setLogString("Content " + content, true); Assert.assertEquals(response.getStatusLine().getStatusCode(), expectedStatus); } catch (Exception e) { LOGGER.error(e.getMessage()); } finally { request.releaseConnection(); } return content; }
From source file:com.wbtech.ums.common.NetworkUitlity.java
public static MyMessage post(String url, String data) { // TODO Auto-generated method stub CommonUtil.printLog("ums", url); String returnContent = ""; MyMessage message = new MyMessage(); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); try {/*from w w w. j a va 2 s.co m*/ StringEntity se = new StringEntity("content=" + data, HTTP.UTF_8); CommonUtil.printLog("postdata", "content=" + data); se.setContentType("application/x-www-form-urlencoded"); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); int status = response.getStatusLine().getStatusCode(); CommonUtil.printLog("ums", status + ""); String returnXML = EntityUtils.toString(response.getEntity()); returnContent = URLDecoder.decode(returnXML); switch (status) { case 200: message.setFlag(true); message.setMsg(returnContent); break; default: Log.e("error", status + returnContent); message.setFlag(false); message.setMsg(returnContent); break; } } catch (Exception e) { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("err", e.toString()); returnContent = jsonObject.toString(); message.setFlag(false); message.setMsg(returnContent); } catch (JSONException e1) { e1.printStackTrace(); } } CommonUtil.printLog("UMSAGENT", message.getMsg()); return message; }
From source file:lets.code.project.conectividad.ConectivityClass.java
public static String getHTMLPage() { String str = "***"; try {/*w w w. ja v a 2 s.c o m*/ HttpClient hc = new DefaultHttpClient(); //HttpPost post = new HttpPost("http://www.yahoo.com"); HttpGet get = new HttpGet("http://barrapunto.com/index.xml"); HttpResponse rp = hc.execute(get); System.out.println("STATUS " + rp.getStatusLine().getStatusCode()); if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { str = EntityUtils.toString(rp.getEntity()); } } catch (IOException e) { e.printStackTrace(); } return str; }
From source file:com.mysoft.b2b.event.util.HttpUtil.java
/** * http???//from w w w. j ava 2s . com * @param uri ? * @param body ? */ public static Integer send(String uri, String body) { if (StringUtils.isEmpty(uri) || StringUtils.isEmpty(body)) return null; HttpClient client = new DefaultHttpClient(); try { HttpPost post = new HttpPost(uri + "/dealEvent.do"); post.addHeader("Content-Type", "application/json;charset=UTF-8"); HttpEntity entity = new StringEntity(body); post.setEntity(entity); HttpResponse response = client.execute(post); int code = response.getStatusLine().getStatusCode(); return code; } catch (Exception e) { logger.error("http???" + e); } return null; }
From source file:eu.musesproject.windowsclient.contextmonitoring.sensors.RESTController.java
public static Map<String, String> requestSensorInfo(String sensorType) throws IOException { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet("http://localhost:9000/api/" + sensorType); HttpResponse response = client.execute(request); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String responseData = ""; Gson gson = new Gson(); //ToDo:check for readLine function responseData = rd.readLine();/* www . j a va 2 s . c om*/ return gson.fromJson(responseData, new HashMap<String, String>().getClass()); }
From source file:com.matze5800.paupdater.JSONfunctions.java
public static JSONObject getJSONfromURL(String url) { InputStream is = null;/* w ww . j a v a 2 s. c om*/ String result = ""; JSONObject jArray = null; //http post try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch (Exception e) { Log.e("log_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("log_tag", "Error converting result " + e.toString()); } try { jArray = new JSONObject(result); } catch (JSONException e) { Log.e("log_tag", "Error parsing data " + e.toString()); } return jArray; }
From source file:com.iiordanov.wiktionarylookup.util.HttpUtil.java
public static String curl(String url) { String html = null;/*from ww w.ja v a2 s .c o m*/ try { HttpUriRequest get = new HttpGet(url); HttpResponse response = new DefaultHttpClient().execute(get); html = EntityUtils.toString(response.getEntity(), "utf-8"); } catch (ClientProtocolException e) { //Log.e (Global.TAG, "Could not curl", e); } catch (IOException e) { //Log.e (Global.TAG, "Could not curl", e); } return html; }
From source file:Tools.HttpClientUtil.java
public static String getRequest(List<KeyValuePair> paramList) { HttpClient client = new DefaultHttpClient(); String paramStr = Utils.getParamsURL(paramList); HttpGet request = new HttpGet(Constant.SERVER_URL + Constant.LOGIN_SERVLET + paramStr); System.out.println("$Total Request URL:" + Constant.SERVER_URL + Constant.LOGIN_SERVLET + paramStr); HttpResponse response;//ww w . j a va2 s. c o m StringBuilder sb = new StringBuilder(); try { response = client.execute(request); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = ""; while ((line = rd.readLine()) != null) { System.out.println("#" + response.getStatusLine()); sb.append(line); } } catch (IOException ex) { Logger.getLogger(HttpClientUtil.class.getName()).log(Level.SEVERE, null, ex); } return sb.toString(); }