List of usage examples for org.apache.http.client.methods HttpPut setEntity
public void setEntity(final HttpEntity entity)
From source file:com.expertiseandroid.lib.sociallib.utils.Utils.java
private static HttpRequestBase getRequestBaseByVerbParams(String verb, String request, List<NameValuePair> bodyParams) throws UnsupportedEncodingException { if (verb.equals(POST)) { HttpPost post = new HttpPost(request); post.setEntity(new UrlEncodedFormEntity(bodyParams, HTTP.UTF_8)); setParams(post);//from ww w. j a v a 2 s .c om return post; } else if (verb.equals(GET)) return new HttpGet(request); else if (verb.equals(PUT)) { HttpPut put = new HttpPut(request); put.setEntity(new UrlEncodedFormEntity(bodyParams, HTTP.UTF_8)); setParams(put); return put; } else if (verb.equals(DELETE)) return new HttpDelete(request); else Log.e("Utils", "Cannot reckognize the verb " + verb); return null; }
From source file:cn.tc.ulife.platform.msg.http.util.HttpUtil.java
/** * HTTP PUT//from w ww . java 2s . c o m * @param host * @param path * @param connectTimeout * @param headers * @param querys * @param bodys * @param signHeaderPrefixList * @param appKey * @param appSecret * @return * @throws Exception */ public static Response httpPut(String host, String path, int connectTimeout, Map<String, String> headers, Map<String, String> querys, byte[] bodys, List<String> signHeaderPrefixList, String appKey, String appSecret) throws Exception { headers = initialBasicHeader(HttpMethod.PUT, path, headers, querys, null, signHeaderPrefixList, appKey, appSecret); HttpClient httpClient = wrapClient(host); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, getTimeout(connectTimeout)); HttpPut put = new HttpPut(initUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { put.addHeader(e.getKey(), MessageDigestUtil.utf8ToIso88591(e.getValue())); } if (bodys != null) { put.setEntity(new ByteArrayEntity(bodys)); } return convert(httpClient.execute(put)); }
From source file:cn.tc.ulife.platform.msg.http.util.HttpUtil.java
/** * HTTP PUT //w ww .j a v a 2 s . co m * @param host * @param path * @param connectTimeout * @param headers * @param querys * @param body * @param signHeaderPrefixList * @param appKey * @param appSecret * @return * @throws Exception */ public static Response httpPut(String host, String path, int connectTimeout, Map<String, String> headers, Map<String, String> querys, String body, List<String> signHeaderPrefixList, String appKey, String appSecret) throws Exception { headers = initialBasicHeader(HttpMethod.PUT, path, headers, querys, null, signHeaderPrefixList, appKey, appSecret); HttpClient httpClient = wrapClient(host); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, getTimeout(connectTimeout)); HttpPut put = new HttpPut(initUrl(host, path, querys)); for (Map.Entry<String, String> e : headers.entrySet()) { put.addHeader(e.getKey(), MessageDigestUtil.utf8ToIso88591(e.getValue())); } if (StringUtils.isNotBlank(body)) { put.setEntity(new StringEntity(body, Constants.ENCODING)); } return convert(httpClient.execute(put)); }
From source file:com.spoiledmilk.ibikecph.util.HttpUtils.java
public static JsonNode putToServer(String urlString, JSONObject objectToPost) { JsonNode ret = null;//from ww w. j a va 2 s .co m LOG.d("PUT api request, url = " + urlString); HttpParams myParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT); HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT); HttpClient httpclient = new DefaultHttpClient(myParams); httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT); HttpPut httput = null; URL url = null; try { url = new URL(urlString); httput = new HttpPut(url.toString()); httput.setHeader("Content-type", "application/json"); httput.setHeader("Accept", ACCEPT); httput.setHeader("LANGUAGE_CODE", IbikeApplication.getLanguageString()); StringEntity se = new StringEntity(objectToPost.toString(), HTTP.UTF_8); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httput.setEntity(se); HttpResponse response = httpclient.execute(httput); String serverResponse = EntityUtils.toString(response.getEntity()); LOG.d("API response = " + serverResponse); ret = Util.stringToJsonNode(serverResponse); } catch (Exception e) { if (e != null && e.getLocalizedMessage() != null) { LOG.e(e.getLocalizedMessage()); } } return ret; }
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); }//from w w w. j a v a2 s. co 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:tagtime.beeminder.BeeminderAPI.java
/** * Runs a put request.// w w w . j a v a2 s . c o m * @return Whether the request completed successfully. If this is * false, then Beeminder is probably inaccessible, and no * more requests should be sent for now. */ private static boolean runPutRequest(HttpClient client, TagTime tagTimeInstance, String dataURL, List<NameValuePair> postData) { //add the authorization token postData.add(new BasicNameValuePair("auth_token", tagTimeInstance.settings.getStringValue(SettingType.AUTH_TOKEN))); //build the request HttpPut putRequest = new HttpPut(dataURL); HttpResponse response; try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postData); System.out.println("PUT " + dataURL + "?" + new BufferedReader(new InputStreamReader(entity.getContent())).readLine()); putRequest.setEntity(entity); response = client.execute(putRequest); } catch (Exception e) { e.printStackTrace(); return false; } StatusLine status = response.getStatusLine(); System.out.println("Response: " + status.getStatusCode() + " " + status.getReasonPhrase()); try { EntityUtils.consume(response.getEntity()); } catch (IOException e) { e.printStackTrace(); } return true; }
From source file:org.wso2.mdm.qsg.utils.HTTPInvoker.java
public static HTTPResponse sendHTTPPutWithOAuthSecurity(String url, String payload, HashMap<String, String> headers) { HttpPut put = null; HttpResponse response = null;// w w w.j a v a2 s . c o m HTTPResponse httpResponse = new HTTPResponse(); CloseableHttpClient httpclient = null; try { httpclient = (CloseableHttpClient) createHttpClient(); StringEntity requestEntity = new StringEntity(payload, Constants.UTF_8); put = new HttpPut(url); put.setEntity(requestEntity); for (String key : headers.keySet()) { put.setHeader(key, headers.get(key)); } put.setHeader(Constants.Header.AUTH, OAUTH_BEARER + oAuthToken); response = httpclient.execute(put); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } BufferedReader rd = null; try { rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); } catch (IOException e) { e.printStackTrace(); } StringBuffer result = new StringBuffer(); String line = ""; try { while ((line = rd.readLine()) != null) { result.append(line); } } catch (IOException e) { e.printStackTrace(); } httpResponse.setResponseCode(response.getStatusLine().getStatusCode()); httpResponse.setResponse(result.toString()); try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } return httpResponse; }
From source file:eu.trentorise.smartcampus.network.RemoteConnector.java
public static String putJSON(String host, String service, String body, String token, Map<String, Object> parameters) throws SecurityException, RemoteException { final HttpResponse resp; String queryString = generateQueryString(parameters); String uriString = normalizeURL(host + service) + queryString; final HttpPut put = new HttpPut(uriString); put.setHeader(RH_ACCEPT, "application/json"); put.setHeader(RH_AUTH_TOKEN, bearer(token)); try {// www. j av a2s. co m if (body != null) { StringEntity input = new StringEntity(body, DEFAULT_CHARSET); input.setContentType("application/json"); put.setEntity(input); } resp = getHttpClient().execute(put); String response = EntityUtils.toString(resp.getEntity(), DEFAULT_CHARSET); if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { return response; } if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_FORBIDDEN || resp.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { throw new SecurityException(); } throw new RemoteException("Error validating " + resp.getStatusLine()); } catch (final Exception e) { throw new RemoteException(e.getMessage(), e); } }
From source file:de.adesso.referencer.search.helper.MyHelpMethods.java
public static String sendHttpRequest(String url, String requestBody, String requestType) throws IOException { String result = null;//ww w . j a v a 2 s . com CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse httpResponse; try { switch (requestType) { case "Get": httpResponse = httpclient.execute(new HttpGet(url)); break; case "Post": HttpPost httppost = new HttpPost(url); if (requestBody != null) { httppost.setEntity(new StringEntity(requestBody, DEFAULT_CHARSET)); } httpResponse = httpclient.execute(httppost); break; case "Put": HttpPut httpPut = new HttpPut(url); httpPut.addHeader("Content-Type", "application/json"); httpPut.addHeader("Accept", "application/json"); if (requestBody != null) { httpPut.setEntity(new StringEntity(requestBody, DEFAULT_CHARSET)); } httpResponse = httpclient.execute(httpPut); break; case "Delete": httpResponse = httpclient.execute(new HttpDelete(url)); break; default: httpResponse = httpclient.execute(new HttpGet(url)); break; } try { HttpEntity entity1 = httpResponse.getEntity(); if (entity1 != null) { long len = entity1.getContentLength(); if (len != -1 && len < MAX_CONTENT_LENGTH) { result = EntityUtils.toString(entity1, DEFAULT_CHARSET); } else { System.out.println("Error!!!! entity length=" + len); } } EntityUtils.consume(entity1); } finally { httpResponse.close(); } } catch (Exception e) { e.printStackTrace(); } finally { httpclient.close(); } return result; }
From source file:de.adesso.referencer.search.helper.MyHelpMethods.java
public static String sendHttpRequest2(String url, String requestBody, String requestType) throws IOException { String result = null;//from w w w . ja va 2 s . c o m CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse httpResponse; try { switch (requestType) { case "Get": httpResponse = httpclient.execute(new HttpGet(url)); break; case "Post": HttpPost httppost = new HttpPost(url); if (requestBody != null) { httppost.setEntity(new StringEntity(requestBody)); } httpResponse = httpclient.execute(httppost); break; case "Put": HttpPut httpPut = new HttpPut(url); httpPut.addHeader("Content-Type", "application/json"); httpPut.addHeader("Accept", "application/json"); if (requestBody != null) { httpPut.setEntity(new StringEntity(requestBody, DEFAULT_CHARSET)); } httpResponse = httpclient.execute(httpPut); break; case "Delete": httpResponse = httpclient.execute(new HttpDelete(url)); break; default: httpResponse = httpclient.execute(new HttpGet(url)); break; } try { HttpEntity entity1 = httpResponse.getEntity(); if (entity1 != null) { long len = entity1.getContentLength(); if (len != -1 && len < MAX_CONTENT_LENGTH) { result = EntityUtils.toString(entity1, DEFAULT_CHARSET); } else { System.out.println("Error!!!! entity length=" + len); } } EntityUtils.consume(entity1); } finally { httpResponse.close(); } } catch (Exception e) { e.printStackTrace(); } finally { httpclient.close(); } return result; }