@Override public void sendRequest() throws IOException { request.setEntity(new ByteArrayEntity(output.toByteArray())); this.response = httpClient.execute(request); }
@Override public void sendRequest() throws IOException { request.setEntity(new ByteArrayEntity(output.toByteArray())); this.response = httpClient.execute(request); }
public HttpEntity getEntity() { return new ByteArrayEntity(body); } }
int TIMEOUT_MILLISEC = 10000; // = 10 seconds String postMessage="{}"; //HERE_YOUR_POST_STRING. HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC); HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); HttpClient client = new DefaultHttpClient(httpParams); HttpPost request = new HttpPost(serverUrl); request.setEntity(new ByteArrayEntity( postMessage.toString().getBytes("UTF8"))); HttpResponse response = client.execute(request);
public void post() throws Exception{ HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://www.baidu.com"); String xml = "<xml>xxxx</xml>"; HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8")); post.setEntity(entity); HttpResponse response = client.execute(post); String result = EntityUtils.toString(response.getEntity()); }
protected HttpEntity createEntity(String ct, byte[] byteArray) { final ByteArrayEntity byteArrayEntity = new ByteArrayEntity(byteArray); byteArrayEntity.setContentType(ct); return byteArrayEntity; }
/** * Set the given serialized remote invocation as request body. * <p>The default implementation simply sets the serialized invocation as the * HttpPost's request body. This can be overridden, for example, to write a * specific encoding and to potentially set appropriate HTTP request headers. * @param config the HTTP invoker configuration that specifies the target service * @param httpPost the HttpPost to set the request body on * @param baos the ByteArrayOutputStream that contains the serialized * RemoteInvocation object * @throws java.io.IOException if thrown by I/O methods */ protected void setRequestBody( HttpInvokerClientConfiguration config, HttpPost httpPost, ByteArrayOutputStream baos) throws IOException { ByteArrayEntity entity = new ByteArrayEntity(baos.toByteArray()); entity.setContentType(getContentType()); httpPost.setEntity(entity); }
private static void setEntityIfNonEmptyBody(HttpEntityEnclosingRequestBase httpRequest, Request<?> request) throws AuthFailureError { byte[] body = request.getBody(); if (body != null) { HttpEntity entity = new ByteArrayEntity(body); httpRequest.setEntity(entity); } }
private static void setEntityIfNonEmptyBody(HttpEntityEnclosingRequestBase httpRequest, Request<?> request) throws AuthFailureError { byte[] body = request.getBody(); if (body != null) { HttpEntity entity = new ByteArrayEntity(body); httpRequest.setEntity(entity); } }
@Override protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException { addHeaders(this.httpRequest, headers); if (this.httpRequest instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest; HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput); entityEnclosingRequest.setEntity(requestEntity); } HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext); return new HttpComponentsClientHttpResponse(httpResponse); }
/** * 如果request的请求体不为空,进行设置请求体信息 * @param httpRequest * @param request * @throws AuthFailureError */ private static void setEntityIfNonEmptyBody(HttpEntityEnclosingRequestBase httpRequest, Request<?> request) throws AuthFailureError { byte[] body = request.getBody(); if (body != null) { HttpEntity entity = new ByteArrayEntity(body); httpRequest.setEntity(entity); } }
/** * Set the given serialized remote invocation as request body. * <p>The default implementation simply sets the serialized invocation as the * HttpPost's request body. This can be overridden, for example, to write a * specific encoding and to potentially set appropriate HTTP request headers. * @param config the HTTP invoker configuration that specifies the target service * @param httpPost the HttpPost to set the request body on * @param baos the ByteArrayOutputStream that contains the serialized * RemoteInvocation object * @throws java.io.IOException if thrown by I/O methods */ protected void setRequestBody( HttpInvokerClientConfiguration config, HttpPost httpPost, ByteArrayOutputStream baos) throws IOException { ByteArrayEntity entity = new ByteArrayEntity(baos.toByteArray()); entity.setContentType(getContentType()); httpPost.setEntity(entity); }
@Override protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException { addHeaders(this.httpRequest, headers); if (this.httpRequest instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest; HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput); entityEnclosingRequest.setEntity(requestEntity); } HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext); return new HttpComponentsClientHttpResponse(httpResponse); }
HttpPost buildSendXMLMethod( byte[] content, String service ) throws Exception { // Prepare HTTP put // String urlString = constructUrl( service ); if ( log.isDebug() ) { log.logDebug( BaseMessages.getString( PKG, "SlaveServer.DEBUG_ConnectingTo", urlString ) ); } HttpPost postMethod = new HttpPost( urlString ); // Request content will be retrieved directly from the input stream // HttpEntity entity = new ByteArrayEntity( content ); postMethod.setEntity( entity ); postMethod.addHeader( new BasicHeader( "Content-Type", "text/xml;charset=" + Const.XML_ENCODING ) ); return postMethod; }
static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) { try { BytesRef source = XContentHelper.toXContent(toXContent, xContentType, false).toBytesRef(); return new ByteArrayEntity(source.bytes, source.offset, source.length, createContentType(xContentType)); } catch (IOException e) { throw new RuntimeException(e); } }
private HttpEntity asEntity(final ContentResource resource, final Request request) { return new ByteArrayEntity(resource.readFor(of(request)).getContent(), getContentType((HttpRequest) request)); }
@Override protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception { HttpPost postMethod = new HttpPost(config.getServiceUrl()); ByteArrayEntity entity = new ByteArrayEntity(baos.toByteArray()); entity.setContentType(getContentType()); postMethod.setEntity(entity); BasicHttpContext context = null; if (environment.useSslContext()) { context = new BasicHttpContext(); context.setAttribute(HttpClientContext.USER_TOKEN, goAgentServerHttpClient.principal()); } try (CloseableHttpResponse response = goAgentServerHttpClient.execute(postMethod, context)) { validateResponse(response); InputStream responseBody = getResponseBody(response); return readRemoteInvocationResult(responseBody, config.getCodebaseUrl()); } }
private RequestBuilder addFormParams(RequestBuilder requestBuilder, Request request) { if (request.getRequestBody() != null) { ByteArrayEntity entity = new ByteArrayEntity(request.getRequestBody().getBody()); entity.setContentType(request.getRequestBody().getContentType()); requestBuilder.setEntity(entity); } return requestBuilder; }
public static Request multiSearch(MultiSearchRequest multiSearchRequest) throws IOException { Request request = new Request(HttpMethod.POST.name(), "/_msearch"); Params params = new Params(request); params.putParam("typed_keys", "true"); if (multiSearchRequest.maxConcurrentSearchRequests() != MultiSearchRequest.MAX_CONCURRENT_SEARCH_REQUESTS_DEFAULT) { params.putParam("max_concurrent_searches", Integer.toString(multiSearchRequest.maxConcurrentSearchRequests())); } XContent xContent = REQUEST_BODY_CONTENT_TYPE.xContent(); byte[] source = MultiSearchRequest.writeMultiLineFormat(multiSearchRequest, xContent); request.setEntity(new ByteArrayEntity(source, createContentType(xContent.type()))); return request; }
protected void handleException(final HttpException ex, final HttpResponse response) { if (ex instanceof MethodNotSupportedException) { response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED); } else if (ex instanceof UnsupportedHttpVersionException) { response.setStatusCode(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED); } else if (ex instanceof ProtocolException) { response.setStatusCode(HttpStatus.SC_BAD_REQUEST); } else { response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR); } byte[] msg = EncodingUtils.getAsciiBytes(ex.getMessage()); ByteArrayEntity entity = new ByteArrayEntity(msg); entity.setContentType("text/plain; charset=US-ASCII"); response.setEntity(entity); }