List of usage examples for org.apache.commons.httpclient HttpMethod setStrictMode
public abstract void setStrictMode(boolean paramBoolean);
From source file:ExampleP2PHttpClient.java
public static void main(String[] args) { // initialize JXTA try {// www . ja va 2s . c o m // sign in and initialize the JXTA network; profile this peer and create it // if it doesn't exist P2PNetwork.signin("clientpeer", "clientpeerpassword", "TestNetwork", true); } catch (Exception e) { e.printStackTrace(); System.exit(1); } // register the P2P socket protocol factory Protocol jxtaHttp = new Protocol("p2phttp", new P2PProtocolSocketFactory(), 80); Protocol.registerProtocol("p2phttp", jxtaHttp); //create a singular HttpClient object HttpClient client = new HttpClient(); //establish a connection within 50 seconds client.setConnectionTimeout(50000); String url = System.getProperty("url"); if (url == null || url.equals("")) { System.out.println("You must provide a URL to access. For example:"); System.out.println("ant example-webclient-run -D--url=p2phttp://www.somedomain.foo"); System.exit(1); } System.out.println("Connecting to " + url + "..."); HttpMethod method = null; //create a method object method = new GetMethod(url); method.setFollowRedirects(true); method.setStrictMode(false); //} catch (MalformedURLException murle) { // System.out.println("<url> argument '" + url // + "' is not a valid URL"); // System.exit(-2); //} //execute the method String responseBody = null; try { client.executeMethod(method); responseBody = method.getResponseBodyAsString(); } catch (HttpException he) { System.err.println("Http error connecting to '" + url + "'"); System.err.println(he.getMessage()); System.exit(-4); } catch (IOException ioe) { System.err.println("Unable to connect to '" + url + "'"); System.exit(-3); } //write out the request headers System.out.println("*** Request ***"); System.out.println("Request Path: " + method.getPath()); System.out.println("Request Query: " + method.getQueryString()); Header[] requestHeaders = method.getRequestHeaders(); for (int i = 0; i < requestHeaders.length; i++) { System.out.print(requestHeaders[i]); } //write out the response headers System.out.println("*** Response ***"); System.out.println("Status Line: " + method.getStatusLine()); Header[] responseHeaders = method.getResponseHeaders(); for (int i = 0; i < responseHeaders.length; i++) { System.out.print(responseHeaders[i]); } //write out the response body System.out.println("*** Response Body ***"); System.out.println(responseBody); //clean up the connection resources method.releaseConnection(); method.recycle(); System.exit(0); }
From source file:lucee.commons.net.http.httpclient3.HttpMethodCloner.java
/** * Clones a HttpMethod. <br>//from www .j ava 2 s .c om * <b>Attention:</b> You have to clone a method before it has * been executed, because the URI can change if followRedirects * is set to true. * * @param m the HttpMethod to clone * * @return the cloned HttpMethod, null if the HttpMethod could * not be instantiated * * @throws java.io.IOException if the request body couldn't be read */ public static HttpMethod clone(HttpMethod m) { HttpMethod copy = null; try { copy = m.getClass().newInstance(); } catch (InstantiationException iEx) { } catch (IllegalAccessException iaEx) { } if (copy == null) { return null; } copy.setDoAuthentication(m.getDoAuthentication()); copy.setFollowRedirects(m.getFollowRedirects()); copy.setPath(m.getPath()); copy.setQueryString(m.getQueryString()); Header[] h = m.getRequestHeaders(); int size = (h == null) ? 0 : h.length; for (int i = 0; i < size; i++) { copy.setRequestHeader(new Header(h[i].getName(), h[i].getValue())); } copy.setStrictMode(m.isStrictMode()); if (m instanceof HttpMethodBase) { copyHttpMethodBase((HttpMethodBase) m, (HttpMethodBase) copy); } if (m instanceof EntityEnclosingMethod) { copyEntityEnclosingMethod((EntityEnclosingMethod) m, (EntityEnclosingMethod) copy); } return copy; }
From source file:autohit.call.modules.SimpleHttpModule.java
/** * Start method. It will set the target address for the client, as well as * clearing any state.//from w ww . ja va2 s. c om * * @param url * the Url path, not to include protocol, address, and port (ie. * "/goats/index.html"). * @return the data from the page as a String * @throws CallException */ private String get(String url) throws CallException { if (started == false) { throw buildException("module:SimpleHttp:Tried to get when a session wasn't started.", CallException.CODE_MODULE_FAULT); } String result = null; // Construct our method. HttpMethod method = new GetMethod(url); method.setFollowRedirects(true); method.setStrictMode(false); //execute the method try { // Do it debug("(get)get=" + url); httpClient.executeMethod(method); // Process result result = method.getResponseBodyAsString(); log("(get)" + method.getStatusLine().toString() + " size=" + result.length()); } catch (HttpException he) { // Bad but not fatal error("(get)Error on connect to url " + url + ". Error=" + he.getMessage()); } catch (IOException ioe) { // Fatal throw buildException("(get)Unable to connect. Session is invalid. message=" + ioe.getMessage(), CallException.CODE_MODULE_FAULT, ioe); } finally { try { method.releaseConnection(); method.recycle(); } catch (Exception e) { // Already FUBAR } } return result; }
From source file:de.innovationgate.webgate.api.query.rss.WGDatabaseImpl.java
private Document retrievePage(String url) { try {//w w w .j ava 2 s . c om // Try to retrieve from cache CachedPage page = (CachedPage) this.cachedPages.get(url); if (page != null) { return page.getDocument(); } SAXReader reader = new SAXReader(); // Retrieve from web HttpClient client = WGFactory.getHttpClientFactory().createHttpClient(); client.setConnectionTimeout(5000); HttpMethod method = new GetMethod(url); method.setFollowRedirects(true); method.setStrictMode(false); client.executeMethod(method); // Read response. Wrap content decoder if necessary. InputStream inStream = method.getResponseBodyAsStream(); // Not necessary - HttpClient decodes automatically /*Header contentEncoding = method.getResponseHeader("Content-Encoding"); if (contentEncoding != null) { if (contentEncoding.getValue().equals("gzip")) { inStream = new GZIPInputStream(inStream); } }*/ Document doc = reader.read(inStream); /*HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection(); Document doc = reader.read(conn.getInputStream());*/ // Put into cache, if cache latency is set if (cacheLatency > 0) { this.cachedPages.put(url, new CachedPage(doc)); } return doc; } catch (MalformedURLException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } catch (DocumentException e) { e.printStackTrace(); return null; } }
From source file:de.innovationgate.webgate.api.rss2.SimpleRSS.java
private InputStream retrievePage(String url, NativeQueryOptions nativeOptions) throws WGQueryException { try {//from ww w . ja va 2 s. c o m // Retrieve from web HttpClient client = WGFactory.getHttpClientFactory().createHttpClient(); client.setConnectionTimeout(10000); if (_useProxy) { client.getHostConfiguration().setProxy(_proxyHost, _proxyPort); if (_proxyCredentials != null) { Credentials credentials; if (_proxyDomain != null) { List elements = WGUtils.deserializeCollection(_proxyCredentials, ":"); credentials = new NTCredentials((String) elements.get(0), (String) elements.get(1), _proxyHost, _proxyDomain); } else { credentials = new UsernamePasswordCredentials(_proxyCredentials); } client.getState().setProxyCredentials(null, _proxyHost, credentials); } } HttpMethod method = new GetMethod(url); method.setFollowRedirects(true); method.setStrictMode(false); if (nativeOptions.containsKey(QUERYOPTION_USER) && nativeOptions.containsKey(QUERYOPTION_PWD)) { method.setDoAuthentication(true); client.getParams().setAuthenticationPreemptive(true); client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials( nativeOptions.get(QUERYOPTION_USER), nativeOptions.get(QUERYOPTION_PWD))); } client.executeMethod(method); // Read response. Wrap content decoder if necessary. InputStream inStream = method.getResponseBodyAsStream(); // Return InputStream from given URL return inStream; } catch (MalformedURLException e) { throw new WGQueryException("Malformed feed URL", url, e); } catch (IOException e) { throw new WGQueryException("IO Exception retrieving feed", url, e); } }
From source file:org.codehaus.wadi.web.impl.CommonsHttpProxy.java
protected void doProxy(URI uri, WebInvocation context) throws ProxyingException { HttpServletRequest hreq = context.getHreq(); HttpServletResponse hres = context.getHres(); long startTime = System.currentTimeMillis(); String m = hreq.getMethod();/*from w w w . ja v a2 s . c o m*/ Class clazz = (Class) _methods.get(m); if (clazz == null) { throw new IrrecoverableException("unsupported http method: " + m); } HttpMethod hm = null; try { hm = (HttpMethod) clazz.newInstance(); } catch (Exception e) { throw new IrrecoverableException("could not create HttpMethod instance", e); // should never happen } String requestURI = getRequestURI(hreq); hm.setPath(requestURI); String queryString = hreq.getQueryString(); if (queryString != null) { hm.setQueryString(queryString); requestURI += queryString; } hm.setFollowRedirects(false); //hm.setURI(new URI(uri)); hm.setStrictMode(false); // check connection header String connectionHdr = hreq.getHeader("Connection"); // TODO - what if there are multiple values ? if (connectionHdr != null) { connectionHdr = connectionHdr.toLowerCase(); if (connectionHdr.equals("keep-alive") || connectionHdr.equals("close")) connectionHdr = null; // TODO ?? } // copy headers boolean xForwardedFor = false; boolean hasContent = false; int contentLength = 0; Enumeration enm = hreq.getHeaderNames(); while (enm.hasMoreElements()) { // TODO could be better than this! - using javax.servlet ? String hdr = (String) enm.nextElement(); String lhdr = hdr.toLowerCase(); if (_DontProxyHeaders.contains(lhdr)) continue; if (connectionHdr != null && connectionHdr.indexOf(lhdr) >= 0) continue; if ("content-length".equals(lhdr)) { try { contentLength = hreq.getIntHeader(hdr); hasContent = contentLength > 0; } catch (NumberFormatException e) { if (_log.isWarnEnabled()) _log.warn("bad Content-Length header value: " + hreq.getHeader(hdr), e); } } if ("content-type".equals(lhdr)) { hasContent = true; } Enumeration vals = hreq.getHeaders(hdr); while (vals.hasMoreElements()) { String val = (String) vals.nextElement(); if (val != null) { hm.addRequestHeader(hdr, val); // if (_log.isInfoEnabled()) _log.info("Request " + hdr + ": " + val); xForwardedFor |= "X-Forwarded-For".equalsIgnoreCase(hdr); // why is this not in the outer loop ? } } } // cookies... // although we copy cookie headers into the request abover - commons-httpclient thinks it knows better and strips them out before sending. // we have to explicitly use their interface to add the cookies - painful... // DOH! - an org.apache.commons.httpclient.Cookie is NOT a // javax.servlet.http.Cookie - and it looks like the two don't // map onto each other without data loss... HttpState state = new HttpState(); javax.servlet.http.Cookie[] cookies = hreq.getCookies(); if (cookies != null) { for (int i = 0; i < cookies.length; i++) { javax.servlet.http.Cookie c = cookies[i]; String domain = c.getDomain(); if (domain == null) { domain = hreq.getServerName(); // TODO - tmp test // _log.warn("defaulting cookie domain"); } // domain=null; String cpath = c.getPath(); if (cpath == null) { cpath = hreq.getContextPath(); // fix for Jetty // _log.warn("defaulting cookie path"); } //if (_log.isTraceEnabled()) _log.trace("PATH: value="+path+" length="+(path==null?0:path.length())); Cookie cookie = new Cookie(domain, c.getName(), c.getValue(), cpath, c.getMaxAge(), c.getSecure()); // TODO - sort out domain //if (_log.isTraceEnabled()) _log.trace("Cookie: "+cookie.getDomain()+","+ cookie.getName()+","+ cookie.getValue()+","+ cookie.getPath()+","+ cookie.getExpiryDate()+","+ cookie.getSecure()); state.addCookie(cookie); //if (_log.isTraceEnabled()) _log.trace("Cookie: "+cookie.toString()); } } // Proxy headers hm.addRequestHeader("Via", "1.1 " + hreq.getLocalName() + ":" + hreq.getLocalPort() + " \"WADI\""); if (!xForwardedFor) hm.addRequestHeader("X-Forwarded-For", hreq.getRemoteAddr()); // Max-Forwards... // a little bit of cache control // String cache_control = hreq.getHeader("Cache-Control"); // if (cache_control != null && (cache_control.indexOf("no-cache") >= 0 || cache_control.indexOf("no-store") >= 0)) // httpMethod.setUseCaches(false); // customize Connection // uc.setDoInput(true); int client2ServerTotal = 0; if (hasContent) { // uc.setDoOutput(true); try { if (hm instanceof EntityEnclosingMethod) ((EntityEnclosingMethod) hm).setRequestBody(hreq.getInputStream()); // TODO - do we need to close response stream at end... ? } catch (IOException e) { throw new IrrecoverableException("could not pss request input across proxy", e); } } try { HttpClient client = new HttpClient(); HostConfiguration hc = new HostConfiguration(); //String host=location.getAddress().getHostAddress(); // inefficient - but stops httpclient from rejecting half our cookies... String host = uri.getHost(); hc.setHost(host, uri.getPort()); client.executeMethod(hc, hm, state); } catch (IOException e) // TODO { _log.warn("problem proxying connection:", e); } InputStream fromServer = null; // handler status codes etc. int code = 502; // String message="Bad Gateway: could not read server response code or message"; code = hm.getStatusCode(); // IOException // message=hm.getStatusText(); // IOException hres.setStatus(code); // hres.setStatus(code, message); - deprecated... try { fromServer = hm.getResponseBodyAsStream(); // IOException } catch (IOException e) { _log.warn("problem acquiring http client output", e); } // clear response defaults. hres.setHeader("Date", null); hres.setHeader("Server", null); // set response headers // TODO - is it a bug in Jetty that I have to start my loop at 1 ? or that key[0]==null ? // Try this inside Tomcat... Header[] headers = hm.getResponseHeaders(); for (int i = 0; i < headers.length; i++) { String h = headers[i].toExternalForm(); int index = h.indexOf(':'); String key = h.substring(0, index).trim().toLowerCase(); String val = h.substring(index + 1, h.length()).trim(); if (val != null && !_DontProxyHeaders.contains(key)) { hres.addHeader(key, val); // if (_log.isInfoEnabled()) _log.info("Response: "+key+" - "+val); } } hres.addHeader("Via", "1.1 (WADI)"); // copy server->client int server2ClientTotal = 0; if (fromServer != null) { try { OutputStream toClient = hres.getOutputStream();// IOException server2ClientTotal += copy(fromServer, toClient, 8192);// IOException } catch (IOException e) { _log.warn("problem proxying server response back to client", e); } finally { try { fromServer.close(); } catch (IOException e) { // well - we did our best... _log.warn("problem closing server response stream", e); } } } long endTime = System.currentTimeMillis(); long elapsed = endTime - startTime; if (_log.isDebugEnabled()) { _log.debug("in:" + client2ServerTotal + ", out:" + server2ClientTotal + ", status:" + code + ", time:" + elapsed + ", uri:" + uri); } }