List of usage examples for org.apache.commons.httpclient HttpMethod getParams
public abstract HttpMethodParams getParams();
From source file:ch.ksfx.web.services.spidering.http.HttpClientHelper.java
private HttpMethod executeMethod(HttpMethod httpMethod) { for (Header header : this.headers.getHeaders()) { httpMethod.setRequestHeader(header); }/*from w w w. j ava2 s . co m*/ httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new KsfxHttpRetryHandler(retryCount, retryDelay)); try { int tryCount = 0; int statusCode; do { if (tryCount > 1) { httpMethod = createNewHttpMethod(httpMethod); try { if (retryDelay == 0) { retryDelay = DEFAULT_RETRY_DELAY; } Thread.sleep(retryDelay); } catch (InterruptedException e) { logger.severe("InterruptedException"); } } //PROXY Configuration /* if (torify) { String proxyHost = ""; Integer proxyPort = 0; try { proxyHost = SpiderConfiguration.getConfiguration().getString("torifyHost"); proxyPort = SpiderConfiguration.getConfiguration().getInt("torifyPort"); } catch (Exception e) { logger.severe("Cannot get Proxy information"); } this.httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort); } */ statusCode = this.httpClient.executeMethod(httpMethod); tryCount++; } while (!(statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_FORBIDDEN || statusCode == HttpStatus.SC_NOT_FOUND) && tryCount < retryCount); if (statusCode != HttpStatus.SC_OK) { System.out.println("HTTP method failed: " + httpMethod.getStatusLine() + " - " + httpMethod.getURI().toString()); } } catch (HttpException e) { e.printStackTrace(); httpMethod.abort(); try { logger.log(Level.SEVERE, "Redirrex " + e.getClass(), e); if (e.getClass().equals(RedirectException.class)) { logger.log(Level.SEVERE, "Is real redirect exception", e); throw new RuntimeException("HttpRedirectException"); } logger.log(Level.SEVERE, "HTTP protocol error for URL: " + httpMethod.getURI().toString(), e); } catch (URIException e1) { e.printStackTrace(); logger.log(Level.SEVERE, "URI exception", e); } throw new RuntimeException("HttpException"); } catch (IOException e) { try { e.printStackTrace(); logger.log(Level.SEVERE, "HTTP transport error for URL: " + httpMethod.getURI().toString(), e); } catch (URIException e1) { e.printStackTrace(); logger.log(Level.SEVERE, "URI exception", e); } throw new RuntimeException("IOException"); } return httpMethod; }
From source file:net.sourceforge.jwbf.actions.HttpActionClient.java
/** * Process a POST Message.//from w w w.ja v a2 s . c o m * * @param authpost * a * @param cp * a * @return a returning message, not null * @throws IOException on problems * @throws ProcessException on problems * @throws CookieException on problems */ protected String post(HttpMethod authpost, ContentProcessable cp) throws IOException, ProcessException, CookieException { showCookies(client); authpost.getParams().setParameter("http.protocol.content-charset", MediaWikiBot.CHARSET); String out = ""; client.executeMethod(authpost); // Header locationHeader = authpost.getResponseHeader("location"); // if (locationHeader != null) { // authpost.setRequestHeader(locationHeader) ; // } // Usually a successful form-based login results in a redicrect to // another url int statuscode = authpost.getStatusCode(); if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) || (statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) { Header header = authpost.getResponseHeader("location"); if (header != null) { String newuri = header.getValue(); if ((newuri == null) || (newuri.equals(""))) { newuri = "/"; } LOG.debug("Redirect target: " + newuri); GetMethod redirect = new GetMethod(newuri); client.executeMethod(redirect); LOG.debug("Redirect: " + redirect.getStatusLine().toString()); // release any connection resources used by the method authpost.releaseConnection(); authpost = redirect; } } out = authpost.getResponseBodyAsString(); out = cp.processReturningText(out, authpost); cp.validateReturningCookies(client.getState().getCookies(), authpost); authpost.releaseConnection(); LOG.debug(authpost.getURI() + " || " + "POST: " + authpost.getStatusLine().toString()); return out; }
From source file:net.sourceforge.jwbf.actions.HttpActionClient.java
/** * Process a GET Message.//from ww w .j a va 2 s .com * * @param authgets * a * @param cp * a * @return a returning message, not null * @throws IOException on problems * @throws CookieException on problems * @throws ProcessException on problems */ protected String get(HttpMethod authgets, ContentProcessable cp) throws IOException, CookieException, ProcessException { showCookies(client); String out = ""; authgets.getParams().setParameter("http.protocol.content-charset", MediaWikiBot.CHARSET); // System.err.println(authgets.getParams().getParameter("http.protocol.content-charset")); client.executeMethod(authgets); cp.validateReturningCookies(client.getState().getCookies(), authgets); LOG.debug(authgets.getURI()); LOG.debug("GET: " + authgets.getStatusLine().toString()); out = authgets.getResponseBodyAsString(); out = cp.processReturningText(out, authgets); // release any connection resources used by the method authgets.releaseConnection(); int statuscode = authgets.getStatusCode(); if (statuscode == HttpStatus.SC_NOT_FOUND) { LOG.warn("Not Found: " + authgets.getQueryString()); throw new FileNotFoundException(authgets.getQueryString()); } return out; }
From source file:com.yahoo.flowetl.services.http.BaseHttpGenerator.java
/** * Applies any common properties to the http method based on properties of * the http params given./*from ww w. java 2 s . com*/ */ protected void applyCommonProperties(HttpParams in, HttpMethod toCall) { // common ops if (in.headers != null) { for (Entry<String, String> e : in.headers.entrySet()) { toCall.addRequestHeader(e.getKey(), e.getValue()); } } // we handle our own retries toCall.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); if (StringUtils.isBlank(in.userAgent) == false && toCall.getRequestHeader(USER_AGENT_HEADER) == null) { toCall.setRequestHeader(USER_AGENT_HEADER, in.userAgent); } else if (toCall.getRequestHeader(USER_AGENT_HEADER) == null) { toCall.setRequestHeader(USER_AGENT_HEADER, userAgent); } }
From source file:com.boyuanitsm.pay.alipay.util.httpClient.HttpProtocolHandler.java
/** * Http//from w w w . j av a 2 s .c om * * @param request ? * @param strParaFileName ??? * @param strFilePath * @return * @throws HttpException, IOException */ public HttpResponse execute(HttpRequest request, String strParaFileName, String strFilePath) throws HttpException, IOException { HttpClient httpclient = new HttpClient(connectionManager); // int connectionTimeout = defaultConnectionTimeout; if (request.getConnectionTimeout() > 0) { connectionTimeout = request.getConnectionTimeout(); } httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout); // int soTimeout = defaultSoTimeout; if (request.getTimeout() > 0) { soTimeout = request.getTimeout(); } httpclient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout); // ConnectionManagerconnection httpclient.getParams().setConnectionManagerTimeout(defaultHttpConnectionManagerTimeout); String charset = request.getCharset(); charset = charset == null ? DEFAULT_CHARSET : charset; HttpMethod method = null; //get?? if (request.getMethod().equals(HttpRequest.METHOD_GET)) { method = new GetMethod(request.getUrl()); method.getParams().setCredentialCharset(charset); // parseNotifyConfig??GETrequestQueryString method.setQueryString(request.getQueryString()); } else if (strParaFileName.equals("") && strFilePath.equals("")) { //post?? method = new PostMethod(request.getUrl()); ((PostMethod) method).addParameters(request.getParameters()); method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; text/html; charset=" + charset); } else { //post? method = new PostMethod(request.getUrl()); List<Part> parts = new ArrayList<Part>(); for (int i = 0; i < request.getParameters().length; i++) { parts.add(new StringPart(request.getParameters()[i].getName(), request.getParameters()[i].getValue(), charset)); } //?strParaFileName??? parts.add(new FilePart(strParaFileName, new FilePartSource(new File(strFilePath)))); // ((PostMethod) method).setRequestEntity( new MultipartRequestEntity(parts.toArray(new Part[0]), new HttpMethodParams())); } // Http HeaderUser-Agent method.addRequestHeader("User-Agent", "Mozilla/4.0"); HttpResponse response = new HttpResponse(); try { httpclient.executeMethod(method); if (request.getResultType().equals(HttpResultType.STRING)) { response.setStringResult(method.getResponseBodyAsString()); } else if (request.getResultType().equals(HttpResultType.BYTES)) { response.setByteResult(method.getResponseBody()); } response.setResponseHeaders(method.getResponseHeaders()); } catch (UnknownHostException ex) { return null; } catch (IOException ex) { return null; } catch (Exception ex) { return null; } finally { method.releaseConnection(); } return response; }
From source file:edu.caltech.ipac.firefly.server.network.HttpServices.java
/** * Execute the given HTTP method with the given parameters. * @param method the function or method to perform * @param cookies optional, sent with request if present. * @return true is the request was successfully received, understood, and accepted (code 2xx). *//* w ww.j av a2s .c om*/ public static boolean executeMethod(HttpMethod method, String userId, String password, Map<String, String> cookies) { try { if (!StringUtils.isEmpty(userId)) { UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userId, password); httpClient.getState().setCredentials(AuthScope.ANY, credentials); } else { // check to see if the userId and password is in the url userId = URLDownload.getUserFromUrl(method.toString()); if (userId != null) { password = URLDownload.getPasswordFromUrl(method.toString()); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userId, password); httpClient.getState().setCredentials(AuthScope.ANY, credentials); } } if (cookies != null) { StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : cookies.entrySet()) { if (sb.length() > 0) sb.append("; "); sb.append(entry.getKey()); sb.append("="); sb.append(entry.getValue()); } if (sb.length() > 0) { method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); method.setRequestHeader("Cookie", sb.toString()); } } int status = httpClient.executeMethod(method); boolean isSuccess = status >= 200 && status < 300; String reqDesc = "URL:" + method.getURI(); if (isSuccess) { LOG.info(reqDesc); } else { reqDesc = reqDesc + "\nREQUEST HEADERS: " + CollectionUtil.toString(method.getRequestHeaders()).replaceAll("\\r|\\n", "") + "\nPARAMETERS:\n " + getDesc(method.getParams()) + "\nRESPONSE HEADERS: " + CollectionUtil.toString(method.getResponseHeaders()).replaceAll("\\r|\\n", ""); LOG.error("HTTP request failed with status:" + status + "\n" + reqDesc); } return isSuccess; } catch (Exception e) { LOG.error(e, "Unable to connect to:" + method.toString()); } return false; }
From source file:com.cerema.cloud2.lib.common.OwnCloudClient.java
/** * Requests the received method.//from ww w .j a v a2s . c o m * * Executes the method through the inherited HttpClient.executedMethod(method). * * @param method HTTP method request. */ @Override public int executeMethod(HttpMethod method) throws IOException { try { // Update User Agent HttpParams params = method.getParams(); String userAgent = OwnCloudClientManagerFactory.getUserAgent(); params.setParameter(HttpMethodParams.USER_AGENT, userAgent); Log_OC.d(TAG + " #" + mInstanceNumber, "REQUEST " + method.getName() + " " + method.getPath()); // logCookiesAtRequest(method.getRequestHeaders(), "before"); // logCookiesAtState("before"); method.setFollowRedirects(false); int status = super.executeMethod(method); if (mFollowRedirects) { status = followRedirection(method).getLastStatus(); } // logCookiesAtRequest(method.getRequestHeaders(), "after"); // logCookiesAtState("after"); // logSetCookiesAtResponse(method.getResponseHeaders()); return status; } catch (IOException e) { //Log_OC.d(TAG + " #" + mInstanceNumber, "Exception occurred", e); throw e; } }
From source file:ch.ksfx.web.services.spidering.http.HttpClientHelper.java
private HttpMethod createNewHttpMethod(HttpMethod oldMethod) throws URIException { HttpMethod httpMethod;/*from w w w .ja va 2 s . co m*/ if (oldMethod instanceof GetMethod) { httpMethod = new GetMethod(); } else { httpMethod = new PostMethod(); ((PostMethod) httpMethod).setRequestEntity(((PostMethod) oldMethod).getRequestEntity()); httpMethod.setParams(oldMethod.getParams()); } httpMethod.setURI(oldMethod.getURI()); httpMethod.setFollowRedirects(oldMethod.getFollowRedirects()); return httpMethod; }
From source file:com.sun.jersey.client.apache.DefaultApacheHttpMethodExecutor.java
@Override public void executeMethod(final HttpMethod method, final ClientRequest cr) { final Map<String, Object> props = cr.getProperties(); method.setDoAuthentication(true);/*from w w w . j a v a2s . c o m*/ final HttpMethodParams methodParams = method.getParams(); // Set the handle cookies property if (!cr.getPropertyAsFeature(ApacheHttpClientConfig.PROPERTY_HANDLE_COOKIES)) { methodParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES); } // Set the interactive and credential provider properties if (cr.getPropertyAsFeature(ApacheHttpClientConfig.PROPERTY_INTERACTIVE)) { CredentialsProvider provider = (CredentialsProvider) props .get(ApacheHttpClientConfig.PROPERTY_CREDENTIALS_PROVIDER); if (provider == null) { provider = DEFAULT_CREDENTIALS_PROVIDER; } methodParams.setParameter(CredentialsProvider.PROVIDER, provider); } else { methodParams.setParameter(CredentialsProvider.PROVIDER, null); } // Set the read timeout final Integer readTimeout = (Integer) props.get(ApacheHttpClientConfig.PROPERTY_READ_TIMEOUT); if (readTimeout != null) { methodParams.setSoTimeout(readTimeout); } if (method instanceof EntityEnclosingMethod) { final EntityEnclosingMethod entMethod = (EntityEnclosingMethod) method; if (cr.getEntity() != null) { final RequestEntityWriter re = getRequestEntityWriter(cr); final Integer chunkedEncodingSize = (Integer) props .get(ApacheHttpClientConfig.PROPERTY_CHUNKED_ENCODING_SIZE); if (chunkedEncodingSize != null) { // There doesn't seems to be a way to set the chunk size. entMethod.setContentChunked(true); // It is not possible for a MessageBodyWriter to modify // the set of headers before writing out any bytes to // the OutputStream // This makes it impossible to use the multipart // writer that modifies the content type to add a boundary // parameter writeOutBoundHeaders(cr.getHeaders(), method); // Do not buffer the request entity when chunked encoding is // set entMethod.setRequestEntity(new RequestEntity() { @Override public boolean isRepeatable() { return false; } @Override public void writeRequest(OutputStream out) throws IOException { re.writeRequestEntity(out); } @Override public long getContentLength() { return re.getSize(); } @Override public String getContentType() { return re.getMediaType().toString(); } }); } else { entMethod.setContentChunked(false); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { re.writeRequestEntity(new CommittingOutputStream(baos) { @Override protected void commit() throws IOException { writeOutBoundHeaders(cr.getMetadata(), method); } }); } catch (IOException ex) { throw new ClientHandlerException(ex); } final byte[] content = baos.toByteArray(); entMethod.setRequestEntity(new RequestEntity() { @Override public boolean isRepeatable() { return true; } @Override public void writeRequest(OutputStream out) throws IOException { out.write(content); } @Override public long getContentLength() { return content.length; } @Override public String getContentType() { return re.getMediaType().toString(); } }); } } else { writeOutBoundHeaders(cr.getHeaders(), method); } } else { writeOutBoundHeaders(cr.getHeaders(), method); // Follow redirects method.setFollowRedirects(cr.getPropertyAsFeature(ApacheHttpClientConfig.PROPERTY_FOLLOW_REDIRECTS)); } try { httpClient.executeMethod(getHostConfiguration(httpClient, props), method, getHttpState(props)); } catch (Exception e) { method.releaseConnection(); throw new ClientHandlerException(e); } }
From source file:com.exalead.io.failover.FailoverHttpClient.java
public int executeMethod(HttpMethod method, int timeout, int retries) throws HttpException, IOException { if (manager.hosts.size() == 0) { logger.error("Could not execute method without any host."); throw new HttpException("Trying to execute methods without host"); }//from w w w. j a va 2 s .co m // Fake config, the underlying manager manages all HostConfiguration config = new HostConfiguration(); /* Set method parameters */ method.getParams().setSoTimeout(timeout); /* DO NOT retry magically the method */ method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); if (manager.getCredentials() != null) { client.getParams().setAuthenticationPreemptive(true); client.getState().setCredentials(new AuthScope(AuthScope.ANY), manager.getCredentials()); } IOException fail = null; for (int i = 1; i <= retries; ++i) { try { return client.executeMethod(config, method); } catch (IOException e) { logger.warn("Failed to execute method - try " + i + "/" + retries); fail = e; continue; } } logger.warn("exception in executeMethod: " + fail.getMessage()); throw fail; }