List of usage examples for org.apache.http.entity StringEntity setContentType
public void setContentType(Header header)
From source file:test.java.ecplugins.weblogic.TestUtils.java
/** * Creates a new workspace. If the workspace already exists,It continues. * //from w w w .j a va 2 s . c o m */ static void createCommanderWorkspace(String workspaceName) throws Exception { props = getProperties(); HttpClient httpClient = new DefaultHttpClient(); JSONObject jo = new JSONObject(); try { String url = "http://" + props.getProperty(StringConstants.COMMANDER_SERVER) + ":8000/rest/v1.0/workspaces/"; String encoding = new String( org.apache.commons.codec.binary.Base64.encodeBase64(org.apache.commons.codec.binary.StringUtils .getBytesUtf8(props.getProperty(StringConstants.COMMANDER_USER) + ":" + props.getProperty(StringConstants.COMMANDER_PASSWORD)))); HttpPost httpPostRequest = new HttpPost(url); jo.put("workspaceName", workspaceName); jo.put("description", workspaceName); jo.put("agentDrivePath", "C:/Program Files/Electric Cloud/ElectricCommander"); jo.put("agentUncPath", "C:/Program Files/Electric Cloud/ElectricCommander"); jo.put("agentUnixPath", "/opt/electriccloud/electriccommander"); jo.put("local", true); StringEntity input = new StringEntity(jo.toString()); input.setContentType("application/json"); httpPostRequest.setEntity(input); httpPostRequest.setHeader("Authorization", "Basic " + encoding); HttpResponse httpResponse = httpClient.execute(httpPostRequest); if (httpResponse.getStatusLine().getStatusCode() == 409) { System.out.println("Commander workspace already exists.Continuing...."); } else if (httpResponse.getStatusLine().getStatusCode() >= 400) { throw new RuntimeException( "Failed to create commander workspace " + httpResponse.getStatusLine().getStatusCode() + "-" + httpResponse.getStatusLine().getReasonPhrase()); } } finally { httpClient.getConnectionManager().shutdown(); } }
From source file:test.java.ecplugins.weblogic.TestUtils.java
/** * //from ww w . j ava 2 s. com * @return */ static void createCommanderResource(String resourceName, String workspaceName, String resourceIP) throws Exception { props = getProperties(); HttpClient httpClient = new DefaultHttpClient(); JSONObject jo = new JSONObject(); try { HttpPost httpPostRequest = new HttpPost( "http://" + props.getProperty(StringConstants.COMMANDER_SERVER) + ":8000/rest/v1.0/resources/"); String encoding = new String( org.apache.commons.codec.binary.Base64.encodeBase64(org.apache.commons.codec.binary.StringUtils .getBytesUtf8(props.getProperty(StringConstants.COMMANDER_USER) + ":" + props.getProperty(StringConstants.COMMANDER_PASSWORD)))); jo.put("resourceName", resourceName); jo.put("description", "Resource created for test automation"); jo.put("hostName", resourceIP); jo.put("port", StringConstants.EC_AGENT_PORT); jo.put("workspaceName", workspaceName); jo.put("pools", "default"); jo.put("local", true); StringEntity input = new StringEntity(jo.toString()); input.setContentType("application/json"); httpPostRequest.setEntity(input); httpPostRequest.setHeader("Authorization", "Basic " + encoding); HttpResponse httpResponse = httpClient.execute(httpPostRequest); if (httpResponse.getStatusLine().getStatusCode() == 409) { System.out.println("Commander resource already exists.Continuing...."); } else if (httpResponse.getStatusLine().getStatusCode() >= 400) { throw new RuntimeException( "Failed to create commander workspace " + httpResponse.getStatusLine().getStatusCode() + "-" + httpResponse.getStatusLine().getReasonPhrase()); } } finally { httpClient.getConnectionManager().shutdown(); } }
From source file:co.aurasphere.botmill.skype.util.network.NetworkUtils.java
/** * To string entity.//w ww. j a v a 2 s . com * * @param object the object * @return the string entity */ private static StringEntity toStringEntity(Object object) { StringEntity input = null; try { String json = JsonUtils.toJson(object); input = new StringEntity(json); input.setContentType("application/json"); logger.debug("Request: {}", inputStreamToString(input.getContent())); } catch (Exception e) { logger.error("Error during JSON message creation: ", e); } return input; }
From source file:com.wbtech.ums.NetworkUtil.java
public static MyMessage Post(String url, String data) { CobubLog.d(UmsConstants.LOG_TAG, NetworkUtil.class, "URL = " + url); CobubLog.d(UmsConstants.LOG_TAG, NetworkUtil.class, "LENGTH:" + data.length() + " *Data = " + data + "*"); if (!hasInitSSL && UmsConstants.SDK_SECURITY_LEVEL.equals("2")) { initSSL();// w w w . j av a2 s . c o m hasInitSSL = true; } BasicHttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT); HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT); DefaultHttpClient httpclient = null; /*SDK??: * 1,CPOS:???sslSDK_SSL=true;??dn(?dnSDK_HTTPS_DNnone) * 2,HTTPS???????https? * 3,http */ if (UmsConstants.SDK_SECURITY_LEVEL.equals("2")) { httpclient = new DefaultHttpClient(httpParams); // cpos with dn check if (!UmsConstants.SDK_HTTPS_DN.equals("none")) { SSLSocketFactory mysf = null; try { mysf = new CposSSLSocketFactory(); if (serverUrl == null) { serverUrl = new URL(url); serverPort = ((serverUrl.getPort() == -1) ? serverUrl.getDefaultPort() : serverUrl.getPort()); } httpclient.getConnectionManager().getSchemeRegistry() .register(new Scheme(serverUrl.getProtocol(), mysf, serverPort)); } catch (Exception e) { CobubLog.d(UmsConstants.LOG_TAG, NetworkUtil.class, e.toString()); } } } else if (UmsConstants.SDK_SECURITY_LEVEL.equals("1") && url.toLowerCase().startsWith("https")) { // for https with company cert if (serverPort < 0) { serverPort = getPort(); } CobubLog.d(UmsConstants.LOG_TAG, NetworkUtil.class, "InitSSL port is:" + serverPort); SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("https", SSLCustomSocketFactory.getSocketFactory(), serverPort)); ClientConnectionManager connMgr = new ThreadSafeClientConnManager(httpParams, schReg); httpclient = new DefaultHttpClient(connMgr, httpParams); } else { httpclient = new DefaultHttpClient(httpParams); } processCookieRejected(httpclient); MyMessage message = new MyMessage(); try { HttpPost httppost = new HttpPost(url); StringEntity se = new StringEntity("content=" + URLEncoder.encode(data), HTTP.UTF_8); se.setContentType("application/x-www-form-urlencoded"); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); CobubLog.d(UmsConstants.LOG_TAG, NetworkUtil.class, "Status code=" + response.getStatusLine().getStatusCode()); String returnXML = EntityUtils.toString(response.getEntity()); int status = response.getStatusLine().getStatusCode(); String returnContent = URLDecoder.decode(returnXML, "UTF-8"); CobubLog.d(UmsConstants.LOG_TAG, NetworkUtil.class, "returnString = " + returnContent); //TODO:???200okjson??????????flag<0 //?flag??? switch (status) { case 200: message.setSuccess(isJson(returnContent)); message.setMsg(returnContent); break; default: Log.e("error", status + returnContent); message.setSuccess(false); message.setMsg(returnContent); break; } } catch (Exception e) { message.setSuccess(false); message.setMsg(e.toString()); } return message; }
From source file:ecplugins.s3.TestUtils.java
/** * callRunProcedure//from w ww . j a v a2 s . c o m * * @param jo * @return the jobId of the job launched by runProcedure */ public static String callRunProcedure(JSONObject jo) throws Exception { HttpClient httpClient = new DefaultHttpClient(); JSONObject result = null; try { HttpPost httpPostRequest = new HttpPost("http://" + props.getProperty(StringConstants.COMMANDER_USER) + ":" + props.getProperty(StringConstants.COMMANDER_PASSWORD) + "@" + StringConstants.COMMANDER_SERVER + ":8000/rest/v1.0/jobs?request=runProcedure"); StringEntity input = new StringEntity(jo.toString()); input.setContentType("application/json"); httpPostRequest.setEntity(input); HttpResponse httpResponse = httpClient.execute(httpPostRequest); result = new JSONObject(EntityUtils.toString(httpResponse.getEntity())); return result.getString("jobId"); } finally { httpClient.getConnectionManager().shutdown(); } }
From source file:no.ntnu.wifimanager.ServerUtilities.java
public static void postJSON(String url, String JSONString) { HttpPost httpPost = new HttpPost(url); StringEntity se; try {//from www . j a v a 2s .c o m se = new StringEntity(JSONString); se.setContentType("application/json"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); //httpPost.setHeader("Accept", "application/json"); httpPost.setEntity(se); HttpResponse resp = new DefaultHttpClient().execute(httpPost); } catch (UnsupportedEncodingException 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(); } }
From source file:com.rukman.emde.smsgroups.client.NetworkUtilities.java
private static String doPostRequest(String url, String authToken, JSONObject parameters) throws IOException, JSONException { final HttpPost post = new HttpPost(url); if (!TextUtils.isEmpty(authToken)) { post.addHeader(PARAM_AUTHORIZATION, authToken); }/*from w w w . j a va 2s . c om*/ final StringEntity entity = new StringEntity(parameters.toString()); entity.setContentType("application/json"); post.addHeader(entity.getContentType()); post.addHeader(new BasicHeader(GMS_HEADER_API_KEY, GMS_ANDROID_API_KEY)); post.setEntity(entity); return executeHttpRequest(post); }
From source file:com.wbtech.dao.NetworkUitlity.java
public static MyMessage post(String url, String data) { // TODO Auto-generated method stub String returnContent = ""; MyMessage message = new MyMessage(); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); try {//from ww w . j a v a 2 s . c o m StringEntity se = new StringEntity("content=" + data, HTTP.UTF_8); Log.d("postdata", "content=" + data); se.setContentType("application/x-www-form-urlencoded"); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); int status = response.getStatusLine().getStatusCode(); 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(); if (e.getMessage().equalsIgnoreCase("no route to host")) { try { jsonObject.put("err", "??,???"); returnContent = jsonObject.toString(); message.setFlag(false); message.setMsg(returnContent); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else if (e.getMessage().equalsIgnoreCase("network unreachable") || e.getMessage().equalsIgnoreCase("www.cobub.com")) { try { jsonObject.put("err", ""); returnContent = jsonObject.toString(); message.setFlag(false); message.setMsg(returnContent); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { try { jsonObject.put("err", ""); returnContent = jsonObject.toString(); message.setFlag(false); message.setMsg(returnContent); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } return message; }
From source file:co.aurasphere.botmill.util.NetworkUtils.java
/** * Post json message./*from w ww . j av a 2 s . c o m*/ * * @param jsonData the json data * @return the string */ public static String postTrainingString(String jsonData) { StringEntity stringEntity; try { stringEntity = new StringEntity(jsonData); stringEntity.setContentType("application/json"); HttpPost post = new HttpPost( RasaBotMillContext.getRasaConfig().toString() + NetworkConstants.TRAIN_EP + concatToken()); post.setEntity(stringEntity); return send(post); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:org.openjena.riot.web.HttpOp.java
/** POST a string, expect a response body.*/ public static void execHttpPost(String url, String contentType, String content, String acceptType, Map<String, HttpResponseHandler> handlers) { StringEntity e = null; try {//from w ww.j a v a 2 s. c om e = new StringEntity(content, "UTF-8"); e.setContentType(contentType); execHttpPost(url, e, acceptType, handlers); } catch (UnsupportedEncodingException e1) { throw new ARQInternalErrorException("Platform does not support required UTF-8"); } finally { closeEntity(e); } }