List of usage examples for org.apache.commons.httpclient.methods GetMethod setQueryString
public void setQueryString(String queryString)
From source file:edu.uci.ics.pregelix.example.util.TestExecutor.java
private InputStream getHandleResult(String handle, OutputFormat fmt) throws Exception { final String url = "http://localhost:19002/query/result"; // Create a method instance. GetMethod method = new GetMethod(url); method.setQueryString(new NameValuePair[] { new NameValuePair("handle", handle) }); method.setRequestHeader("Accept", fmt.mimeType()); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); executeHttpMethod(method);//w ww .j a va 2 s . c om return method.getResponseBodyAsStream(); }
From source file:com.gisgraphy.servlet.ReverseGeocodingServletTest.java
@Test public void testShouldReturnCorrectContentTypeForSupportedFormat() { String url = reverseGeocodingServletUrl + REVERSE_GECODOING_SERVLET_CONTEXT + "/search"; String queryString;// w ww . j ava 2 s . co m for (OutputFormat format : OutputFormat.values()) { GetMethod get = null; try { queryString = ReverseGeocodingQuery.LAT_PARAMETER + "=3&" + ReverseGeocodingQuery.LONG_PARAMETER + "=4&format=" + format.toString(); HttpClient client = new HttpClient(); get = new GetMethod(url); get.setQueryString(queryString); client.executeMethod(get); // result = get.getResponseBodyAsString(); Header contentType = get.getResponseHeader("Content-Type"); OutputFormat expectedformat = OutputFormatHelper.getDefaultForServiceIfNotSupported(format, GisgraphyServiceType.REVERSEGEOCODING); assertTrue(contentType.getValue().equals(expectedformat.getContentType())); } catch (IOException e) { fail("An exception has occured " + e.getMessage()); } finally { if (get != null) { get.releaseConnection(); } } } }
From source file:com.gisgraphy.servlet.StreetServletTest.java
@Test public void testServletShouldReturnCorrectXMLError() { String url = streetServletUrl + STREET_SERVLET_CONTEXT + "/streetsearch"; String result;/* w w w.j ava 2 s . c o m*/ String queryString; OutputFormat format = OutputFormat.XML; GetMethod get = null; try { queryString = StreetServlet.FORMAT_PARAMETER + "=" + format.getParameterValue(); HttpClient client = new HttpClient(); get = new GetMethod(url); get.setQueryString(queryString); client.executeMethod(get); result = get.getResponseBodyAsString().trim(); // JsTester String expected = ResourceBundle.getBundle(Constants.BUNDLE_ERROR_KEY).getString("error.emptyLatLong"); FeedChecker.assertQ("The XML error is not correct", result, "//error[.='" + expected + "']"); } catch (IOException e) { fail("An exception has occured " + e.getMessage()); } finally { if (get != null) { get.releaseConnection(); } } }
From source file:com.gisgraphy.servlet.StreetServletTest.java
@Test public void testServletShouldReturnCorrectContentTypeForSupportedFormatWhenErrorOccured() { String url = streetServletUrl + STREET_SERVLET_CONTEXT + "/streetsearch"; String queryStringWithMissingLat; for (OutputFormat format : OutputFormat.values()) { GetMethod get = null; try {//w w w . jav a 2 s . co m queryStringWithMissingLat = StreetSearchQuery.LONG_PARAMETER + "=4&format=" + format.toString(); HttpClient client = new HttpClient(); get = new GetMethod(url); get.setQueryString(queryStringWithMissingLat); client.executeMethod(get); // result = get.getResponseBodyAsString(); Header contentType = get.getResponseHeader("Content-Type"); OutputFormat expectedformat = OutputFormatHelper.getDefaultForServiceIfNotSupported(format, GisgraphyServiceType.STREET); assertEquals("The content-type is not correct", expectedformat.getContentType(), contentType.getValue()); } catch (IOException e) { fail("An exception has occured " + e.getMessage()); } finally { if (get != null) { get.releaseConnection(); } } } }
From source file:com.gisgraphy.servlet.StreetServletTest.java
@Test public void testServletShouldReturnCorrectStatusCode() { String url = streetServletUrl + STREET_SERVLET_CONTEXT + "/streetsearch"; String queryStringWithMissingLat; GetMethod get = null; try {// ww w . j a va 2s. c o m queryStringWithMissingLat = StreetSearchQuery.LONG_PARAMETER + "=4&format=" + OutputFormat.JSON.toString(); HttpClient client = new HttpClient(); get = new GetMethod(url); get.setQueryString(queryStringWithMissingLat); client.executeMethod(get); // result = get.getResponseBodyAsString(); assertEquals("status code is not correct ", 500, get.getStatusCode()); } catch (IOException e) { fail("An exception has occured " + e.getMessage()); } finally { if (get != null) { get.releaseConnection(); } } }
From source file:fr.openwide.talendalfresco.rest.client.RestAuthenticationTest.java
public void testRestLogout() { // first login testRestLogin();//w w w . ja va 2 s . c o m // create client and configure it HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout); // instantiating a new method and configuring it GetMethod method = new GetMethod(restCommandUrlPrefix + "logout"); method.setFollowRedirects(true); // ? // Provide custom retry handler is necessary (?) method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); NameValuePair[] params = new NameValuePair[] { new NameValuePair("ticket", ticket) }; // TODO always provide ticket method.setQueryString(params); try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } // Read the response body. byte[] responseBody = method.getResponseBody(); // Deal with the response. // Use caution: ensure correct character encoding and is not binary data System.out.println(new String(responseBody)); } catch (HttpException e) { // TODO e.printStackTrace(); } catch (IOException e) { // TODO e.printStackTrace(); } finally { // Release the connection. method.releaseConnection(); } }
From source file:eu.learnpad.core.impl.or.XwikiBridgeInterfaceRestResource.java
@Override public KBProcessingStatus getHandlingProcessStatus(String kbProcessProcessId) throws LpRestException { HttpClient httpClient = this.getClient(); String uri = String.format("%s/learnpad/or/bridge/kb/getstatus", DefaultRestResource.REST_URI); GetMethod getMethod = new GetMethod(uri); getMethod.addRequestHeader("Accept", "application/xml"); NameValuePair[] queryString = new NameValuePair[1]; queryString[0] = new NameValuePair("kbprocessprocessid", kbProcessProcessId); getMethod.setQueryString(queryString); KBProcessingStatus processingStatus = null; try {// ww w.j a va 2 s. c o m httpClient.executeMethod(getMethod); InputStream responseStream = getMethod.getResponseBodyAsStream(); if (responseStream != null) { JAXBContext jc = JAXBContext.newInstance(KBProcessingStatus.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); processingStatus = (KBProcessingStatus) unmarshaller.unmarshal(responseStream); } } catch (JAXBException | IOException e) { throw new LpRestExceptionXWikiImpl(e.getMessage(), e.getCause()); } return processingStatus; }
From source file:ccc.api.http.SiteBrowserImpl.java
/** {@inheritDoc} */ @Override/*from w ww.ja va 2 s . c o m*/ public String get(final String absolutePath, final Map<String, String[]> params) { final GetMethod get = new GetMethod(_hostUrl + absolutePath); final List<NameValuePair> qParams = new ArrayList<NameValuePair>(); for (final Map.Entry<String, String[]> param : params.entrySet()) { for (final String value : param.getValue()) { final NameValuePair qParam = new NameValuePair(param.getKey(), value); qParams.add(qParam); } } final StringBuilder buffer = createQueryString(qParams); get.setQueryString(buffer.toString()); return invoke(get); }
From source file:com.gisgraphy.servlet.StreetServletTest.java
@Test public void testServletShouldReturnCorrectJSONError() { JsTester jsTester = null;/* www. j ava2 s. co m*/ String url = streetServletUrl + STREET_SERVLET_CONTEXT + "/streetsearch"; String result; String queryString; OutputFormat format = OutputFormat.JSON; GetMethod get = null; try { jsTester = new JsTester(); jsTester.onSetUp(); queryString = StreetServlet.FORMAT_PARAMETER + "=" + format.getParameterValue(); HttpClient client = new HttpClient(); get = new GetMethod(url); get.setQueryString(queryString); client.executeMethod(get); result = get.getResponseBodyAsString(); // JsTester jsTester.eval("evalresult= eval(" + result + ");"); jsTester.assertNotNull("evalresult"); String error = jsTester.eval("evalresult.error").toString(); String expected = ResourceBundle.getBundle(Constants.BUNDLE_ERROR_KEY).getString("error.emptyLatLong"); assertEquals(expected, error); } catch (IOException e) { fail("An exception has occured " + e.getMessage()); } finally { if (jsTester != null) { jsTester.onTearDown(); } if (get != null) { get.releaseConnection(); } } }
From source file:mclub.util.loc.LocationCoderService.java
protected LocationObject queryGeoCodeService(String addr) { // - http://jueyue.iteye.com/blog/1688718 // -// ww w . ja v a 2 s. c o m // http://api.map.baidu.com/geocoder?address=%E6%B5%99%E6%B1%9F%E6%9D%AD%E5%B7%9E&output=json&key=g2fQ61PYZCgBwTY7YAk9c8n7&city=%E6%B5%99%E6%B1%9F%E6%9D%AD%E5%B7%9E LocationObject location = null; String apiURL = "http://api.map.baidu.com/geocoder"; try { HttpClient client = new HttpClient(); GetMethod httpget = new GetMethod(apiURL /* "http://api.map.baidu.com/geocoder" */); httpget.setRequestHeader("Accept-Charset", "utf-8,gb2312"); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new NameValuePair("address", addr)); params.add(new NameValuePair("city", addr)); params.add(new NameValuePair("output", "json")); params.add(new NameValuePair("key", "g2fQ61PYZCgBwTY7YAk9c8n7")); httpget.setQueryString(params.toArray(new NameValuePair[0])); int status = client.executeMethod(httpget); // System.out.println("Response status: " + status); log.debug("Response status: " + status); String jsonString = httpget.getResponseBodyAsString(); log.debug("JSON:"); log.debug(jsonString); // System.out.println("JSON:"); // System.out.println(jsonString); JSONObject obj = (JSONObject) JSON.parse(jsonString); if (obj != null) { if ("OK".equalsIgnoreCase(obj.getString("status"))) { JSONObject loc = obj.getJSONObject("result").getJSONObject("location"); if (loc != null) { // construct the GeoLocation object location = new LocationObject(); location.addr = addr; location.lat = loc.getDouble("lat"); location.lon = loc.getDouble("lng"); // location.lat = String.format("%.6f", // loc.getDouble("lat"));// new String(loc("lat"); // location.lon = String.format("%.6f", // loc.getDouble("lng"));// loc.getString("lon"); } } } } catch (Exception e) { log.info("Error query location : " + e.getMessage()); } return location; }