List of usage examples for org.apache.http.entity StringEntity StringEntity
public StringEntity(String str) throws UnsupportedEncodingException
From source file:com.sepgil.ral.CreateNodeTask.java
@Override public HttpRequestBase getHttpRequest(String url) { HttpPut put = new HttpPut(url); try {// w w w . j a va 2 s .com StringEntity enity = new StringEntity(mNode.getJSON()); put.setEntity(enity); } catch (JSONException e) { mListener.onError(ErrorState.ERROR_PARSE); } catch (UnsupportedEncodingException e) { mListener.onError(ErrorState.ERROR_PARSE); } return put; }
From source file:org.ounl.lifelonglearninghub.learntracker.gis.ou.db.ws.ActivityWSPostAsyncTask.java
private int executeCheckIn(ActivityDO a) { String json = new GsonBuilder().create().toJson(a, ActivityDO.class); String uri = Session.getSingleInstance().getWSPath() + "/_ah/api/activityendpoint/v1/activity"; try {//from w w w . j a v a 2 s.c o m HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(new StringEntity(json)); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); new DefaultHttpClient().execute(httpPost); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return WSConstants.POST_RESPONSE_KO; } catch (ClientProtocolException e) { e.printStackTrace(); return WSConstants.POST_RESPONSE_KO; } catch (IOException e) { e.printStackTrace(); return WSConstants.POST_RESPONSE_KO; } return WSConstants.POST_RESPONSE_OK; }
From source file:nl.surfnet.sab.HttpClientTransportTest.java
@Before public void setUp() throws Exception { server = new LocalTestServer(null, null); server.register("/test", new HttpRequestHandler() { @Override/*w ww. j av a2s . co m*/ public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { response.setEntity(new StringEntity("This is the response")); response.setStatusCode(200); } }); server.start(); endPoint = URI.create("http:/" + server.getServiceAddress().toString() + "/test"); credentials = new UsernamePasswordCredentials("theuser", "thepass"); transport = new HttpClientTransport(credentials, credentials, endPoint, endPoint); }
From source file:org.netvogue.server.webmvc.pusher.HttpClientPusherTransport.java
@Override public PusherResponse fetch(URL url, String jsonData) throws PusherTransportException { PusherResponse response = new PusherResponse(); try {/*from w ww . j av a2s . c om*/ HttpPost post = new HttpPost(url.toURI()); post.addHeader("Content-Type", "application/json"); post.setEntity(new StringEntity(jsonData)); // executr post request HttpResponse httpResponse = this.httpClient.execute(post); // get the content response.setContent(EntityUtils.toByteArray(httpResponse.getEntity())); // extract and set headers response.setHeaders(this.extractHeaders(httpResponse)); // set http status response.setResponseCode(httpResponse.getStatusLine().getStatusCode()); } catch (URISyntaxException ex) { throw new PusherTransportException("bad uri syntax", ex); } catch (ClientProtocolException ex) { throw new PusherTransportException("bad client protocol", ex); } catch (IOException ex) { throw new PusherTransportException("i/o failed", ex); } return response; }
From source file:com.strato.hidrive.api.connection.httpgateway.request.JSONPutRequest.java
protected HttpRequestBase createHttpRequest(String requestUri, HttpRequestParamsVisitor<?> visitor) throws UnsupportedEncodingException { HttpPut httpPost = new HttpPut(requestUri); String jsonString = ""; if (jsonObject != null) { jsonString = jsonObject.toString(); } else if (jsonArray != null) { jsonString = jsonArray.toString(); }// w w w .j ava 2 s. c om StringEntity stringEntity = new StringEntity(jsonString); stringEntity.setContentType(CONTENT_TYPE_APPLICATION_JSON); httpPost.setEntity(stringEntity); return httpPost; }
From source file:ecplugins.websphere.TestUtils.java
public static void setDefaultResourceAndWorkspace() throws Exception { if (!isResourceSetSuccessfully) { HttpClient httpClient = new DefaultHttpClient(); JSONObject jo = new JSONObject(); jo.put("projectName", "EC-WebSphere-" + StringConstants.PLUGIN_VERSION); jo.put("resourceName", StringConstants.RESOURCE_NAME); jo.put("workspaceName", StringConstants.WORKSPACE_NAME); HttpPut httpPutRequest = new HttpPut("http://" + props.getProperty(StringConstants.COMMANDER_USER) + ":" + props.getProperty(StringConstants.COMMANDER_PASSWORD) + "@" + StringConstants.COMMANDER_SERVER + ":8000/rest/v1.0/projects/" + "EC-WebSphere-" + StringConstants.PLUGIN_VERSION); StringEntity input = new StringEntity(jo.toString()); input.setContentType("application/json"); httpPutRequest.setEntity(input); HttpResponse httpResponse = httpClient.execute(httpPutRequest); if (httpResponse.getStatusLine().getStatusCode() >= 400) { throw new RuntimeException("Failed to set default resource " + StringConstants.RESOURCE_NAME + " to project " + "EC-WebSphere-" + StringConstants.PLUGIN_VERSION); }/*www . j a va 2 s.c o m*/ System.out.println("Set the default resource as " + StringConstants.RESOURCE_NAME + " and default workspace as " + StringConstants.WORKSPACE_NAME + " successfully."); isResourceSetSuccessfully = true; } }
From source file:bigbluej.Crawler.java
public String post(String url, String body) throws IOException { System.out.println("post with body> " + url); HttpClient client = httpClientFactory.create(); HttpPost request = new HttpPost(url); request.setEntity(new StringEntity(body)); HttpResponse response = client.execute(request); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder s = new StringBuilder(); String line;/* w w w . jav a 2 s . co m*/ while ((line = rd.readLine()) != null) { s.append(line); } return s.toString(); }
From source file:com.groupon.jenkins.util.HttpPoster.java
public String post(String url, Map postData) throws IOException { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost post = new HttpPost(url); try {//from w w w . j a v a 2 s .co m post.setEntity(new StringEntity(new ObjectMapper().writeValueAsString(postData))); HttpResponse response = httpclient.execute(post); HttpHost proxy = getProxy(post); if (proxy != null) { httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException(response.getStatusLine().toString()); } return getResponse(response); } finally { httpclient.getConnectionManager().shutdown(); } }
From source file:org.jboss.arquillian.ce.httpclient.HttpRequestImpl.java
public void setEntity(String body) throws IOException { if (request instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest.class.cast(request).setEntity(new StringEntity(body)); }// ww w .java2s . c o m }
From source file:org.apache.stratos.mock.iaas.client.rest.RestClient.java
/** * Handle http post request. Return String * * @param resourcePath This should be REST endpoint * @param jsonParamString The json string which should be executed from the post request * @return The HttpResponse//from w w w . j a v a2 s . c om * @throws Exception if any errors occur when executing the request */ public HttpResponse doPost(URI resourcePath, String jsonParamString) throws Exception { HttpPost postRequest = null; try { postRequest = new HttpPost(resourcePath); StringEntity input = new StringEntity(jsonParamString); input.setContentType("application/json"); postRequest.setEntity(input); return httpClient.execute(postRequest, new HttpResponseHandler()); } finally { releaseConnection(postRequest); } }