List of usage examples for org.apache.commons.httpclient.params HttpMethodParams HttpMethodParams
public HttpMethodParams()
From source file:apm.common.utils.HttpTookit.java
/** * HTTP POST?HTML/*from ww w .jav a 2s . co m*/ * * @param url * URL? * @param params * ?,?null * @param charset * * @param pretty * ? * @return ?HTML */ public static String doPost(String url, Map<String, String> params, String charset, boolean pretty) { StringBuffer response = new StringBuffer(); HttpClient client = new HttpClient(); HttpMethod method = new PostMethod(url); // Http Post? if (params != null) { HttpMethodParams p = new HttpMethodParams(); for (Map.Entry<String, String> entry : params.entrySet()) { p.setParameter(entry.getKey(), entry.getValue()); } method.setParams(p); } try { client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { BufferedReader reader = new BufferedReader( new InputStreamReader(method.getResponseBodyAsStream(), charset)); String line; while ((line = reader.readLine()) != null) { if (pretty) { response.append(line).append(System.getProperty("line.separator")); } else { response.append(line); } } reader.close(); } } catch (IOException e) { log.error("HTTP Post" + url + "??", e); } finally { method.releaseConnection(); } return response.toString(); }
From source file:com.linkedin.pinot.common.utils.FileUploadUtils.java
public static int sendFile(final String host, final String port, final String path, final String fileName, final InputStream inputStream, final long lengthInBytes, SendFileMethod httpMethod) { EntityEnclosingMethod method = null; try {/*from w ww.ja va 2 s .c o m*/ method = httpMethod.forUri("http://" + host + ":" + port + "/" + path); Part[] parts = { new FilePart(fileName, new PartSource() { @Override public long getLength() { return lengthInBytes; } @Override public String getFileName() { return "fileName"; } @Override public InputStream createInputStream() throws IOException { return new BufferedInputStream(inputStream); } }) }; method.setRequestEntity(new MultipartRequestEntity(parts, new HttpMethodParams())); FILE_UPLOAD_HTTP_CLIENT.executeMethod(method); if (method.getStatusCode() >= 400) { String errorString = "POST Status Code: " + method.getStatusCode() + "\n"; if (method.getResponseHeader("Error") != null) { errorString += "ServletException: " + method.getResponseHeader("Error").getValue(); } throw new HttpException(errorString); } return method.getStatusCode(); } catch (Exception e) { LOGGER.error("Caught exception while sending file: {}", fileName, e); Utils.rethrowException(e); throw new AssertionError("Should not reach this"); } finally { if (method != null) { method.releaseConnection(); } } }
From source file:com.linkedin.pinot.common.utils.SchemaUtils.java
/** * Given host, port and schema, send a http POST request to upload the {@link Schema}. * * @return <code>true</code> on success. * <P><code>false</code> on failure. *//*from w w w. jav a2s . c om*/ public static boolean postSchema(@Nonnull String host, int port, @Nonnull Schema schema) { Preconditions.checkNotNull(host); Preconditions.checkNotNull(schema); try { URL url = new URL("http", host, port, "/schemas"); PostMethod httpPost = new PostMethod(url.toString()); try { Part[] parts = { new StringPart(schema.getSchemaName(), schema.toString()) }; MultipartRequestEntity requestEntity = new MultipartRequestEntity(parts, new HttpMethodParams()); httpPost.setRequestEntity(requestEntity); int responseCode = HTTP_CLIENT.executeMethod(httpPost); if (responseCode >= 400) { String response = httpPost.getResponseBodyAsString(); LOGGER.warn("Got error response code: {}, response: {}", responseCode, response); return false; } return true; } finally { httpPost.releaseConnection(); } } catch (Exception e) { LOGGER.error("Caught exception while posting the schema: {} to host: {}, port: {}", schema.getSchemaName(), host, port, e); return false; } }
From source file:com.linkedin.pinot.server.realtime.ServerSegmentCompletionProtocolHandler.java
private SegmentCompletionProtocol.Response doHttp(SegmentCompletionProtocol.Request request, Part[] parts) { SegmentCompletionProtocol.Response response = new SegmentCompletionProtocol.Response( SegmentCompletionProtocol.ControllerResponseStatus.NOT_SENT, -1L); HttpClient httpClient = new HttpClient(); ControllerLeaderLocator leaderLocator = ControllerLeaderLocator.getInstance(); final String leaderAddress = leaderLocator.getControllerLeader(); if (leaderAddress == null) { LOGGER.error("No leader found {}", this.toString()); return new SegmentCompletionProtocol.Response( SegmentCompletionProtocol.ControllerResponseStatus.NOT_LEADER, -1L); }/* w w w.j a va2s .com*/ final String url = request.getUrl(leaderAddress); HttpMethodBase method; if (parts != null) { PostMethod postMethod = new PostMethod(url); postMethod.setRequestEntity(new MultipartRequestEntity(parts, new HttpMethodParams())); method = postMethod; } else { method = new GetMethod(url); } LOGGER.info("Sending request {} for {}", url, this.toString()); try { int responseCode = httpClient.executeMethod(method); if (responseCode >= 300) { LOGGER.error("Bad controller response code {} for {}", responseCode, this.toString()); return response; } else { response = new SegmentCompletionProtocol.Response(method.getResponseBodyAsString()); LOGGER.info("Controller response {} for {}", response.toJsonString(), this.toString()); if (response.getStatus().equals(SegmentCompletionProtocol.ControllerResponseStatus.NOT_LEADER)) { leaderLocator.refreshControllerLeader(); } return response; } } catch (IOException e) { LOGGER.error("IOException {}", this.toString(), e); leaderLocator.refreshControllerLeader(); return response; } }
From source file:com.boyuanitsm.pay.alipay.util.httpClient.HttpProtocolHandler.java
/** * Http/*from ww w. j av a 2s . co m*/ * * @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:com.zimbra.cs.service.FeedManager.java
private static RemoteDataInfo retrieveRemoteData(String url, Folder.SyncData fsd) throws ServiceException, HttpException, IOException { assert !Strings.isNullOrEmpty(url); HttpClient client = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient(); HttpProxyUtil.configureProxy(client); // cannot set connection timeout because it'll affect all HttpClients associated with the conn mgr. // see comments in ZimbraHttpConnectionManager // client.setConnectionTimeout(10000); HttpMethodParams params = new HttpMethodParams(); params.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, MimeConstants.P_CHARSET_UTF8); params.setSoTimeout(60000);//w ww. ja va 2 s .c o m GetMethod get = null; BufferedInputStream content = null; long lastModified = 0; String expectedCharset = MimeConstants.P_CHARSET_UTF8; int redirects = 0; int statusCode = HttpServletResponse.SC_NOT_FOUND; try { do { String lcurl = url.toLowerCase(); if (lcurl.startsWith("webcal:")) { url = "http:" + url.substring(7); } else if (lcurl.startsWith("feed:")) { url = "http:" + url.substring(5); } else if (!lcurl.startsWith("http:") && !lcurl.startsWith("https:")) { throw ServiceException.INVALID_REQUEST("url must begin with http: or https:", null); } // username and password are encoded in the URL as http://user:pass@host/... if (url.indexOf('@') != -1) { HttpURL httpurl = lcurl.startsWith("https:") ? new HttpsURL(url) : new HttpURL(url); if (httpurl.getUser() != null) { String user = httpurl.getUser(); if (user.indexOf('%') != -1) { try { user = URLDecoder.decode(user, "UTF-8"); } catch (OutOfMemoryError e) { Zimbra.halt("out of memory", e); } catch (Throwable t) { } } UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, httpurl.getPassword()); client.getParams().setAuthenticationPreemptive(true); client.getState().setCredentials(AuthScope.ANY, creds); } } try { get = new GetMethod(url); } catch (OutOfMemoryError e) { Zimbra.halt("out of memory", e); return null; } catch (Throwable t) { throw ServiceException.INVALID_REQUEST("invalid url for feed: " + url, t); } get.setParams(params); get.setFollowRedirects(true); get.setDoAuthentication(true); get.addRequestHeader("User-Agent", HTTP_USER_AGENT); get.addRequestHeader("Accept", HTTP_ACCEPT); if (fsd != null && fsd.getLastSyncDate() > 0) { String lastSyncAt = org.apache.commons.httpclient.util.DateUtil .formatDate(new Date(fsd.getLastSyncDate())); get.addRequestHeader("If-Modified-Since", lastSyncAt); } HttpClientUtil.executeMethod(client, get); Header locationHeader = get.getResponseHeader("location"); if (locationHeader != null) { // update our target URL and loop again to do another HTTP GET url = locationHeader.getValue(); get.releaseConnection(); } else { statusCode = get.getStatusCode(); if (statusCode == HttpServletResponse.SC_OK) { Header contentEncoding = get.getResponseHeader("Content-Encoding"); InputStream respInputStream = get.getResponseBodyAsStream(); if (contentEncoding != null) { if (contentEncoding.getValue().indexOf("gzip") != -1) { respInputStream = new GZIPInputStream(respInputStream); } } content = new BufferedInputStream(respInputStream); expectedCharset = get.getResponseCharSet(); Header lastModHdr = get.getResponseHeader("Last-Modified"); if (lastModHdr == null) { lastModHdr = get.getResponseHeader("Date"); } if (lastModHdr != null) { try { Date d = org.apache.commons.httpclient.util.DateUtil .parseDate(lastModHdr.getValue()); lastModified = d.getTime(); } catch (DateParseException e) { ZimbraLog.misc.warn( "unable to parse Last-Modified/Date header: " + lastModHdr.getValue(), e); lastModified = System.currentTimeMillis(); } } else { lastModified = System.currentTimeMillis(); } } else if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) { ZimbraLog.misc.debug("Remote data at " + url + " not modified since last sync"); return new RemoteDataInfo(statusCode, redirects, null, expectedCharset, lastModified); } else { throw ServiceException.RESOURCE_UNREACHABLE(get.getStatusLine().toString(), null); } break; } } while (++redirects <= MAX_REDIRECTS); } catch (ServiceException ex) { if (get != null) { get.releaseConnection(); } throw ex; } catch (HttpException ex) { if (get != null) { get.releaseConnection(); } throw ex; } catch (IOException ex) { if (get != null) { get.releaseConnection(); } throw ex; } RemoteDataInfo rdi = new RemoteDataInfo(statusCode, redirects, content, expectedCharset, lastModified); rdi.setGetMethod(get); return rdi; }
From source file:com.vmware.aurora.vc.VcFileManager.java
static private void uploadFileWork(String url, boolean isPost, File file, String contentType, String cookie, ProgressListener listener) throws Exception { EntityEnclosingMethod method;//from w w w . j a v a2 s . c o m final RequestEntity entity = new ProgressListenerRequestEntity(file, contentType, listener); if (isPost) { method = new PostMethod(url); method.setContentChunked(true); } else { method = new PutMethod(url); method.addRequestHeader("Cookie", cookie); method.setContentChunked(false); HttpMethodParams params = new HttpMethodParams(); params.setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true); method.setParams(params); } method.setRequestEntity(entity); logger.info("upload " + file + " to " + url); long t1 = System.currentTimeMillis(); boolean ok = false; try { HttpClient httpClient = new HttpClient(); int statusCode = httpClient.executeMethod(method); String response = method.getResponseBodyAsString(100); logger.debug("status: " + statusCode + " response: " + response); if (statusCode != HttpStatus.SC_CREATED && statusCode != HttpStatus.SC_OK) { throw new Exception("Http post failed"); } method.releaseConnection(); ok = true; } finally { if (!ok) { method.abort(); } } long t2 = System.currentTimeMillis(); logger.info("upload " + file + " done in " + (t2 - t1) + " ms"); }
From source file:net.cbtltd.server.WebService.java
/** * Gets the HTTP response.// w w w.ja v a2s .c om * * @param url the url of the service. * @param nameids the parameter key value pairs. * @return the HTTP response string. * @throws Throwable the exception that can be thrown. */ public static final String getHttpResponse(String url, NameId[] nameids) throws Throwable { GetMethod get = new GetMethod(url); get.addRequestHeader("Accept", "text/plain"); HttpMethodParams params = new HttpMethodParams(); get.setParams(params); for (NameId nameid : nameids) { params.setParameter(nameid.getId(), nameid.getName()); } HttpClient httpclient = new HttpClient(); int rs = httpclient.executeMethod(get); LOG.debug("rs=" + rs + " " + get.getResponseBodyAsString()); return get.getResponseBodyAsString(); }
From source file:de.innovationgate.wgpublisher.webtml.Include.java
private void performURLInclude() throws TMLException { Status status = (Status) getStatus(); try {/*from www . j ava 2 s . com*/ HttpClient client = WGFactory.getHttpClientFactory().createHttpClient(); HttpMethodParams methodParams = new HttpMethodParams(); methodParams.setSoTimeout(Integer.parseInt(getTimeout())); methodParams.setVersion(HttpVersion.HTTP_1_1); GetMethod getMethod = new GetMethod(); getMethod.setURI(new URI(status.ref, false)); getMethod.setParams(methodParams); getMethod.setFollowRedirects(true); int httpStatus = client.executeMethod(getMethod); if (httpStatus != HttpServletResponse.SC_OK) { throw new TMLException("Response status " + httpStatus + " (" + getMethod.getStatusText() + ") for included URL " + ref, true); } String encoding = getEncoding(); if (encoding == null) { encoding = getMethod.getResponseCharSet(); if (encoding == null) { getTMLContext().addwarning("No encoding returned from URL '" + status.ref + "'. Assuming default encoding " + encoding); encoding = getCore().getCharacterEncoding(); } } Reader reader = new InputStreamReader(getMethod.getResponseBodyAsStream(), encoding); StringWriter writer = new StringWriter(); char[] buf = new char[2048]; long count = 0; String limitStr = getLimit(); long charLimit = 0; try { charLimit = Math.round(1024 * 1024 * Double.parseDouble(limitStr)); } catch (NumberFormatException e) { throw new TMLException("Cannot parse limit attribute as number: " + limitStr); } int len; while ((len = reader.read(buf)) != -1) { writer.write(buf, 0, len); count += len; if (charLimit != 0 && count > charLimit) { throw new TMLException("Include of URL '" + status.ref + "' reaches content limit of " + limitStr + " million characters. Include is cancelled."); } } this.setResult(writer.toString()); } catch (java.io.IOException exc) { log.error("Exception including url", exc); this.addWarning("Exception while including url: " + exc.getMessage()); } }
From source file:games.strategy.triplea.pbem.AxisAndAlliesForumPoster.java
public boolean postTurnSummary(final String message, final String subject) { try {/*from w w w .j a v a2 s. co m*/ login(); // Now we load the post page, and find the hidden fields needed to post final GetMethod get = new GetMethod( "http://www.axisandallies.org/forums/index.php?action=post;topic=" + m_topicId + ".0"); int status = m_client.executeMethod(m_hostConfiguration, get, m_httpState); String body = get.getResponseBodyAsString(); if (status == 200) { String numReplies; String seqNum; String sc; Matcher m = NUM_REPLIES_PATTERN.matcher(body); if (m.matches()) { numReplies = m.group(1); } else { throw new Exception("Hidden field 'num_replies' not found on page"); } m = SEQ_NUM_PATTERN.matcher(body); if (m.matches()) { seqNum = m.group(1); } else { throw new Exception("Hidden field 'seqnum' not found on page"); } m = SC_PATTERN.matcher(body); if (m.matches()) { sc = m.group(1); } else { throw new Exception("Hidden field 'sc' not found on page"); } // now we have the required hidden fields to reply to final PostMethod post = new PostMethod( "http://www.axisandallies.org/forums/index.php?action=post2;start=0;board=40"); try { // Construct the multi part post final List<Part> parts = new ArrayList<Part>(); parts.add(createStringPart("topic", m_topicId)); parts.add(createStringPart("subject", subject)); parts.add(createStringPart("icon", "xx")); parts.add(createStringPart("message", message)); // If the user has chosen to receive notifications, ensure this setting is passed on parts.add(createStringPart("notify", NOTIFY_PATTERN.matcher(body).matches() ? "1" : "0")); if (m_includeSaveGame && m_saveGameFile != null) { final FilePart part = new FilePart("attachment[]", m_saveGameFileName, m_saveGameFile); part.setContentType("application/octet-stream"); part.setTransferEncoding(null); part.setCharSet(null); parts.add(part); } parts.add(createStringPart("post", "Post")); parts.add(createStringPart("num_replies", numReplies)); parts.add(createStringPart("additional_options", "1")); parts.add(createStringPart("sc", sc)); parts.add(createStringPart("seqnum", seqNum)); final MultipartRequestEntity entity = new MultipartRequestEntity( parts.toArray(new Part[parts.size()]), new HttpMethodParams()); post.setRequestEntity(entity); // add headers post.addRequestHeader("Referer", "http://www.axisandallies.org/forums/index.php?action=post;topic=" + m_topicId + ".0;num_replies=" + numReplies); post.addRequestHeader("Accept", "*/*"); try { // the site has spam prevention which means you can't post until 15 seconds after login Thread.sleep(15 * 1000); } catch (final InterruptedException ie) { ie.printStackTrace(); // this should never happen } post.setFollowRedirects(false); status = m_client.executeMethod(m_hostConfiguration, post, m_httpState); body = post.getResponseBodyAsString(); if (status == 302) { // site responds with a 302 redirect back to the forum index (board=40) // The syntax for post is ".....topic=xx.yy" where xx is the thread id, and yy is the post number in the given thread // since the site is lenient we can just give a high post_number to go to the last post in the thread m_turnSummaryRef = "http://www.axisandallies.org/forums/index.php?topic=" + m_topicId + ".10000"; } else { // these two patterns find general errors, where the first pattern checks if the error text appears, // the second pattern extracts the error message. This could be the "The last posting from your IP was less than 15 seconds ago.Please try again later" // this patter finds errors that are marked in red (for instance "You are not allowed to post URLs", or // "Some one else has posted while you vere reading" Matcher matcher = ERROR_LIST_PATTERN.matcher(body); if (matcher.matches()) { throw new Exception("The site gave an error: '" + matcher.group(1) + "'"); } matcher = AN_ERROR_OCCURRED_PATTERN.matcher(body); if (matcher.matches()) { matcher = ERROR_TEXT_PATTERN.matcher(body); if (matcher.matches()) { throw new Exception("The site gave an error: '" + matcher.group(1) + "'"); } } final Header refreshHeader = post.getResponseHeader("Refresh"); if (refreshHeader != null) { // sometimes the message will be flagged as spam, and a refresh url is given final String value = refreshHeader.getValue(); // refresh: 0; URL=http://...topic=26114.new%3bspam=true#new final Pattern p = Pattern.compile("[^;]*;\\s*url=.*spam=true.*", Pattern.DOTALL | Pattern.CASE_INSENSITIVE); m = p.matcher(value); if (m.matches()) { throw new Exception("The summary was posted but was flagged as spam"); } } throw new Exception( "Unknown error, please contact the forum owner and also post a bug to the tripleA development team"); } } finally { post.releaseConnection(); // Commented out log out call since it was causing all of a user's sessions to be logged out and doesn't appear to be needed // final GetMethod logout = new GetMethod("http://www.axisandallies.org/forums/index.php?action=logout;sesc=" + sc); // try // { // status = m_client.executeMethod(m_hostConfiguration, logout, m_httpState); // // site responds with a 200 + Refresh header to redirect to index.php // if (status != 200) // { // // nothing we can do if this fails // } // } finally // { // logout.releaseConnection(); // } } } else { throw new Exception("Unable to load forum post " + m_topicId); } } catch (final Exception e) { m_turnSummaryRef = e.getMessage(); return false; } return true; }