List of usage examples for org.apache.commons.httpclient.methods PostMethod setContentChunked
public void setContentChunked(boolean paramBoolean)
From source file:org.geoserver.gss.HTTPGSSClient.java
public void postDiff(PostDiffType postDiff) throws IOException { // prepare the encoder Encoder encoder = buildEncoderForTransaction(postDiff.getTransaction()); // prepare POST request PostMethod method = new PostMethod(address.toExternalForm()); method.setContentChunked(true); method.setRequestEntity(new XMLEntity(postDiff, GSS.PostDiff, encoder)); // execute the request and interpret the parsed response Object response = executeMethod(method); if (response instanceof PostDiffResponseType) { // that's fine, it's like a placeholder for now } else {/*from ww w .ja v a2 s . co m*/ if (response == null) { throw new IOException("The response was parsed to a null object"); } throw new IOException("The response was parsed to an unrecognized object type: " + response.getClass()); } }
From source file:org.jboss.mod_cluster.Client.java
public int runit() throws Exception { PostMethod pm = null; GetMethod gm = null;//from ww w. jav a 2s . co m HttpMethodBase bm = null; long starttime, endtime; if (httpClient == null) httpClient = new HttpClient(); if (fd != null) { pm = new PostMethod(URL); // InputStreamRequestEntity buf = new InputStreamRequestEntity(fd); // XXX: Ugly hack to test... byte[] buffet = new byte[6144]; for (int i = 0; i < buffet.length; i++) buffet[i] = 'a'; ByteArrayRequestEntity buf = new ByteArrayRequestEntity(buffet); pm.setRequestEntity(buf); // pm.setRequestBody(fd); pm.setHttp11(true); pm.setContentChunked(true); // pm.setRequestContentLength(PostMethod.CONTENT_LENGTH_CHUNKED); bm = pm; } else if (post != null) { pm = new PostMethod(URL); pm.setRequestEntity(new StringRequestEntity(post, "application/x-www-form-urlencoded", "UTF8")); bm = pm; } else { gm = new GetMethod(URL); bm = gm; } if (user != null) { Credentials cred = new UsernamePasswordCredentials(user, pass); httpClient.getState().setCredentials(org.apache.commons.httpclient.auth.AuthScope.ANY, cred); } // System.out.println("Connecting to " + URL); Integer connectionTimeout = 40000; bm.getParams().setParameter("http.socket.timeout", connectionTimeout); bm.getParams().setParameter("http.connection.timeout", connectionTimeout); if (VirtualHost != null) bm.getParams().setVirtualHost(VirtualHost); httpClient.getParams().setParameter("http.socket.timeout", connectionTimeout); httpClient.getParams().setParameter("http.connection.timeout", connectionTimeout); if (jsessionid != null) { // System.out.println("jsessionid: " + jsessionid); bm.setRequestHeader("Cookie", "JSESSIONID=" + jsessionid); } try { if (gm == null) { pm.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY); starttime = System.currentTimeMillis(); httpResponseCode = httpClient.executeMethod(pm); endtime = System.currentTimeMillis(); } else { gm.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY); starttime = System.currentTimeMillis(); httpResponseCode = httpClient.executeMethod(gm); endtime = System.currentTimeMillis(); } if (httpResponseCode == 200) { response = bm.getResponseBodyAsString(); Cookie[] cookies = httpClient.getState().getCookies(); // System.out.println( "Cookies: " + cookies); if (cookies != null && cookies.length != 0) { for (int i = 0; i < cookies.length; i++) { Cookie cookie = cookies[i]; // System.out.println( "Cookie: " + cookie.getName() + ", Value: " + cookie.getValue()); if (cookie.getName().equals("JSESSIONID")) { if (jsessionid == null) { jsessionid = cookie.getValue(); String nodes[] = jsessionid.split("\\."); if (nodes.length == 2) node = nodes[1]; System.out.println("cookie first time: " + jsessionid); bm.releaseConnection(); return 0; // first time ok. } else { if (jsessionid.compareTo(cookie.getValue()) == 0) { if (logok) if (bm.getResponseHeader("Date") != null) System.out.println("cookie ok: " + bm.getResponseHeader("Date").toString().replace('\r', ' ') .replace('\n', ' ') + " response time: " + (endtime - starttime)); else { DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = new Date(); System.out.println("cookie ok: " + dateFormat.format(date) + " response time: " + (endtime - starttime)); } bm.releaseConnection(); return 0; } else { System.out.println( "cookie \"second\" time: " + cookie.getValue() + " : " + jsessionid); System.out.println("cookie changed"); bm.releaseConnection(); if (checkcookie) return -1; else if (checknode) { String nodes[] = cookie.getValue().split("\\."); if (nodes.length != 2) { System.out.println("Can't find node in cookie"); return -1; } if (nodes[1].compareTo(node) == 0) { return 0; } else { System.out.println("node " + nodes[1] + " changed too"); return -1; } } else return 0; } } } } } else { // Look in the response to make sure that there is a cookie. int len = (int) bm.getResponseContentLength(); if (jsessionid != null && bm.getResponseBodyAsString(len).indexOf(jsessionid) != -1) { bm.releaseConnection(); return 0; } if (jsessionid == null && !checkcookie) { return 0; } System.out.println("No cookies"); } Header head = bm.getResponseHeader("getRequestedSessionId"); if (head != null) { HeaderElement[] heade = head.getElements(); requestedSessionId = heade[0].getValue(); } else { requestedSessionId = null; } } else { System.out.println("response: " + httpResponseCode); System.out.println("response: " + bm.getStatusLine()); response = bm.getResponseBodyAsString(); System.out.println("response: " + response); success = false; httpClient = null; } // System.out.println("response:\n" + bm.getResponseBodyAsString(len)); } catch (HttpException e) { e.printStackTrace(); success = false; httpClient = null; } System.out.println("DONE: " + httpResponseCode); bm.releaseConnection(); return httpResponseCode; }
From source file:org.ybygjy.httpclient.ChunkEncodedPost.java
public static void main(String[] args) throws Exception { /*/* w w w .ja va2 s .c o m*/ if (args.length != 1) { System.out.println("Usage: ChunkEncodedPost <file>"); System.out.println("<file> - full path to a file to be posted"); System.exit(1); } */ HttpClient client = new HttpClient(); PostMethod httppost = new PostMethod("http://localhost:8080/org.ybygjy.web.servlet.HttpClientServlet"); File file = new File("D:\\DEV\\04_mywork\\001_mywork\\src\\org\\ybygjy\\httpclient\\ChunkEncodedPost.java"); httppost.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(file))); httppost.setContentChunked(true); try { int rtnFlag = client.executeMethod(httppost); System.out.println("rtnFlag==>" + rtnFlag); if (httppost.getStatusCode() == HttpStatus.SC_OK) { System.out.println(httppost.getResponseBodyAsString()); } else { System.out.println("Unexpected failure: " + httppost.getStatusLine().toString()); } Header[] headers = httppost.getResponseHeaders(); for (Header header : headers) { System.out.println(header.getName() + ":" + header.getValue()); } System.out.println("+"); System.out.println(new String(httppost.getResponseBody())); } finally { httppost.releaseConnection(); } }
From source file:org.yccheok.jstock.gui.UtilsRef.java
/** * Get response body through non-standard POST method. * Please refer to <url>http://stackoverflow.com/questions/1473255/is-jakarta-httpclient-sutitable-for-the-following-task/1473305#1473305</url> * * @param uri For example, http://X/%5bvUpJYKw4QvGRMBmhATUxRwv4JrU9aDnwNEuangVyy6OuHxi2YiY=%5dImage? * @param formData For example, [SORT]=0,1,0,10,5,0,KL,0&[FIELD]=33,38,51 * @return the response body. null if fail. *//*from ww w. j a v a 2 s . c om*/ public static String getPOSTResponseBodyAsStringBasedOnProxyAuthOption(String uri, String formData) { ///org.yccheok.jstock.engine.Utils.setHttpClientProxyFromSystemProperties(httpClient); org.yccheok.jstock.gui.UtilsRef.setHttpClientProxyCredentialsFromJStockOptions(httpClient); final PostMethod method = new PostMethod(uri); final RequestEntity entity; try { entity = new StringRequestEntity(formData, "application/x-www-form-urlencoded", "UTF-8"); } catch (UnsupportedEncodingException exp) { log.error(null, exp); return null; } method.setRequestEntity(entity); method.setContentChunked(false); ///final JStockOptions jStockOptions = MainFrame.getInstance().getJStockOptions(); String respond = null; try { if (false/*jStockOptions.isProxyAuthEnabled()*/) { /* WARNING : This chunck of code block is not tested! */ method.setFollowRedirects(false); httpClient.executeMethod(method); int statuscode = method.getStatusCode(); if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) || (statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) { //Make new Request with new URL Header header = method.getResponseHeader("location"); /* WARNING : Correct method to redirect? Shall we use POST? How about form data? */ HttpMethod RedirectMethod = new GetMethod(header.getValue()); // I assume it is OK to release method for twice. (The second // release will happen in finally block). We shouldn't have an // unreleased method, before executing another new method. method.releaseConnection(); // Do RedirectMethod within try-catch-finally, so that we can have a // exception free way to release RedirectMethod connection. // #2836422 try { httpClient.executeMethod(RedirectMethod); respond = RedirectMethod.getResponseBodyAsString(); } catch (HttpException exp) { log.error(null, exp); return null; } catch (IOException exp) { log.error(null, exp); return null; } finally { RedirectMethod.releaseConnection(); } } else { respond = method.getResponseBodyAsString(); } // if statuscode = Redirect } else { httpClient.executeMethod(method); respond = method.getResponseBodyAsString(); } // if jStockOptions.isProxyAuthEnabled() } catch (HttpException exp) { log.error(null, exp); return null; } catch (IOException exp) { log.error(null, exp); return null; } finally { method.releaseConnection(); } return respond; }
From source file:org.yccheok.jstock.gui.Utils.java
/** * Get response body through non-standard POST method. * Please refer to <url>http://stackoverflow.com/questions/1473255/is-jakarta-httpclient-sutitable-for-the-following-task/1473305#1473305</url> * * @param uri For example, http://X/%5bvUpJYKw4QvGRMBmhATUxRwv4JrU9aDnwNEuangVyy6OuHxi2YiY=%5dImage? * @param formData For example, [SORT]=0,1,0,10,5,0,KL,0&[FIELD]=33,38,51 * @return the response body. null if fail. *///from w ww .j a va2 s . co m public static String getPOSTResponseBodyAsStringBasedOnProxyAuthOption(String uri, String formData) { org.yccheok.jstock.engine.Utils.setHttpClientProxyFromSystemProperties(httpClient); org.yccheok.jstock.gui.Utils.setHttpClientProxyCredentialsFromJStockOptions(httpClient); final PostMethod method = new PostMethod(uri); final RequestEntity entity; try { entity = new StringRequestEntity(formData, "application/x-www-form-urlencoded", "UTF-8"); } catch (UnsupportedEncodingException exp) { log.error(null, exp); return null; } method.setRequestEntity(entity); method.setContentChunked(false); final JStockOptions jStockOptions = JStock.instance().getJStockOptions(); String respond = null; try { if (jStockOptions.isProxyAuthEnabled()) { /* WARNING : This chunck of code block is not tested! */ method.setFollowRedirects(false); httpClient.executeMethod(method); int statuscode = method.getStatusCode(); if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) || (statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) { //Make new Request with new URL Header header = method.getResponseHeader("location"); /* WARNING : Correct method to redirect? Shall we use POST? How about form data? */ HttpMethod RedirectMethod = new GetMethod(header.getValue()); // I assume it is OK to release method for twice. (The second // release will happen in finally block). We shouldn't have an // unreleased method, before executing another new method. method.releaseConnection(); // Do RedirectMethod within try-catch-finally, so that we can have a // exception free way to release RedirectMethod connection. // #2836422 try { httpClient.executeMethod(RedirectMethod); respond = RedirectMethod.getResponseBodyAsString(); } catch (HttpException exp) { log.error(null, exp); return null; } catch (IOException exp) { log.error(null, exp); return null; } finally { RedirectMethod.releaseConnection(); } } else { respond = method.getResponseBodyAsString(); } // if statuscode = Redirect } else { httpClient.executeMethod(method); respond = method.getResponseBodyAsString(); } // if jStockOptions.isProxyAuthEnabled() } catch (HttpException exp) { log.error(null, exp); return null; } catch (IOException exp) { log.error(null, exp); return null; } finally { method.releaseConnection(); } return respond; }