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 jo-client-platform by jo-source.
the class HttpClientStub method post.
private HttpResponse post(final HttpPost request) throws IOException, ClientProtocolException {
postInvocations.incrementAndGet();
final StatusLine status = postStatus.get();
if (status.getStatusCode() == 200) {
try {
queue.put((MessageStub) new ObjectInputStream(request.getEntity().getContent()).readObject());
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
return new BasicHttpResponse(status);
}
Aggregations