List of usage examples for org.apache.commons.httpclient HttpMethodBase setQueryString
@Override public void setQueryString(NameValuePair[] params)
From source file:org.apache.ambari.funtest.server.AmbariHttpWebRequest.java
/** * Executes the current request by using HttpClient methods and returns the response. * * @return - Response from the Ambari server/Agent server. * @throws IOException/*from ww w. j a v a2s.com*/ */ private WebResponse executeRequest() throws IOException { HttpMethodBase methodBase = null; String httpMethod; httpMethod = getHttpMethod(); if (httpMethod.equals("GET")) { methodBase = getGetMethod(); } else if (httpMethod.equals("POST")) { methodBase = getPostMethod(); } else if (httpMethod.equals("PUT")) { methodBase = getPutMethod(); } else if (httpMethod.equals("DELETE")) { methodBase = getDeleteMethod(); } else { new RuntimeException(String.format("Unsupported HTTP method: %s", httpMethod)); } WebResponse response = new WebResponse(); HttpClient httpClient = new HttpClient(); Map<String, String> headers = getHeaders(); for (Map.Entry<String, String> header : headers.entrySet()) { methodBase.addRequestHeader(header.getKey(), header.getValue()); } methodBase.setQueryString(getQueryString()); try { int statusCode = httpClient.executeMethod(methodBase); response.setStatusCode(statusCode); response.setContent(methodBase.getResponseBodyAsString()); } finally { methodBase.releaseConnection(); } return response; }
From source file:org.apache.asterix.test.aql.TestsUtils.java
public static InputStream executeQuery(String str, OutputFormat fmt) throws Exception { final String url = "http://localhost:19002/query"; HttpMethodBase method = null; if (str.length() + url.length() < MAX_URL_LENGTH) { //Use GET for small-ish queries method = new GetMethod(url); method.setQueryString(new NameValuePair[] { new NameValuePair("query", str) }); } else {//from w w w .j a v a 2 s . co m //Use POST for bigger ones to avoid 413 FULL_HEAD method = new PostMethod(url); ((PostMethod) method).setRequestEntity(new StringRequestEntity(str)); } //Set accepted output response type method.setRequestHeader("Accept", fmt.mimeType()); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); executeHttpMethod(method); return method.getResponseBodyAsStream(); }
From source file:org.apache.axis2.transport.http.AbstractHTTPSender.java
/** * Method used to copy all the common properties * * @param msgContext - The messageContext of the request message * @param url - The target URL * @param httpMethod - The http method used to send the request * @param httpClient - The httpclient used to send the request * @param soapActionString - The soap action atring of the request message * @return MessageFormatter - The messageFormatter for the relavent request message * @throws AxisFault - Thrown in case an exception occurs *///w w w . ja v a 2 s . co m protected MessageFormatter populateCommonProperties(MessageContext msgContext, URL url, HttpMethodBase httpMethod, HttpClient httpClient, String soapActionString) throws AxisFault { if (isAuthenticationEnabled(msgContext)) { httpMethod.setDoAuthentication(true); } MessageFormatter messageFormatter = MessageProcessorSelector.getMessageFormatter(msgContext); url = messageFormatter.getTargetAddress(msgContext, format, url); httpMethod.setPath(url.getPath()); httpMethod.setQueryString(url.getQuery()); httpMethod.setRequestHeader(HTTPConstants.HEADER_CONTENT_TYPE, messageFormatter.getContentType(msgContext, format, soapActionString)); httpMethod.setRequestHeader(HTTPConstants.HEADER_HOST, url.getHost()); if (msgContext.getOptions() != null && msgContext.getOptions().isManageSession()) { // setting the cookie in the out path Object cookieString = msgContext.getProperty(HTTPConstants.COOKIE_STRING); if (cookieString != null) { StringBuffer buffer = new StringBuffer(); buffer.append(cookieString); httpMethod.setRequestHeader(HTTPConstants.HEADER_COOKIE, buffer.toString()); } } if (httpVersion.equals(HTTPConstants.HEADER_PROTOCOL_10)) { httpClient.getParams().setVersion(HttpVersion.HTTP_1_0); } return messageFormatter; }
From source file:org.apache.axis2.transport.http.impl.httpclient3.HTTPSenderImpl.java
/** * Method used to copy all the common properties * //w w w. j a v a 2 s. c o m * @param msgContext * - The messageContext of the request message * @param url * - The target URL * @param httpMethod * - The http method used to send the request * @param httpClient * - The httpclient used to send the request * @param soapActionString * - The soap action atring of the request message * @return MessageFormatter - The messageFormatter for the relavent request * message * @throws AxisFault * - Thrown in case an exception occurs */ protected MessageFormatter populateCommonProperties(MessageContext msgContext, URL url, HttpMethodBase httpMethod, HttpClient httpClient, String soapActionString) throws AxisFault { if (isAuthenticationEnabled(msgContext)) { httpMethod.setDoAuthentication(true); } MessageFormatter messageFormatter = TransportUtils.getMessageFormatter(msgContext); url = messageFormatter.getTargetAddress(msgContext, format, url); httpMethod.setPath(url.getPath()); httpMethod.setQueryString(url.getQuery()); httpMethod.setRequestHeader(HTTPConstants.HEADER_CONTENT_TYPE, messageFormatter.getContentType(msgContext, format, soapActionString)); httpMethod.setRequestHeader(HTTPConstants.HEADER_HOST, url.getHost()); if (msgContext.getOptions() != null && msgContext.getOptions().isManageSession()) { // setting the cookie in the out path Object cookieString = msgContext.getProperty(HTTPConstants.COOKIE_STRING); if (cookieString != null) { StringBuffer buffer = new StringBuffer(); buffer.append(cookieString); httpMethod.setRequestHeader(HTTPConstants.HEADER_COOKIE, buffer.toString()); } } if (httpVersion.equals(HTTPConstants.HEADER_PROTOCOL_10)) { httpClient.getParams().setVersion(HttpVersion.HTTP_1_0); } return messageFormatter; }
From source file:org.apache.sling.commons.testing.integration.HttpTestBase.java
/** retrieve the contents of given URL and assert its content type * @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be checked * @param httpMethod supports just GET and POST methods * @throws IOException/* www . jav a 2 s. c o m*/ * @throws HttpException */ public String getContent(String url, String expectedContentType, List<NameValuePair> params, int expectedStatusCode, String httpMethod) throws IOException { HttpMethodBase method = null; if (HTTP_METHOD_GET.equals(httpMethod)) { method = new GetMethod(url); } else if (HTTP_METHOD_POST.equals(httpMethod)) { method = new PostMethod(url); } else { fail("Http Method not supported in this test suite, method: " + httpMethod); } if (params != null) { final NameValuePair[] nvp = new NameValuePair[0]; method.setQueryString(params.toArray(nvp)); } final int status = httpClient.executeMethod(method); final String content = getResponseBodyAsStream(method, 0); assertEquals("Expected status " + expectedStatusCode + " for " + url + " (content=" + content + ")", expectedStatusCode, status); final Header h = method.getResponseHeader("Content-Type"); if (expectedContentType == null) { if (h != null) { fail("Expected null Content-Type, got " + h.getValue()); } } else if (CONTENT_TYPE_DONTCARE.equals(expectedContentType)) { // no check } else if (h == null) { fail("Expected Content-Type that starts with '" + expectedContentType + " but got no Content-Type header at " + url); } else { assertTrue("Expected Content-Type that starts with '" + expectedContentType + "' for " + url + ", got '" + h.getValue() + "'", h.getValue().startsWith(expectedContentType)); } return content.toString(); }
From source file:org.apache.solr.servlet.CacheHeaderTest.java
@Test public void testCacheVetoHandler() throws Exception { File f = makeFile(CONTENTS);//from www.ja va 2 s .c o m HttpMethodBase m = getUpdateMethod("GET"); m.setQueryString(new NameValuePair[] { new NameValuePair("stream.file", f.getCanonicalPath()) }); getClient().executeMethod(m); assertEquals(200, m.getStatusCode()); checkVetoHeaders(m, true); }
From source file:org.apache.solr.servlet.CacheHeaderTest.java
@Test public void testCacheVetoException() throws Exception { HttpMethodBase m = getSelectMethod("GET"); // We force an exception from Solr. This should emit "no-cache" HTTP headers m.setQueryString(new NameValuePair[] { new NameValuePair("q", "xyz_ignore_exception:solr"), new NameValuePair("qt", "standard") }); getClient().executeMethod(m);/*from ww w .j av a2 s. c o m*/ assertFalse(m.getStatusCode() == 200); checkVetoHeaders(m, false); }
From source file:org.apache.solr.servlet.CacheHeaderTestBase.java
protected HttpMethodBase getSelectMethod(String method) { CommonsHttpSolrServer httpserver = (CommonsHttpSolrServer) getSolrServer(); HttpMethodBase m = null; if ("GET".equals(method)) { m = new GetMethod(httpserver.getBaseURL() + "/select"); } else if ("HEAD".equals(method)) { m = new HeadMethod(httpserver.getBaseURL() + "/select"); } else if ("POST".equals(method)) { m = new PostMethod(httpserver.getBaseURL() + "/select"); }// w ww .ja v a 2 s. c om m.setQueryString( new NameValuePair[] { new NameValuePair("q", "solr"), new NameValuePair("qt", "standard") }); return m; }
From source file:org.entando.entando.plugins.jpoauthclient.aps.system.services.client.ProviderConnectionManager.java
private void addQueryString(HttpMethodBase method, Properties parameters) throws Throwable { if (null == parameters) return;/* w w w . j av a 2s . co m*/ StringBuilder builder = new StringBuilder(); boolean first = true; Iterator<Object> keyIter = parameters.keySet().iterator(); while (keyIter.hasNext()) { Object key = keyIter.next(); if (!first) builder.append("&"); builder.append(key.toString()).append("=").append(parameters.getProperty(key.toString())); if (first) first = false; } method.setQueryString(URIUtil.encodeQuery(builder.toString())); }
From source file:org.fao.geonet.csw.common.requests.CatalogRequest.java
private HttpMethodBase setupHttpMethod() throws UnsupportedEncodingException { HttpMethodBase httpMethod; if (method == Method.GET) { alGetParams = new ArrayList<NameValuePair>(); if (alSetupGetParams.size() != 0) { alGetParams.addAll(alSetupGetParams); }/* www. j a va 2s . co m*/ setupGetParams(); httpMethod = new GetMethod(); httpMethod.setPath(path); httpMethod.setQueryString(alGetParams.toArray(new NameValuePair[1])); System.out.println("GET params:" + httpMethod.getQueryString()); if (useSOAP) httpMethod.addRequestHeader("Accept", "application/soap+xml"); } else { Element params = getPostParams(); PostMethod post = new PostMethod(); if (!useSOAP) { postData = Xml.getString(new Document(params)); post.setRequestEntity(new StringRequestEntity(postData, "application/xml", "UTF8")); } else { postData = Xml.getString(new Document(soapEmbed(params))); post.setRequestEntity(new StringRequestEntity(postData, "application/soap+xml", "UTF8")); } System.out.println("POST params:" + Xml.getString(params)); httpMethod = post; httpMethod.setPath(path); } // httpMethod.setFollowRedirects(true); if (useAuthent) { Credentials cred = new UsernamePasswordCredentials(username, password); AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM); client.getState().setCredentials(scope, cred); httpMethod.setDoAuthentication(true); } return httpMethod; }