StringRequestEntity requestEntity = new StringRequestEntity( JSON_STRING, "application/json", "UTF-8"); PostMethod postMethod = new PostMethod("http://example.com/action"); postMethod.setRequestEntity(requestEntity); int statusCode = httpClient.executeMethod(postMethod);
/** * Factory method to create a {@link HttpMethod}-object according to the * given String <code>httpMethod</codde> * * @param httpMethodString the name of the {@link HttpMethod} to create * @param url * * @return an object of type {@link GetMethod}, {@link PutMethod}, * {@link PostMethod} or {@link DeleteMethod} * @throws IllegalArgumentException if <code>httpMethod</code> is none of * <code>GET</code>, <code>PUT</code>, <code>POST</POST> or <code>DELETE</code> */ public static HttpMethod createHttpMethod(String httpMethodString, String url) { if ("GET".equals(httpMethodString)) { return new GetMethod(url); } else if ("PUT".equals(httpMethodString)) { return new PutMethod(url); } else if ("POST".equals(httpMethodString)) { return new PostMethod(url); } else if ("DELETE".equals(httpMethodString)) { return new DeleteMethod(url); } else { throw new IllegalArgumentException("given httpMethod '" + httpMethodString + "' is unknown"); } }
PostMethod post = new PostMethod("http://jakarata.apache.org/"); NameValuePair[] data = { new NameValuePair("user", "joe"), new NameValuePair("password", "bloggs") }; post.setRequestBody(data); // execute method and handle any error responses. ... InputStream in = post.getResponseBodyAsStream(); // handle response.
PostMethod httpPost = new PostMethod(url.toString()); try { Part[] parts = {new StringPart(schema.getSchemaName(), schema.toString())};
/** * Uses the post method to send a url with arguments by http, this method can call RESTful Api. * * @param url the http url * @param timeout milliseconds to wait for the server to respond before giving up * @param processInputStream the response body stream processor */ public static void post(String url, Integer timeout, IProcessInputStream processInputStream) throws IOException { Preconditions.checkNotNull(timeout, "timeout"); Preconditions.checkNotNull(processInputStream, "processInputStream"); PostMethod postMethod = new PostMethod(url); try { HttpClient httpClient = new HttpClient(); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(timeout); httpClient.getHttpConnectionManager().getParams().setSoTimeout(timeout); int statusCode = httpClient.executeMethod(postMethod); if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) { InputStream inputStream = postMethod.getResponseBodyAsStream(); processInputStream.process(inputStream); } else { throw new IOException("Failed to perform POST request. Status code: " + statusCode); } } finally { postMethod.releaseConnection(); } }
PostMethod post = new PostMethod(url); post.addParameter("schema", schema.toString());
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.http.impl.client.HttpClients; public class PostReqEx { public void sendReq(String url,String email,String fname){ HttpClient httpClient = HttpClients.createDefault(); PostMethod postMethod = new PostMethod(url); postMethod.addParameter("Email", email); postMethod.addParameter("fname", fname); try { httpClient.executeMethod(postMethod); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (postMethod.getStatusCode() == HttpStatus.SC_OK) { String resp = postMethod.getResponseBodyAsString(); } else { //...postMethod.getStatusLine(); } } }
public static PostMethod sendMultipartPostRequest(String url, String body) throws IOException { HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(url); // our handlers ignore key...so we can put anything here Part[] parts = {new StringPart("body", body)}; postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams())); httpClient.executeMethod(postMethod); return postMethod; }
/** * Register a schema to the Kafka schema registry under the provided input name. This method will change the name * of the schema to the provided name if configured to do so. This is useful because certain services (like Gobblin kafka adaptor and * Camus) get the schema for a topic by querying for the latest schema with the topic name, requiring the topic * name and schema name to match for all topics. If it is not configured to switch names, this is useful for the case * where the Kafka topic and Avro schema names do not match. This method registers the schema to the schema registry in such a * way that any schema can be written to any topic. * * @param schema {@link org.apache.avro.Schema} to register. * @param name Name of the schema when registerd to the schema registry. This name should match the name * of the topic where instances will be published. * @return schema ID of the registered schema. * @throws SchemaRegistryException if registration failed */ @Override public MD5Digest register(String name, Schema schema) throws SchemaRegistryException { PostMethod post = new PostMethod(url); if (this.switchTopicNames) { return register(AvroUtils.switchName(schema, name), post); } else { post.addParameter("name", name); return register(schema, post); } }
private static Header[] applyHeaders(Settings settings) { HeaderProcessor processor = new HeaderProcessor(settings); HttpMethod method = new PostMethod("http://localhost:9200"); processor.applyTo(method); return method.getRequestHeaders(); }
break; case POST: http = new PostMethod(); break; case PUT:
@Override protected HttpRequest createRequest(String endpointUrl) throws Exception { PostMethod method = new PostMethod(endpointUrl); return new Http3RequestAdapter(method); }
protected HttpMethod createMethod(final String type, final String uri, final int port) throws BigSwitchBcfApiException { String url; try { url = new URL(S_PROTOCOL, host, port, uri).toString(); } catch (MalformedURLException e) { S_LOGGER.error("Unable to build Big Switch API URL", e); throw new BigSwitchBcfApiException("Unable to build Big Switch API URL", e); } if ("post".equalsIgnoreCase(type)) { return new PostMethod(url); } else if ("get".equalsIgnoreCase(type)) { return new GetMethod(url); } else if ("delete".equalsIgnoreCase(type)) { return new DeleteMethod(url); } else if ("put".equalsIgnoreCase(type)) { return new PutMethod(url); } else { throw new BigSwitchBcfApiException("Requesting unknown method type"); } }
@Override public boolean ping(final String callingPeer) throws RemoteException { if (s_logger.isDebugEnabled()) { s_logger.debug("Ping at " + _serviceUrl); } final HttpClient client = getHttpClient(); final PostMethod method = new PostMethod(_serviceUrl); method.addParameter("method", Integer.toString(RemoteMethodConstants.METHOD_PING)); method.addParameter("callingPeer", callingPeer); final String returnVal = executePostMethod(client, method); if ("true".equalsIgnoreCase(returnVal)) { return true; } return false; }
PostMethod method = new PostMethod(url);
@Override public HTTPResponse post( final URL url, final InputStream postContent, final String postContentType) throws IOException { PostMethod postMethod = new PostMethod(url.toExternalForm()); postMethod.setDoAuthentication(user != null && password != null); if (postContentType != null) { postMethod.setRequestHeader("Content-type", postContentType); } RequestEntity requestEntity = new InputStreamRequestEntity(postContent); postMethod.setRequestEntity(requestEntity); int responseCode = client.executeMethod(postMethod); if (200 != responseCode) { postMethod.releaseConnection(); throw new IOException( "Server returned HTTP error code " + responseCode + " for URL " + url.toExternalForm()); } return new HttpMethodResponse(postMethod); }
@Override public void connect() throws ConnectionException { PostMethod post = new PostMethod(conn.getConnectUrl()); HttpClient httpClient = new HttpClient(); if (conn.getConnectUrl().toLowerCase().startsWith("https://")) { registerSsl(); } addPostHeaders(post); try { StringRequestEntity requestEntity = new StringRequestEntity("{}", "application/json", "UTF-8"); post.setRequestEntity(requestEntity); httpClient.executeMethod(post); if (post.getStatusCode() != 200 && post.getStatusCode() != 201) { logger.error("Connect failed with error code " + post.getStatusCode() + " and message:\n" + post.getResponseBodyAsString()); throw new ConnectionException("Connect failed, error code " + post.getStatusCode() + " and message: " + post.getResponseBodyAsString()); } } catch (HttpException e) { logger.error(e.getLocalizedMessage(), e); throw new ConnectionException(e.getLocalizedMessage()); } catch (IOException e) { logger.error(e.getLocalizedMessage(), e); throw new ConnectionException(e.getLocalizedMessage()); } }
@Override public String execute(final ClusterServicePdu pdu) throws RemoteException { final HttpClient client = getHttpClient(); final PostMethod method = new PostMethod(_serviceUrl); method.addParameter("method", Integer.toString(RemoteMethodConstants.METHOD_DELIVER_PDU)); method.addParameter("sourcePeer", pdu.getSourcePeer()); method.addParameter("destPeer", pdu.getDestPeer()); method.addParameter("pduSeq", Long.toString(pdu.getSequenceId())); method.addParameter("pduAckSeq", Long.toString(pdu.getAckSequenceId())); method.addParameter("agentId", Long.toString(pdu.getAgentId())); method.addParameter("gsonPackage", pdu.getJsonPackage()); method.addParameter("stopOnError", pdu.isStopOnError() ? "1" : "0"); method.addParameter("pduType", Integer.toString(pdu.getPduType())); return executePostMethod(client, method); }
@Override public void prepareRequest() throws Exception { PostMethod method = new PostMethod(URL); method.setRequestHeader(HEADER_NAME, HEADER_VALUE); RequestEntity body = new StringRequestEntity(PAYLOAD,CONTENT_TYPE,null); method.setRequestEntity(body); request = new Http3RequestAdapter(method); } }
private void init() { initList(m_ipList, ipFilePath); initList(m_uaList, uaFilePath); initList(m_itemList, itmFilePath); initList(m_tsList,tsFilePath); initList(m_siteList,siteFilePath); initList(m_dsList,dsFilePath); initGUIDList(); String finalURL = ""; if (batchMode) { finalURL = BATCH_URL; } else { finalURL = URL; } m_payload = readFromResource(); HttpClientParams clientParams = new HttpClientParams(); clientParams.setSoTimeout(60000); m_client = new HttpClient(clientParams); m_method = new PostMethod(NODE + finalURL + EVENTTYPE); m_method.setRequestHeader("Connection", "Keep-Alive"); m_method.setRequestHeader("Accept-Charset", "UTF-8"); }