use of org.apache.http.message.BasicHttpResponse in project CodeUtils by boredream.
the class HttpUtils method getOrPostString.
private static String getOrPostString(int method, String url, Map<String, String> postParams, Map<String, String> headers) throws Exception {
HashMap<String, String> map = new HashMap<String, String>();
if (headers != null) {
map.putAll(headers);
}
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
setConnectionParametersForRequest(connection, method, postParams);
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromConnection(connection));
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
Header contentTypeHeader = response.getHeaders(HTTP.CONTENT_TYPE)[0];
String responseCharset = parseCharset(contentTypeHeader);
byte[] bytes = entityToBytes(response.getEntity());
String responseContent = new String(bytes, responseCharset);
return responseContent;
}
use of org.apache.http.message.BasicHttpResponse in project CodeUtils by boredream.
the class LeanCloudHttpUtils method getOrPostString.
private static String getOrPostString(int method, String url, Map<String, String> postParams) throws Exception {
HashMap<String, String> map = getHeaderMap();
map.put("Content-Type", HEADER_CONTENT_TYPE);
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
setConnectionParametersForRequest(connection, method, postParams);
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromConnection(connection));
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
Header contentTypeHeader = response.getHeaders(HTTP.CONTENT_TYPE)[0];
String responseCharset = parseCharset(contentTypeHeader);
byte[] bytes = entityToBytes(response.getEntity());
String responseContent = new String(bytes, responseCharset);
return responseContent;
}
use of org.apache.http.message.BasicHttpResponse in project CodeUtils by boredream.
the class LeanCloudHttpUtils method updateBean.
public static String updateBean(Object object, String key) throws Exception {
String url = "https://api.leancloud.cn/1.1/classes/";
url += object.getClass().getSimpleName();
url += ("/" + key);
HashMap<String, String> map = getHeaderMap();
map.put("Content-Type", HEADER_CONTENT_TYPE);
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
connection.addRequestProperty("Content-Type", HEADER_CONTENT_TYPE);
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(new Gson().toJson(object).getBytes());
out.close();
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromConnection(connection));
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
Header contentTypeHeader = response.getHeaders(HTTP.CONTENT_TYPE)[0];
String responseCharset = parseCharset(contentTypeHeader);
byte[] bytes = entityToBytes(response.getEntity());
String responseContent = new String(bytes, responseCharset);
return responseContent;
}
use of org.apache.http.message.BasicHttpResponse in project ddf by codice.
the class SolrHttpWrapper method execute.
@Override
public ResponseWrapper execute(URI uri) {
HttpResponse httpResponse;
HttpGet get = new HttpGet(uri);
try {
LOGGER.debug("Executing uri: {}", uri.toString());
httpResponse = solrClient.execute(get);
return new ResponseWrapper(httpResponse);
} catch (IOException e) {
LOGGER.debug("Error during request. Returning null response.");
}
BasicHttpResponse response = new BasicHttpResponse(null, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Error during request.");
return new ResponseWrapper(response);
}
use of org.apache.http.message.BasicHttpResponse in project ddf by codice.
the class BackupCommandTest method prepareResponse.
private HttpResponse prepareResponse(int statusCode, String responseBody) {
HttpResponse httpResponse = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), statusCode, ""));
httpResponse.setStatusCode(statusCode);
try {
httpResponse.setEntity(new StringEntity(responseBody));
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException(e);
}
return httpResponse;
}
Aggregations