List of usage examples for org.apache.commons.httpclient HttpStatus SC_MOVED_PERMANENTLY
int SC_MOVED_PERMANENTLY
To view the source code for org.apache.commons.httpclient HttpStatus SC_MOVED_PERMANENTLY.
Click Source Link
From source file:com.leixl.easyframework.action.httpclient.TestHttpPost.java
public static void main(String[] args) { Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("uid", 1); paramMap.put("desc", "?????"); paramMap.put("payStatus", 1); //HttpConnUtils.postHttpContent(URL, paramMap); //HttpClient/*w w w . jav a2 s . c om*/ HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(URL); // ?? NameValuePair[] data = { new NameValuePair("uid", "1"), new NameValuePair("desc", "?????"), new NameValuePair("payStatus", "1") }; // ?postMethod postMethod.setRequestBody(data); // postMethod int statusCode; try { statusCode = httpClient.executeMethod(postMethod); if (statusCode == HttpStatus.SC_OK) { StringBuffer contentBuffer = new StringBuffer(); InputStream in = postMethod.getResponseBodyAsStream(); BufferedReader reader = new BufferedReader( new InputStreamReader(in, postMethod.getResponseCharSet())); String inputLine = null; while ((inputLine = reader.readLine()) != null) { contentBuffer.append(inputLine); System.out.println("input line:" + inputLine); contentBuffer.append("/n"); } in.close(); } else if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { // ??? Header locationHeader = postMethod.getResponseHeader("location"); String location = null; if (locationHeader != null) { location = locationHeader.getValue(); System.out.println("The page was redirected to:" + location); } else { System.err.println("Location field value is null."); } } } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:FormLoginDemo.java
public static void main(String[] args) throws Exception { HttpClient client = new HttpClient(); client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http"); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); // 'developer.java.sun.com' has cookie compliance problems // Their session cookie's domain attribute is in violation of the RFC2109 // We have to resort to using compatibility cookie policy GetMethod authget = new GetMethod("/servlet/SessionServlet"); client.executeMethod(authget);/*from w ww . j a va2 s.c o m*/ System.out.println("Login form get: " + authget.getStatusLine().toString()); // release any connection resources used by the method authget.releaseConnection(); // See if we got any cookies CookieSpec cookiespec = CookiePolicy.getDefaultSpec(); Cookie[] initcookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies()); System.out.println("Initial set of cookies:"); if (initcookies.length == 0) { System.out.println("None"); } else { for (int i = 0; i < initcookies.length; i++) { System.out.println("- " + initcookies[i].toString()); } } PostMethod authpost = new PostMethod("/servlet/SessionServlet"); // Prepare login parameters NameValuePair action = new NameValuePair("action", "login"); NameValuePair url = new NameValuePair("url", "/index.html"); NameValuePair userid = new NameValuePair("UserId", "userid"); NameValuePair password = new NameValuePair("Password", "password"); authpost.setRequestBody(new NameValuePair[] { action, url, userid, password }); client.executeMethod(authpost); System.out.println("Login form post: " + authpost.getStatusLine().toString()); // release any connection resources used by the method authpost.releaseConnection(); // See if we got any cookies // The only way of telling whether logon succeeded is // by finding a session cookie Cookie[] logoncookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies()); System.out.println("Logon cookies:"); if (logoncookies.length == 0) { System.out.println("None"); } else { for (int i = 0; i < logoncookies.length; i++) { System.out.println("- " + logoncookies[i].toString()); } } // 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 = "/"; } System.out.println("Redirect target: " + newuri); GetMethod redirect = new GetMethod(newuri); client.executeMethod(redirect); System.out.println("Redirect: " + redirect.getStatusLine().toString()); // release any connection resources used by the method redirect.releaseConnection(); } else { System.out.println("Invalid redirect"); System.exit(1); } } }
From source file:com.mytutorials.httpclient.FormLoginDemo.java
public static void main(String[] args) throws Exception { HttpClient client = new HttpClient(); client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http"); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); // 'developer.java.sun.com' has cookie compliance problems // Their session cookie's domain attribute is in violation of the // RFC2109// ww w .jav a 2 s. c o m // We have to resort to using compatibility cookie policy GetMethod authget = new GetMethod("/servlet/SessionServlet"); client.executeMethod(authget); System.out.println("Login form get: " + authget.getStatusLine().toString()); // release any connection resources used by the method authget.releaseConnection(); // See if we got any cookies CookieSpec cookiespec = CookiePolicy.getDefaultSpec(); Cookie[] initcookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies()); System.out.println("Initial set of cookies:"); if (initcookies.length == 0) { System.out.println("None"); } else { for (int i = 0; i < initcookies.length; i++) { System.out.println("- " + initcookies[i].toString()); } } PostMethod authpost = new PostMethod("/servlet/SessionServlet"); // Prepare login parameters NameValuePair action = new NameValuePair("action", "login"); NameValuePair url = new NameValuePair("url", "/index.html"); NameValuePair userid = new NameValuePair("UserId", "userid"); NameValuePair password = new NameValuePair("Password", "password"); authpost.setRequestBody(new NameValuePair[] { action, url, userid, password }); client.executeMethod(authpost); System.out.println("Login form post: " + authpost.getStatusLine().toString()); // release any connection resources used by the method authpost.releaseConnection(); // See if we got any cookies // The only way of telling whether logon succeeded is // by finding a session cookie Cookie[] logoncookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies()); System.out.println("Logon cookies:"); if (logoncookies.length == 0) { System.out.println("None"); } else { for (int i = 0; i < logoncookies.length; i++) { System.out.println("- " + logoncookies[i].toString()); } } // 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 = "/"; } System.out.println("Redirect target: " + newuri); GetMethod redirect = new GetMethod(newuri); client.executeMethod(redirect); System.out.println("Redirect: " + redirect.getStatusLine().toString()); // release any connection resources used by the method redirect.releaseConnection(); } else { System.out.println("Invalid redirect"); System.exit(1); } } }
From source file:com.ms.commons.utilities.HttpClientUtils.java
public static String getResponseBodyAsString(HttpMethod method, Integer tryTimes, String responseCharSet, Integer maximumResponseByteSize, Integer soTimeoutMill) { init();/*from w w w . java2 s. c om*/ if (tryTimes == null) { tryTimes = 1; } if (StringUtils.isBlank(responseCharSet)) { responseCharSet = "utf-8"; } if (maximumResponseByteSize == null) { maximumResponseByteSize = 50 * 1024 * 1024; } if (soTimeoutMill == null) { soTimeoutMill = 20000; } method.getParams().setSoTimeout(soTimeoutMill); InputStream httpInputStream = null; for (int i = 0; i < tryTimes; i++) { try { int responseCode = client.executeMethod(method); if (responseCode == HttpStatus.SC_OK || responseCode == HttpStatus.SC_MOVED_PERMANENTLY || responseCode == HttpStatus.SC_MOVED_TEMPORARILY) { if (method instanceof HttpMethodBase) { responseCharSet = ((HttpMethodBase) method).getResponseCharSet(); } int read = 0; byte[] cbuf = new byte[4096]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); httpInputStream = method.getResponseBodyAsStream(); while ((read = httpInputStream.read(cbuf)) >= 0) { baos.write(cbuf, 0, read); if (baos.size() >= maximumResponseByteSize) { break; } } String content = baos.toString(responseCharSet); return content; } logger.error(String.format( "getResponseBodyAsString failed, responseCode: %s, should be 200, 301, 302", responseCode)); return ""; } catch (Exception e) { logger.error("getResponseBodyAsString failed", e); } finally { IOUtils.closeQuietly(httpInputStream); method.releaseConnection(); } } return ""; }
From source file:com.wafersystems.util.HttpUtil.java
/** * ??/*from www . j a v a 2s.c om*/ * * @param ip IP? * @param port ?? * @param hClient HttpClient * @param method HttpMethod * @param lastResult * * @return */ public static int checkRedirect(String ip, int port, HttpClient hClient, HttpMethod method, int lastResult) throws Exception { int result = HttpStatus.SC_OK; if ((lastResult == HttpStatus.SC_MOVED_TEMPORARILY) || (lastResult == HttpStatus.SC_MOVED_PERMANENTLY) || (lastResult == HttpStatus.SC_SEE_OTHER) || (lastResult == HttpStatus.SC_TEMPORARY_REDIRECT)) { //????URL? Header header = method.getResponseHeader("location"); if (header != null) { String newURI = header.getValue(); if (StrUtil.isEmptyStr(newURI)) newURI = "http://" + ip + ":" + port + "/"; //URL??IP?? if (!newURI.startsWith("http")) newURI = "http://" + ip + ":" + port + newURI; logger.warn("??" + newURI); method = new GetMethod(newURI); result = hClient.executeMethod(method); } } return result; }
From source file:com.cerema.cloud2.lib.common.network.RedirectionPath.java
/** * @return Last location followed corresponding to a permanent redirection (status code 301). *//*w w w.ja v a 2 s. c o m*/ public String getLastPermanentLocation() { for (int i = mLastStatus; i >= 0; i--) { if (mStatuses[i] == HttpStatus.SC_MOVED_PERMANENTLY && i <= mLastLocation) { return mLocations[i]; } } return null; }
From source file:fr.dutra.confluence2wordpress.util.UrlUtils.java
private static URI followRedirectsInternal(URI url, int maxRedirections) { URI response = url;//from w ww. j a v a 2 s . c o m HttpClient client = new HttpClient(); DefaultHttpMethodRetryHandler handler = new DefaultHttpMethodRetryHandler(1, false); client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, handler); HttpMethod method = new HeadMethod(url.toASCIIString()); //method.setRequestHeader("Accept-Language", locale.getLanguage() + ",en"); method.setFollowRedirects(false); try { int statusCode = client.executeMethod(method); if ((statusCode == HttpStatus.SC_MOVED_PERMANENTLY) | (statusCode == HttpStatus.SC_MOVED_TEMPORARILY)) { if (maxRedirections > 0) { Header location = method.getResponseHeader("Location"); if (!location.getValue().equals("")) { // recursively check URL until it's not redirected any more // locations can be relative to previous URL URI target = url.resolve(location.getValue()); response = followRedirectsInternal(target, maxRedirections - 1); } } } } catch (Exception e) { //HttpClient can also throw IllegalArgumentException when URLs are malformed } return response; }
From source file:net.sourceforge.jwbf.actions.HttpActionClient.java
/** * Process a POST Message.// w w w . j ava 2s . 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:com.cloud.utils.net.HTTPUtils.java
/** * @param responseCode// w w w. j a v a 2s . c o m * @return */ public static boolean verifyResponseCode(int responseCode) { switch (responseCode) { case HttpStatus.SC_OK: case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_MOVED_TEMPORARILY: return true; default: return false; } }
From source file:fr.unix_experience.owncloud_sms.engine.OCHttpClient.java
private int followRedirections(HttpMethod httpMethod) throws IOException { int redirectionsCount = 0; int status = httpMethod.getStatusCode(); while (redirectionsCount < 3 && (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_TEMPORARY_REDIRECT)) { Header location = httpMethod.getResponseHeader("Location"); if (location == null) { location = httpMethod.getResponseHeader("location"); }// ww w.ja v a2 s . c o m if (location == null) { Log.e(TAG, "No valid location header found when redirecting."); return 500; } try { httpMethod.setURI(new URI(location.getValue())); } catch (URIException e) { Log.e(TAG, "Invalid URI in 302 FOUND response"); return 500; } status = executeMethod(httpMethod); redirectionsCount++; } if (redirectionsCount >= 3 && status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_TEMPORARY_REDIRECT) { Log.e(TAG, "Too many redirection done. Aborting, please ensure your server is " + "correctly configured"); return 400; } return status; }