Skip to content

Instantly share code, notes, and snippets.

@maxlord
Created December 9, 2016 11:23
Show Gist options
  • Save maxlord/bdf7e7149dfb99b7c97a1bc8f31e3eb6 to your computer and use it in GitHub Desktop.
Save maxlord/bdf7e7149dfb99b7c97a1bc8f31e3eb6 to your computer and use it in GitHub Desktop.
methodPut
@SuppressWarnings("unchecked")
public static <T> T methodPut(Map<String, Object> params, String url, MapperParams mapperParams) {
HttpClient httpClient = getHttpClient();
List<NameValuePair> httpParams = new ArrayList<NameValuePair>();
for (String key : params.keySet()) {
httpParams.add(new BasicNameValuePair(key, params.get(key).toString()));
}
HttpPut put = new HttpPut(url);
try {
put.setEntity(new UrlEncodedFormEntity(httpParams, "UTF-8"));
if (mapperParams.getContentType() != null) {
put.setHeader(HttpHeaders.CONTENT_TYPE, mapperParams.getContentType());
}
}
catch (UnsupportedEncodingException e) {
LOG.error(e.getMessage());
}
try {
LOG.debug("PUT Request line: " + put.getRequestLine());
System.out.println("PUT Request line: " + put.getRequestLine());
LOG.debug("PUT requst params: ");
System.out.println("PUT requst params: ");
for (String s: params.keySet()){
LOG.debug(s + ": " + params.get(s));
System.out.println(s + ": " + params.get(s));
}
HttpResponse response = httpClient.execute(put);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
LOG.error("Страница не загрузилась, код ошибки : " + response.getStatusLine().getStatusCode() + " Ответ: ");
System.out.println("Страница не загрузилась, код ошибки : " + response.getStatusLine().getStatusCode() + " Ответ: ");
LOG.error(IOUtils.toString(response.getEntity().getContent()));
System.out.println(IOUtils.toString(response.getEntity().getContent()));
}
String jsonResult = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
System.out.println("Response: " + jsonResult);
// ObjectMapper mapper = new ObjectMapper();
// setParamsToMapper(mapper, mapperParams);
// T result = (T) mapper.readValue(jsonResult, mapperParams.getResultClass());
// return result;
return null;
}
catch (ClientProtocolException e) {
LOG.error("Ошибка в http-протоколе : " + e.getMessage());
System.out.println("Ошибка в http-протоколе : " + e.getMessage());
}
catch (IOException e) {
LOG.error(e.getMessage());
System.out.println(e.getMessage());
}
finally {
put.releaseConnection();
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment