List of usage examples for org.apache.commons.httpclient HttpMethod getURI
public abstract URI getURI() throws URIException;
From source file:com.discursive.jccook.httpclient.QueryStringExample.java
public static void main(String[] args) throws HttpException, IOException { HttpClient client = new HttpClient(); String url = "http://www.discursive.com/cgi-bin/jccook/param_list.cgi"; // Set the Query String with setQueryString() HttpMethod method = new GetMethod(url); method.setQueryString(URIUtil.encodeQuery("test1=O'Reilly&blah=Whoop")); System.out.println("With Query String: " + method.getURI()); client.executeMethod(method);// ww w. j a va2s. c o m System.out.println("Response:\n " + method.getResponseBodyAsString()); method.releaseConnection(); // Set query string with name value pair objects method = new GetMethod(url); NameValuePair pair = new NameValuePair("test2", URIUtil.encodeQuery("One & Two")); NameValuePair pair2 = new NameValuePair("param2", URIUtil.encodeQuery("TSCM")); NameValuePair[] pairs = new NameValuePair[] { pair, pair2 }; method.setQueryString(pairs); System.out.println("With NameValuePairs: " + method.getURI()); client.executeMethod(method); System.out.println("Response:\n " + method.getResponseBodyAsString()); method.releaseConnection(); }
From source file:com.jaspersoft.studio.server.util.HttpUtils31.java
public static HttpMethod get(HttpClient client, String url) throws HttpException, IOException { HttpMethod method = new GetMethod(url); method.setRequestHeader("Accept", "application/json"); System.out.println(method.getURI()); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) System.err.println("Method failed: " + method.getStatusLine()); return method; }
From source file:de.mpg.escidoc.services.common.util.ProxyHelper.java
/** * * Wrapper for executeMethod with Proxy//from w w w . j a va 2 s .c o m * * @param client, methopd * @throws IOException * @throws HttpException */ public static int executeMethod(HttpClient client, HttpMethod method) throws HttpException, IOException { setProxy(client, method.getURI().toString()); return client.executeMethod(method); }
From source file:com.zimbra.common.httpclient.HttpClientUtil.java
public static int executeMethod(HttpClient client, HttpMethod method, HttpState state) throws HttpException, IOException { ProxyHostConfiguration proxyConfig = HttpProxyConfig.getProxyConfig(client.getHostConfiguration(), method.getURI().toString()); if (proxyConfig != null && proxyConfig.getUsername() != null && proxyConfig.getPassword() != null) { if (state == null) { state = client.getState();//from w w w .j a v a 2s .co m if (state == null) { state = new HttpState(); } } state.setProxyCredentials(new AuthScope(proxyConfig.getHost(), proxyConfig.getPort()), new UsernamePasswordCredentials(proxyConfig.getUsername(), proxyConfig.getPassword())); } return client.executeMethod(proxyConfig, method, state); }
From source file:com.github.jobs.api.GithubJobsApi.java
private static String createUrl(String url, List<NameValuePair> pairs) throws URIException { HttpMethod method = new GetMethod(url); NameValuePair[] nameValuePairs = pairs.toArray(new NameValuePair[pairs.size()]); method.setQueryString(nameValuePairs); return method.getURI().getEscapedURI(); }
From source file:com.wafersystems.util.HttpUtil.java
/** * URL?/*from w ww .ja v a2 s .co m*/ * * @param hClient HttpClient * @param url URL * * @return * @throws Exception */ public static String openURL(HttpClient hClient, String url) throws Exception { HttpMethod method = null; try { //Get method = new GetMethod(url); // URL int result = hClient.executeMethod(method); //cookie? //getCookie(method.getURI().getHost(), method.getURI().getPort(), "/" , false , hClient.getState().getCookies()); //??? result = checkRedirect(method.getURI().getHost(), method.getURI().getPort(), hClient, method, result); if (result != HttpStatus.SC_OK) logger.error(method.getPath() + "" + method.getStatusLine().toString() + "\r\n" + method.getResponseBodyAsString()); return method.getResponseBodyAsString(); } finally { method.releaseConnection(); } }
From source file:com.feilong.tools.net.httpclient3.HttpClientUtil.java
/** * ?log./*from w ww . j a v a2 s . com*/ * * @param httpMethod * the http method * @return the http method attribute map for log */ private static Map<String, Object> getHttpMethodRequestAttributeMapForLog(HttpMethod httpMethod) { Map<String, Object> map = new LinkedHashMap<String, Object>(); try { map.put("httpMethod.getName()", httpMethod.getName()); map.put("httpMethod.getURI()", httpMethod.getURI().toString()); map.put("httpMethod.getPath()", httpMethod.getPath()); map.put("httpMethod.getQueryString()", httpMethod.getQueryString()); map.put("httpMethod.getRequestHeaders()", httpMethod.getRequestHeaders()); map.put("httpMethod.getDoAuthentication()", httpMethod.getDoAuthentication()); map.put("httpMethod.getFollowRedirects()", httpMethod.getFollowRedirects()); map.put("httpMethod.getHostAuthState()", httpMethod.getHostAuthState().toString()); // HttpMethodParams httpMethodParams = httpMethod.getParams(); // map.put("httpMethod.getParams()", httpMethodParams); map.put("httpMethod.getProxyAuthState()", httpMethod.getProxyAuthState().toString()); } catch (Exception e) { log.error(e.getClass().getName(), e); } return map; }
From source file:com.zimbra.cs.servlet.ZimbraServlet.java
public static void proxyServletRequest(HttpServletRequest req, HttpServletResponse resp, HttpMethod method, HttpState state) throws IOException, ServiceException { // create an HTTP client with the same cookies javax.servlet.http.Cookie cookies[] = req.getCookies(); String hostname = method.getURI().getHost(); boolean hasZMAuth = hasZimbraAuthCookie(state); if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (cookies[i].getName().equals(ZimbraCookie.COOKIE_ZM_AUTH_TOKEN) && hasZMAuth) continue; state.addCookie(// w w w . j ava 2s.c o m new Cookie(hostname, cookies[i].getName(), cookies[i].getValue(), "/", null, false)); } } HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient(); if (state != null) client.setState(state); int hopcount = 0; for (Enumeration<?> enm = req.getHeaderNames(); enm.hasMoreElements();) { String hname = (String) enm.nextElement(), hlc = hname.toLowerCase(); if (hlc.equals("x-zimbra-hopcount")) try { hopcount = Math.max(Integer.parseInt(req.getHeader(hname)), 0); } catch (NumberFormatException e) { } else if (hlc.startsWith("x-") || hlc.startsWith("content-") || hlc.equals("authorization")) method.addRequestHeader(hname, req.getHeader(hname)); } if (hopcount >= MAX_PROXY_HOPCOUNT) throw ServiceException.TOO_MANY_HOPS(HttpUtil.getFullRequestURL(req)); method.addRequestHeader("X-Zimbra-Hopcount", Integer.toString(hopcount + 1)); if (method.getRequestHeader("X-Zimbra-Orig-Url") == null) method.addRequestHeader("X-Zimbra-Orig-Url", req.getRequestURL().toString()); String ua = req.getHeader("User-Agent"); if (ua != null) method.setRequestHeader("User-Agent", ua); // dispatch the request and copy over the results int statusCode = -1; for (int retryCount = 3; statusCode == -1 && retryCount > 0; retryCount--) { statusCode = HttpClientUtil.executeMethod(client, method); } if (statusCode == -1) { resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "retry limit reached"); return; } else if (statusCode >= 300) { resp.sendError(statusCode, method.getStatusText()); return; } Header[] headers = method.getResponseHeaders(); for (int i = 0; i < headers.length; i++) { String hname = headers[i].getName(), hlc = hname.toLowerCase(); if (hlc.startsWith("x-") || hlc.startsWith("content-") || hlc.startsWith("www-")) resp.addHeader(hname, headers[i].getValue()); } InputStream responseStream = method.getResponseBodyAsStream(); if (responseStream == null || resp.getOutputStream() == null) return; ByteUtil.copy(method.getResponseBodyAsStream(), false, resp.getOutputStream(), false); }
From source file:com.comcast.cats.service.util.HttpClientUtil.java
public static synchronized Object getForObject(String uri, Map<String, String> paramMap) { Object responseObject = new Object(); HttpMethod httpMethod = new GetMethod(uri); if ((null != paramMap) && (!paramMap.isEmpty())) { httpMethod.setQueryString(getNameValuePair(paramMap)); }/*from ww w .j a v a 2 s. co m*/ Yaml yaml = new Yaml(); HttpClient client = new HttpClient(); InputStream responseStream = null; Reader inputStreamReader = null; try { int responseCode = client.executeMethod(httpMethod); if (HttpStatus.SC_OK != responseCode) { logger.error("[ REQUEST ] " + httpMethod.getURI().toString()); logger.error("[ METHOD ] " + httpMethod.getName()); logger.error("[ STATUS ] " + responseCode); } else { logger.trace("[ REQUEST ] " + httpMethod.getURI().toString()); logger.trace("[ METHOD ] " + httpMethod.getName()); logger.trace("[ STATUS ] " + responseCode); } responseStream = httpMethod.getResponseBodyAsStream(); inputStreamReader = new InputStreamReader(responseStream, VideoRecorderServiceConstants.UTF); responseObject = yaml.load(inputStreamReader); } catch (IOException ioException) { ioException.printStackTrace(); } finally { cleanUp(inputStreamReader, responseStream, httpMethod); } return responseObject; }
From source file:com.comcast.cats.service.util.HttpClientUtil.java
public static synchronized Object postForObject(String uri, Map<String, String> paramMap) { Object responseObject = new Object(); HttpMethod httpMethod = new PostMethod(uri); if ((null != paramMap) && (!paramMap.isEmpty())) { httpMethod.setQueryString(getNameValuePair(paramMap)); }/* w w w. ja v a2 s .c o m*/ Yaml yaml = new Yaml(); HttpClient client = new HttpClient(); InputStream responseStream = null; Reader inputStreamReader = null; try { int responseCode = client.executeMethod(httpMethod); if (HttpStatus.SC_OK != responseCode) { logger.error("[ REQUEST ] " + httpMethod.getURI().toString()); logger.error("[ METHOD ] " + httpMethod.getName()); logger.error("[ STATUS ] " + responseCode); } else { logger.trace("[ REQUEST ] " + httpMethod.getURI().toString()); logger.trace("[ METHOD ] " + httpMethod.getName()); logger.trace("[ STATUS ] " + responseCode); } responseStream = httpMethod.getResponseBodyAsStream(); inputStreamReader = new InputStreamReader(responseStream, VideoRecorderServiceConstants.UTF); responseObject = yaml.load(inputStreamReader); } catch (IOException ioException) { ioException.printStackTrace(); } finally { cleanUp(inputStreamReader, responseStream, httpMethod); } return responseObject; }