List of usage examples for org.apache.commons.httpclient.methods GetMethod setQueryString
public void setQueryString(String queryString)
From source file:com.glaf.core.util.http.CommonsHttpClientUtils.java
/** * ??GET/*from w w w. jav a 2 s . c o m*/ * * @param url * ?? * @param encoding * * @param dataMap * ? * * @return */ public static String doGet(String url, String encoding, Map<String, String> dataMap) { GetMethod method = null; String content = null; try { method = new GetMethod(url); method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encoding); method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding); if (dataMap != null && !dataMap.isEmpty()) { NameValuePair[] nameValues = new NameValuePair[dataMap.size()]; int i = 0; for (Map.Entry<String, String> entry : dataMap.entrySet()) { String name = entry.getKey().toString(); String value = entry.getValue(); nameValues[i] = new NameValuePair(name, value); i++; } method.setQueryString(nameValues); } HttpClient client = new HttpClient(); int status = client.executeMethod(method); if (status == HttpStatus.SC_OK) { content = method.getResponseBodyAsString(); } } catch (IOException ex) { throw new RuntimeException(ex); } finally { if (method != null) { method.releaseConnection(); method = null; } } return content; }
From source file:de.mpg.escidoc.services.reporting.ReportFHI.java
public static String getItemListFromFramework() { // Publications of the test context // Time range: previous month String itemList = null;//from w ww . ja va 2 s.c o m GetMethod method; try { method = new GetMethod(ServiceLocator.getFrameworkUrl() + "/ir/items"); method.setRequestHeader("Cookie", "escidocCookie=" + adminHandler); String query = "operation=searchRetrieve&maximumRecords=1000&query=" + URLEncoder.encode(rprops.getProperty("FHI.query"), "UTF-8") + "%20and%20" + URLEncoder.encode(getTimeRangeQuery(), "UTF-8") + URLEncoder.encode(rprops.getProperty("FHI.sort.by"), "UTF-8"); logger.info("query <" + query + ">"); method.setQueryString(query); logger.info("URI:" + method.getURI()); HttpClient client = new HttpClient(); ProxyHelper.executeMethod(client, method); logger.info("URI:" + method.getURI() + "\nStatus code:" + method.getStatusCode()); if (method.getStatusCode() == HttpServletResponse.SC_OK) { itemList = method.getResponseBodyAsString(); //escape all alone &, otherwise filler throws an exception itemList = replaceAllTotal(itemList, AMPS_ALONE, "&"); if (logger.isDebugEnabled()) writeToFile("./search-res.xml", itemList.getBytes("UTF-8")); logger.info(itemList); } } catch (Exception e) { logger.warn("Exception occured ", e); throw new RuntimeException("Cannot get item-list from framework:", e); } return itemList; }
From source file:de.mpg.mpdl.inge.reporting.ReportFHI.java
public static String getItemListFromFramework() { // Publications of the test context // Time range: previous month String itemList = null;//from w w w. j a va2 s .co m GetMethod method; try { method = new GetMethod(PropertyReader.getFrameworkUrl() + "/ir/items"); method.setRequestHeader("Cookie", "escidocCookie=" + adminHandler); String query = "operation=searchRetrieve&maximumRecords=1000&query=" + URLEncoder.encode(rprops.getProperty("FHI.query"), "UTF-8") + "%20and%20" + URLEncoder.encode(getTimeRangeQuery(), "UTF-8") + URLEncoder.encode(rprops.getProperty("FHI.sort.by"), "UTF-8"); logger.info("query <" + query + ">"); method.setQueryString(query); logger.info("URI:" + method.getURI()); HttpClient client = new HttpClient(); ProxyHelper.executeMethod(client, method); logger.info("URI:" + method.getURI() + "\nStatus code:" + method.getStatusCode()); if (method.getStatusCode() == HttpServletResponse.SC_OK) { itemList = method.getResponseBodyAsString(); // escape all alone &, otherwise filler throws an exception itemList = replaceAllTotal(itemList, AMPS_ALONE, "&"); if (logger.isDebugEnabled()) writeToFile("./search-res.xml", itemList.getBytes("UTF-8")); logger.info(itemList); } } catch (Exception e) { logger.warn("Exception occured ", e); throw new RuntimeException("Cannot get item-list from framework:", e); } return itemList; }
From source file:example.nz.org.take.compiler.userv.main.DUIConvictionInfoSource.java
@Override public ResourceIterator<hasBeenConvictedOfaDUI> fetch(Driver driver) { HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager()); client.getHttpConnectionManager().getParams().setConnectionTimeout(30000); GetMethod get = new GetMethod(URL); get.setFollowRedirects(true);/* www . j a va2s. c om*/ get.setQueryString(new NameValuePair[] { new NameValuePair("id", driver.getId()) }); try { logger.info("DUI conviction lookup " + get.getURI()); client.executeMethod(get); String response = get.getResponseBodyAsString(); logger.info("DUI conviction lookup result: " + response); if (response != null && "true".equals(response.trim())) { hasBeenConvictedOfaDUI record = new hasBeenConvictedOfaDUI(); record.slot1 = driver; get.releaseConnection(); return new SingletonIterator<hasBeenConvictedOfaDUI>(record); } } catch (Exception e) { logger.error("Error connecting to web service " + URL); } return EmptyIterator.DEFAULT; }
From source file:com.assemblade.client.Login.java
public Authentication login(String username, String password) throws ChangePasswordException, CallFailedException { GetMethod method = new GetMethod(baseUrl + "/login"); try {/* ww w.j ava2 s. c o m*/ method.setQueryString(new NameValuePair[] { new NameValuePair("username", username), new NameValuePair("password", password) }); int status = executeMethod(method); if (status == 200) { try { return mapper.readValue(method.getResponseBodyAsStream(), Authentication.class); } catch (IOException e) { } } else if (status == 403) { throw new ChangePasswordException(); } } finally { method.releaseConnection(); } return null; }
From source file:edu.northwestern.bioinformatics.studycalendar.security.plugin.cas.direct.DirectLoginHttpFacade.java
/** * Contacts the CAS server and GETs its login form, returning the content as a string. *//*ww w .jav a 2 s . co m*/ public String getForm() throws IOException { log.trace("GETting {} to obtain login ticket", loginUrl); GetMethod get = initMethod(new GetMethod(loginUrl)); get.setQueryString(new NameValuePair[] { new NameValuePair("service", getServiceUrl()) }); try { httpClient.executeMethod(get); if (get.getStatusCode() == HttpStatus.SC_OK) { return get.getResponseBodyAsString(); } else { throw new CasDirectException("Retrieving the login form %s failed: %s", loginUrl, get.getStatusLine()); } } finally { get.releaseConnection(); } }
From source file:jshm.sh.client.HttpForm.java
/** * //w w w. ja v a2 s . c o m * @param method Is an {@link Object} for the purpose of overloading the constructor * @param url * @param data An array of Strings with alternating keys and values */ public HttpForm(final String method, final Object url, final String... data) { if ((data.length & 1) != 0) throw new IllegalArgumentException("data must have an even number of values"); if (!(url instanceof String)) throw new IllegalArgumentException("url must be a String"); this.url = (String) url; this.data = new NameValuePair[data.length / 2]; // this.data[0] = data[0], data[1] // this.data[1] = data[2], data[3] // this.data[2] = data[4], data[5] for (int i = 0; i < data.length; i += 2) { this.data[i / 2] = new NameValuePair(data[i], data[i + 1]); } this.methodName = method; if ("POST".equalsIgnoreCase(method.toString())) { PostMethod postMethod = new PostMethod(this.url); postMethod.setRequestBody(this.data); this.method = postMethod; } else if ("GET".equalsIgnoreCase(method.toString())) { GetMethod getMethod = new GetMethod(this.url); getMethod.setQueryString(this.data); this.method = getMethod; } else { throw new IllegalArgumentException("method must be POST or GET, given: " + method); } }
From source file:ca.uvic.cs.tagsea.research.GetIDWithProgress.java
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { GetMethod getUidMethod = new GetMethod(uidScript); getUidMethod.setQueryString(new NameValuePair[] { first, last, email, job, size, buisness, anon }); monitor.beginTask("Get User Id", 1); HttpClient client = new HttpClient(); try {/* w w w. ja va2 s . co m*/ status = client.executeMethod(getUidMethod); resp = getData(getUidMethod.getResponseBodyAsStream()); // release the connection to the server getUidMethod.releaseConnection(); } catch (Exception e) { // there was a problem with the file upload so throw up // an error // dialog to inform the user and log the exception failed = true; throw new InvocationTargetException(e); } if (status != 200) { // there was a problem with the file upload so throw up an error // dialog to inform the user failed = true; // there was a problem with the file upload so throw up an error // dialog to inform the user MessageDialog.openError(null, "Error Getting User ID", "There was an error getting a user id: \n" + "HTTP Response Code " + status + "\n" + "Please try again later"); } else { resp = resp.substring(resp.indexOf(":") + 1).trim(); uid = Integer.parseInt(resp); } monitor.worked(1); monitor.done(); }
From source file:io.seldon.client.test.js.BaseJavascriptTest.java
private <T> T retrievePayload(String path, NameValuePair[] queryParameters, Class<T> valueType) throws IOException { GetMethod getMethod = new GetMethod(testState.getEndpoint() + path); getMethod.setQueryString(queryParameters); String httpReferer = testState.getHttpReferer(); if (httpReferer != null) { getMethod.setRequestHeader("Referer", httpReferer); }/*from w ww . j a va2 s . co m*/ client.executeMethod(getMethod); String response = getMethod.getResponseBodyAsString(); // Remove jsonp prefix and suffix: String payload = stripJsonp(response); logger.info("Payload: " + payload); return objectMapper.readValue(payload, valueType); }
From source file:com.owncloud.android.lib.resources.files.SearchOperation.java
@Override protected RemoteOperationResult run(OwnCloudClient client) { RemoteOperationResult result = null; GetMethod get = null; try {/* w w w .j av a 2 s .c om*/ get = new GetMethod(client.getBaseUri() + URI); get.setQueryString(new NameValuePair[] { new NameValuePair("query", itsQuery) }); int status = client.executeMethod(get); if (checkSuccess(status, get)) { Log_OC.d(TAG, "Successful response len: " + get.getResponseContentLength()); result = new RemoteOperationResult(true, status, get.getResponseHeaders()); ArrayList<Object> data = new ArrayList<Object>(); JSONArray respJson = new JSONArray(get.getResponseBodyAsString()); for (int i = 0; i < respJson.length(); ++i) { JSONObject item = respJson.getJSONObject(i); data.add(new Result(item.optString("type", null), item.optString("path", null))); } result.setData(data); } else { result = new RemoteOperationResult(false, status, get.getResponseHeaders()); } } catch (Exception e) { result = new RemoteOperationResult(e); Log_OC.e(TAG, "Exception during search", e); } finally { if (get != null) { get.releaseConnection(); } } return result; }