List of usage examples for org.apache.http.entity StringEntity StringEntity
public StringEntity(String str) throws UnsupportedEncodingException
From source file:Logica.prueba.java
public String getSessionKey() throws UnsupportedEncodingException { post.setEntity(new StringEntity("{\"method\": " + "\"get_session_key\", " + "\"params\": {\"username\": \"admin\", \"password\": " + "\"password\" }, \"id\": 1}")); try {// w w w .j a va2s . c o m HttpResponse response = cliente.execute(post); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entidad = response.getEntity(); key = parse(EntityUtils.toString(entidad)); } } catch (Exception e) { System.err.println("error para conseguir el key"); } return key; }
From source file:org.hydracache.server.httpd.handler.BaseJsonServiceAction.java
private void generateResponse(HttpResponse response, String jsonString) throws UnsupportedEncodingException { StringEntity body = new StringEntity(jsonString); body.setContentType(PLAIN_TEXT_RESPONSE_CONTENT_TYPE); response.setEntity(body);//from w w w .ja v a2 s .c o m }
From source file:org.cryptable.pki.communication.http.PKIHTTPCommunicationTest.java
private HttpResponse prepareResponse(int expectedResponseStatus, String expectedResponseBody) { HttpResponse response = new BasicHttpResponse( new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), expectedResponseStatus, "")); response.setStatusCode(expectedResponseStatus); try {// w w w . ja v a 2 s . c o m response.setEntity(new StringEntity(expectedResponseBody)); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException(e); } return response; }
From source file:com.pb.identify.network.PostRestStringService.java
public PostRestStringService(Context context, String url, final Map<String, String> header, RequestObserver<String> downloadObserver) { super(context, downloadObserver); // APIM specific body try {//from www. j av a 2 s . com se = new StringEntity("grant_type=client_credentials"); se.setContentType("application/x-www-form-urlencoded"); Log.d("SE content has been set"); } catch (UnsupportedEncodingException e) { VolleyLog.e("UnsupportedEncodingException to setContentType"); } String sendUrl = url.replaceAll(" ", "%20"); Log.d("HTTP Post connection to " + sendUrl); _Request = new StringRequest(Request.Method.POST, sendUrl, reponseListener, errorListener) { @Override public Map<String, String> getHeaders() throws AuthFailureError { Log.d("Inside PostRestStringService@getHeaders"); return header; } @Override public byte[] getBody() { // APIM specific body String httpPostBody = "grant_type=client_credentials"; Log.d("custom has been added as per APIM"); return httpPostBody.getBytes(); } @Override protected VolleyError parseNetworkError(VolleyError volleyError) { Log.d("The VolleyError is " + volleyError.getMessage()); if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) { VolleyError error = new VolleyError(volleyError.networkResponse); Log.d("The volley error network data is " + volleyError.networkResponse.statusCode); volleyError = error; } else { Log.d("POST : The volley error network data is null"); } return volleyError; } @Override protected Response<String> parseNetworkResponse(NetworkResponse response) { return super.parseNetworkResponse(response); } }; }
From source file:bit.changepurse.wdk.util.CheckedExceptionMethods.java
public static HttpEntity toHttpEntity(String entity) { try {//w ww. jav a2 s . co m return new StringEntity(entity != null ? entity : ""); } catch (UnsupportedEncodingException e) { throw new UncheckedException(e); } }
From source file:retsys.client.http.HttpHelper.java
public HttpPut getHttpPutObj(String operation, String body) throws IOException { HttpPut put = null;/*from ww w . jav a2 s. com*/ put = new HttpPut(getHttpUrl(hostName, hostPort, context) + "/" + operation); StringEntity se = new StringEntity(body); se.setContentEncoding("UTF-8"); se.setContentType("application/json"); put.setEntity(se); return put; }
From source file:diuf.unifr.ch.first.xwot.notifications.LockNotificationBuilder.java
/** * Encode into xml the Lock JAXB class/*from ww w .j av a2 s . co m*/ * * @see Lock * @param client * @return instance of a StringEntity containg xml informations */ @Override public StringEntity jaxbToStringEntity(Client client) { StringEntity body = null; if (lock == null || !lock.equalsToLock(oldLock)) { setLock(); } try { body = new StringEntity(jaxbToXml(Lock.class, lock)); body.setContentType("application/xml"); } catch (UnsupportedEncodingException ex) { logger.error("Unable to encode StringEntity", ex); } return body; }
From source file:com.qcloud.CloudClient.java
public String post(String url, Map<String, String> header, Map<String, Object> body, byte[] data) throws UnsupportedEncodingException, IOException { HttpPost httpPost = new HttpPost(url); httpPost.setHeader("accept", "*/*"); httpPost.setHeader("connection", "Keep-Alive"); httpPost.setHeader("user-agent", "qcloud-java-sdk"); if (header != null) { for (String key : header.keySet()) { httpPost.setHeader(key, header.get(key)); }// w w w .java 2s .co m } if (false == header.containsKey("Content-Type") || header.get("Content-Type").equals("multipart/form-data")) { MultipartEntity multipartEntity = new MultipartEntity(); if (body != null) { for (String key : body.keySet()) { multipartEntity.addPart(key, new StringBody(body.get(key).toString())); } } if (data != null) { ContentBody contentBody = new ByteArrayBody(data, "qcloud"); multipartEntity.addPart("fileContent", contentBody); } httpPost.setEntity(multipartEntity); } else { if (data != null) { String strBody = new String(data); StringEntity stringEntity = new StringEntity(strBody); httpPost.setEntity(stringEntity); } } // HttpHost proxy = new HttpHost("127.0.0.1",8888); // mClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy); HttpResponse httpResponse = mClient.execute(httpPost); int code = httpResponse.getStatusLine().getStatusCode(); return EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); }
From source file:io.undertow.server.NewlineInHeadersTestCase.java
@Test public void testNewlineInHeaders() throws IOException { DefaultServer.setRootHandler(new HttpHandler() { @Override// w w w. ja v a 2 s.c om public void handleRequest(HttpServerExchange exchange) throws Exception { exchange.getRequestReceiver().receiveFullString(new Receiver.FullStringCallback() { @Override public void handle(HttpServerExchange exchange, String message) { exchange.getResponseHeaders().put(HttpString.tryFromString(ECHO), message); exchange.getResponseSender().send(RESPONSE); } }); } }); final TestHttpClient client = new TestHttpClient(); try { HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL()); post.setEntity(new StringEntity("test")); HttpResponse result = client.execute(post); Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); Assert.assertEquals("test", result.getFirstHeader(ECHO).getValue()); Assert.assertEquals(RESPONSE, HttpClientUtils.readResponse(result)); post = new HttpPost(DefaultServer.getDefaultServerURL()); post.setEntity(new StringEntity("test\nnewline")); result = client.execute(post); Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); Assert.assertEquals("test newline", result.getFirstHeader(ECHO).getValue()); Assert.assertEquals(RESPONSE, HttpClientUtils.readResponse(result)); } finally { client.getConnectionManager().shutdown(); } }
From source file:DataSci.main.JsonRequestResponseController.java
/** * Call REST API for retrieving prediction from Azure ML * @return response from the REST API/*from w w w . j a v a2 s . c o m*/ */ public String rrsHttpPost() { HttpPost post; HttpClient client; StringEntity entity; try { // create HttpPost and HttpClient object post = new HttpPost(apiurl); client = HttpClientBuilder.create().build(); // setup output message by copying JSON body into // apache StringEntity object along with content type entity = new StringEntity(jsonBody/*, HTTP.UTF_8*/); // entity.setContentEncoding(HTTP.UTF_8); entity.setContentType("text/json"); // add HTTP headers post.setHeader("Accept", "text/json"); post.setHeader("Accept-Charset", "UTF-8"); // set Authorization header based on the API key post.setHeader("Authorization", ("Bearer " + apikey)); post.setEntity(entity); // Call REST API and retrieve response content HttpResponse authResponse = client.execute(post); return EntityUtils.toString(authResponse.getEntity()); } catch (Exception e) { return e.toString(); } }