Java Code Examples for org.apache.commons.httpclient.methods.PostMethod#setQueryString()

The following examples show how to use org.apache.commons.httpclient.methods.PostMethod#setQueryString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CatalogITCase.java    From olat with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateCatalogEntryQuery() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");

    final URI uri = UriBuilder.fromUri(getContextURI()).path("catalog").path(entry2.getKey().toString()).build();
    final PostMethod method = createPost(uri, MediaType.APPLICATION_JSON, true);
    method.setQueryString(new NameValuePair[] { new NameValuePair("name", "Entry-2-b"), new NameValuePair("description", "Entry-description-2-b"),
            new NameValuePair("type", String.valueOf(CatalogEntry.TYPE_NODE)) });

    final int code = c.executeMethod(method);
    assertEquals(200, code);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final CatalogEntryVO vo = parse(body, CatalogEntryVO.class);
    assertNotNull(vo);

    final CatalogEntry updatedEntry = catalogService.loadCatalogEntry(entry2);
    assertEquals("Entry-2-b", updatedEntry.getName());
    assertEquals("Entry-description-2-b", updatedEntry.getDescription());
}
 
Example 2
Source File: CatalogITCase.java    From olat with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateCatalogEntryQuery() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");

    final URI uri = UriBuilder.fromUri(getContextURI()).path("catalog").path(entry2.getKey().toString()).build();
    final PostMethod method = createPost(uri, MediaType.APPLICATION_JSON, true);
    method.setQueryString(new NameValuePair[] { new NameValuePair("name", "Entry-2-b"), new NameValuePair("description", "Entry-description-2-b"),
            new NameValuePair("type", String.valueOf(CatalogEntry.TYPE_NODE)) });

    final int code = c.executeMethod(method);
    assertEquals(200, code);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final CatalogEntryVO vo = parse(body, CatalogEntryVO.class);
    assertNotNull(vo);

    final CatalogEntry updatedEntry = catalogService.loadCatalogEntry(entry2);
    assertEquals("Entry-2-b", updatedEntry.getName());
    assertEquals("Entry-description-2-b", updatedEntry.getDescription());
}
 
Example 3
Source File: CreateRouteCommand.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected ServerStatus _doIt() {
	try {
		/* create cloud foundry application */
		URI targetURI = URIUtil.toURI(target.getUrl());
		URI routesURI = targetURI.resolve("/v2/routes"); //$NON-NLS-1$

		PostMethod createRouteMethod = new PostMethod(routesURI.toString());
		ServerStatus confStatus = HttpUtil.configureHttpMethod(createRouteMethod, target.getCloud());
		if (!confStatus.isOK())
			return confStatus;

		/* set request body */
		JSONObject routeRequest = new JSONObject();
		routeRequest.put(CFProtocolConstants.V2_KEY_SPACE_GUID, target.getSpace().getCFJSON().getJSONObject(CFProtocolConstants.V2_KEY_METADATA).getString(CFProtocolConstants.V2_KEY_GUID));
		routeRequest.put(CFProtocolConstants.V2_KEY_HOST, hostName);
		routeRequest.put(CFProtocolConstants.V2_KEY_DOMAIN_GUID, domain.getGuid());
		createRouteMethod.setRequestEntity(new StringRequestEntity(routeRequest.toString(), "application/json", "utf-8")); //$NON-NLS-1$//$NON-NLS-2$
		createRouteMethod.setQueryString("inline-relations-depth=1"); //$NON-NLS-1$

		ServerStatus createRouteStatus = HttpUtil.executeMethod(createRouteMethod);
		if (!createRouteStatus.isOK())
			return createRouteStatus;

		route = new Route().setCFJSON(createRouteStatus.getJsonData());

		return createRouteStatus;
	} catch (Exception e) {
		String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$
		logger.error(msg, e);
		return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e);
	}
}
 
Example 4
Source File: HttpClientUtil.java    From AndroidRobot with Apache License 2.0 4 votes vote down vote up
/**
 * @param url
 * @param queryString 类似a=b&c=d 形式的参数
 * 
 * @param obj   发送到服务器的对象。
 *     
 * @return 服务器返回到客户端的对象。
 * @throws IOException
 */
public static String httpObjRequest(String url, Object obj, String queryString ) throws IOException
{
	String response = null;
	HttpClient client = new HttpClient();
    
    PostMethod post = new PostMethod(url);
    
    post.setQueryString(queryString);

    post.setRequestHeader("Content-Type", "application/octet-stream");

    java.io.ByteArrayOutputStream bOut = new java.io.ByteArrayOutputStream(1024);
    java.io.ByteArrayInputStream bInput = null;
    java.io.ObjectOutputStream out = null;
    
    try
    {
        out = new java.io.ObjectOutputStream(bOut);

        out.writeObject(obj);

        out.flush();
        out.close();

        out = null;

        bInput = new java.io.ByteArrayInputStream(bOut.toByteArray());

        RequestEntity re = new InputStreamRequestEntity(bInput);
        post.setRequestEntity(re);

        client.executeMethod(post);
        response = post.getResponseBodyAsString();
        System.out.println("URI:" + post.getURI());
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally
    {
        if (out != null)
        {
            out.close();
            out = null;

        }

        if (bInput != null)
        {
            bInput.close();
            bInput = null;

        }
        //释放连接
        post.releaseConnection();
    }

    return response;

}