List of usage examples for org.apache.commons.httpclient HttpMethod getPath
public abstract String getPath();
From source file:TrivialApp.java
public static void main(String[] args) { if ((args.length != 1) && (args.length != 3)) { printUsage();// ww w. j a v a 2 s . c o m System.exit(-1); } Credentials creds = null; if (args.length >= 3) { creds = new UsernamePasswordCredentials(args[1], args[2]); } //create a singular HttpClient object HttpClient client = new HttpClient(); //establish a connection within 5 seconds client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); //set the default credentials if (creds != null) { client.getState().setCredentials(AuthScope.ANY, creds); } String url = args[0]; HttpMethod method = null; //create a method object method = new GetMethod(url); method.setFollowRedirects(true); //} 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(); System.exit(0); }
From source file:ExampleP2PHttpClient.java
public static void main(String[] args) { // initialize JXTA try {// w w w . j a va 2 s.c om // 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:com.cloud.utils.net.HTTPUtils.java
/** * @param httpClient//from w w w.j a v a2 s . c om * @param httpMethod * @return * Returns the HTTP Status Code or -1 if an exception occurred. */ public static int executeMethod(HttpClient httpClient, HttpMethod httpMethod) { // Execute GetMethod try { return httpClient.executeMethod(httpMethod); } catch (IOException e) { LOGGER.warn("Exception while executing HttpMethod " + httpMethod.getName() + " on URL " + httpMethod.getPath()); return -1; } }
From source file:lucee.commons.net.http.httpclient3.HttpMethodCloner.java
/** * Clones a HttpMethod. <br>// ww w .ja v a2 s .com * <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:com.feilong.tools.net.httpclient3.HttpClientUtil.java
/** * ?log.//w ww . j av a2 s. c o m * * @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:fr.cls.atoll.motu.library.cas.HttpClientCAS.java
public static String debugHttpMethod(HttpMethod method) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("\nName:"); stringBuffer.append(method.getName()); stringBuffer.append("\n"); stringBuffer.append("\nPath:"); stringBuffer.append(method.getPath()); stringBuffer.append("\n"); stringBuffer.append("\nQueryString:"); stringBuffer.append(method.getQueryString()); stringBuffer.append("\n"); stringBuffer.append("\nUri:"); try {/*ww w. ja v a 2 s . c om*/ stringBuffer.append(method.getURI().toString()); } catch (URIException e) { // Do nothing } stringBuffer.append("\n"); HttpMethodParams httpMethodParams = method.getParams(); stringBuffer.append("\nHttpMethodParams:"); stringBuffer.append(httpMethodParams.toString()); return stringBuffer.toString(); }
From source file:com.wafersystems.util.HttpUtil.java
/** * URL?//from w w w . j a v a 2 s .c o 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:lucee.commons.net.http.httpclient3.HTTPEngine3Impl.java
/** * rewrite request method/* w ww.j a v a 2s . c o m*/ * @param method * @return * @throws MalformedURLException */ private static HttpMethod rewrite(HttpMethod method) throws MalformedURLException { org.apache.commons.httpclient.Header location = method.getResponseHeader("location"); if (location == null) return method; HostConfiguration config = method.getHostConfiguration(); URL url; try { url = new URL(location.getValue()); } catch (MalformedURLException e) { url = new URL(config.getProtocol().getScheme(), config.getHost(), config.getPort(), mergePath(method.getPath(), location.getValue())); } method = clone(method, url); return method; }
From source file:eu.eco2clouds.api.bonfire.client.rest.RestClient.java
private static Response executeMethod(HttpMethod httpMethod, String type, String username, String password, String url) {/*from w w w. j a v a2s. com*/ HttpClient client = getClient(url, username, password); Response response = null; // We set the Heathers httpMethod.setDoAuthentication(true); httpMethod.addRequestHeader(BONFIRE_ASSERTED_ID_HEADER, username); //httpMethod.addRequestHeader(BONFIRE_ASSERTED_ID_HEADER, groups); httpMethod.addRequestHeader("Accept", type); httpMethod.addRequestHeader("Content-Type", type); try { int statusCode = client.executeMethod(httpMethod); String responsePayload = httpMethod.getResponseBodyAsString(); response = new Response(statusCode, responsePayload); } catch (IOException iOException) { System.out.println("ERROR TRYING TO PERFORM GET TO: " + httpMethod.getPath()); System.out.println("ERROR: " + iOException.getMessage()); System.out.println("ERROR: " + iOException.getStackTrace()); } finally { httpMethod.releaseConnection(); } return response; }
From source file:davmail.http.DavGatewayHttpClientFacade.java
/** * Build Http Exception from methode status * * @param method Http Method//from w w w . ja va2 s . c om * @return Http Exception */ public static HttpException buildHttpException(HttpMethod method) { int status = method.getStatusCode(); StringBuilder message = new StringBuilder(); message.append(status).append(' ').append(method.getStatusText()); try { message.append(" at ").append(method.getURI().getURI()); if (method instanceof CopyMethod || method instanceof MoveMethod) { message.append(" to ").append(method.getRequestHeader("Destination")); } } catch (URIException e) { message.append(method.getPath()); } // 440 means forbidden on Exchange if (status == 440) { return new LoginTimeoutException(message.toString()); } else if (status == HttpStatus.SC_FORBIDDEN) { return new HttpForbiddenException(message.toString()); } else if (status == HttpStatus.SC_NOT_FOUND) { return new HttpNotFoundException(message.toString()); } else if (status == HttpStatus.SC_PRECONDITION_FAILED) { return new HttpPreconditionFailedException(message.toString()); } else if (status == HttpStatus.SC_INTERNAL_SERVER_ERROR) { return new HttpServerErrorException(message.toString()); } else { return new HttpException(message.toString()); } }