Search in sources :

Example 66 with BasicHttpResponse

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;
}
Also used : HashMap(java.util.HashMap) ProtocolVersion(org.apache.http.ProtocolVersion) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 67 with BasicHttpResponse

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;
}
Also used : ProtocolVersion(org.apache.http.ProtocolVersion) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 68 with BasicHttpResponse

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;
}
Also used : Gson(com.google.gson.Gson) ProtocolVersion(org.apache.http.ProtocolVersion) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 69 with BasicHttpResponse

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);
}
Also used : BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException)

Example 70 with BasicHttpResponse

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;
}
Also used : StringEntity(org.apache.http.entity.StringEntity) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine)

Aggregations

BasicHttpResponse (org.apache.http.message.BasicHttpResponse)70 ProtocolVersion (org.apache.http.ProtocolVersion)51 BasicStatusLine (org.apache.http.message.BasicStatusLine)45 StatusLine (org.apache.http.StatusLine)39 HttpResponse (org.apache.http.HttpResponse)33 StringEntity (org.apache.http.entity.StringEntity)25 Header (org.apache.http.Header)24 List (java.util.List)23 IOException (java.io.IOException)19 HttpHost (org.apache.http.HttpHost)19 URL (java.net.URL)16 BasicHeader (org.apache.http.message.BasicHeader)16 HttpURLConnection (java.net.HttpURLConnection)15 HashMap (java.util.HashMap)15 HttpEntity (org.apache.http.HttpEntity)14 MainResponse (org.elasticsearch.action.main.MainResponse)14 ElasticsearchException (org.elasticsearch.ElasticsearchException)12 RequestLine (org.apache.http.RequestLine)11 BasicRequestLine (org.apache.http.message.BasicRequestLine)11 RestStatus (org.elasticsearch.rest.RestStatus)11