Example usage for org.apache.http.protocol HTTP CONTENT_TYPE

List of usage examples for org.apache.http.protocol HTTP CONTENT_TYPE

Introduction

In this page you can find the example usage for org.apache.http.protocol HTTP CONTENT_TYPE.

Prototype

String CONTENT_TYPE

To view the source code for org.apache.http.protocol HTTP CONTENT_TYPE.

Click Source Link

Usage

From source file:com.siddroid.offlinews.SendOffline.java

/**
 * Send json post./*from www . j a  va2s. c om*/
 * This function sends a JSON request to webservice
 * @param url the url of webservice
 * @param req the req for the webservice
 * @return the result as object
 */
public Object sendJsonPost(String url, String req) {
    DefaultHttpClient client = new DefaultHttpClient();
    try {
        HttpPost post = new HttpPost(url);
        /*create entity of request to be sent with service*/
        StringEntity se = new StringEntity(req);
        /*Add headers*/
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

        Log.d("CHECK", "JSON: " + req);
        /*set Entity*/
        post.setEntity(se);
        HttpResponse response = client.execute(post);
        if (response != null) {//check response
            Log.d("CHECK", "Response: " + response);
            InputStream in = response.getEntity().getContent();
            Log.d("CHECK", "in stream: " + in);
            /*returns result object after processing inputstream*/
            return sendResponse(in);
        } else {
            Log.e("CHECK", "Response found null");
            return null;
        }
    } catch (Exception e) {
        Log.e("CHECK", e.toString());
    }
    return null;
}

From source file:com.jobs.lib_v1.net.http.multipart.MultipartEntity.java

@Override
public Header getContentType() {
    StringBuffer buffer = new StringBuffer(MULTIPART_FORM_CONTENT_TYPE);
    buffer.append("; boundary=");
    buffer.append(EncodingUtils.getAsciiString(getMultipartBoundary()));
    return new BasicHeader(HTTP.CONTENT_TYPE, buffer.toString());
}

From source file:net.sylvek.where.FacebookClient.java

private String postRequest(String params) throws ClientProtocolException, IOException {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(server);
    post.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
    post.setEntity(new StringEntity(params));
    HttpResponse response = client.execute(post);
    if (response.getStatusLine().getStatusCode() == 200) {
        return EntityUtils.toString(response.getEntity());
    }//from   w w w  . j  a  v a2s .co  m

    throw new IOException(response.getStatusLine().getReasonPhrase());
}

From source file:com.lostad.app.base.util.RequestUtil.java

public static String postJson(String url, String token, String dataJson) throws Exception {
    String json = null;//from   w  w  w .j  ava 2  s  .c o  m
    HttpEntity resEntity = null;
    HttpClient client = HttpClientManager.getHttpClient(true);
    HttpPost post = new HttpPost(url);
    if (Validator.isNotEmpty(token)) {
        post.addHeader("token", token);
    }
    try {
        post.addHeader("Content-Type", "application/json;charset=UTF-8");
        StringEntity se = new StringEntity(dataJson, "UTF-8");
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        post.setEntity(se);
        //
        HttpResponse response = client.execute(post, new BasicHttpContext());
        resEntity = response.getEntity();
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 200
            // ?
            json = EntityUtils.toString(resEntity);
        }
    } catch (Exception e) {
        if (post != null) {
            post.abort();//
        }
        throw e;
    } finally {

        if (resEntity != null) {
            try {
                resEntity.consumeContent();//?

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        client.getConnectionManager().closeExpiredConnections();
        ///client.getConnectionManager().shutdown();
    }
    return json;
}

From source file:org.apache.synapse.mediators.transform.PayloadFactoryMediator.java

private void handleSpecialProperties(Object resultValue,
        org.apache.axis2.context.MessageContext axis2MessageCtx) {
    axis2MessageCtx.setProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE, resultValue);
    Object o = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    Map headers = (Map) o;
    if (headers != null) {
        headers.remove(HTTP.CONTENT_TYPE);
        headers.put(HTTP.CONTENT_TYPE, resultValue);
    }//from   www . j a  v a2s .  com
}

From source file:org.alfresco.dataprep.ContentActions.java

/**
 * Create multiple tags or comments for document or folder
 * //from w w w  . j av  a2  s. co m
 * @param userName login username
 * @param password login password
 * @param siteName site name
 * @param contentName file or folder name
 * @param optType action type
 * @param values List of values
 * @return true if request is successful
 */
private boolean addMultipleActions(final String userName, final String password, final String siteName,
        final String contentName, final boolean repository, final String pathToItem, final ActionType optType,
        final List<String> values) {
    String jsonInput = "";
    String nodeRef;
    AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
    if (!repository) {
        nodeRef = getNodeRef(userName, password, siteName, contentName);
    } else {
        nodeRef = getNodeRefByPath(userName, password, pathToItem);
    }
    String reqUrl = client.getApiVersionUrl() + "nodes/" + nodeRef + optType.name;
    HttpPost post = new HttpPost(reqUrl);
    jsonInput = ("[{" + "\"" + optType.bodyParam + "\"" + ": \"" + values.get(0) + "\"");
    for (int i = 1; i < values.size(); i++) {
        jsonInput = (jsonInput + "},{" + "\"" + optType.bodyParam + "\"" + ": \"" + values.get(i) + "\"");
    }
    jsonInput = (jsonInput + "}]");
    StringEntity se = new StringEntity(jsonInput.toString(), AlfrescoHttpClient.UTF_8_ENCODING);
    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, AlfrescoHttpClient.MIME_TYPE_JSON));
    post.setEntity(se);
    HttpResponse response = client.executeRequest(userName, password, post);
    if (HttpStatus.SC_CREATED == response.getStatusLine().getStatusCode()) {
        return true;
    } else {
        logger.error("Unable to add new action: " + response.toString());
    }
    return false;
}

From source file:FileGetTest.java

@Test
public void testServerGet() {
    running(getTestServer(), HTMLUNIT, new Callback<TestBrowser>() {
        public void invoke(TestBrowser browser) {
            setHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
            setHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
            setHeader(HTTP.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED);
            httpRequest(getURLAddress(), getMethod());
            assertServer("testServerGet", Status.OK, null, false);
        }/* w ww .j a  va  2s  .c o m*/
    });
}

From source file:AdminAssetCreateTest.java

public void routeDeleteAsset() {
    mParametersSimple.put(PARAM_NAME, TEST_ASSET_NAME());
    mParametersSimple.put(PARAM_META, getPayload("/adminAssetCreateMeta.json").toString());

    FakeRequest request = new FakeRequest(DELETE, getRouteAddress() + "/" + mParametersSimple.get(PARAM_NAME));
    request = request.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
    request = request.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
    request = request.withHeader(HTTP.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED);
    request = request.withFormUrlEncodedBody(mParametersSimple);
    Result result = routeAndCall(request);
    assertRoute(result, "testRouteCreateSimpleAsset. Delete", Status.OK, null, false);

}