/** * Sets the content-type header. */ default THttpRequest setContentType(MimeType contentType) { return setHeader(Headers.CONTENT_TYPE, contentType.toString()); }
@Override public String getContentType(Request request) throws Throwable { return response.getContentType().toString(); }
@Override public THttpMultipart addFile(String name, String text, String filename, String contentType) { Args.notEmpty(filename, "filename"); if(null == contentType) { contentType = MimeTypes.getMimeType(filename); if(null == contentType) { contentType = "text/plain"; } } MediaType mediaType = MediaType.parse(MimeTypes.parse(contentType).withCharset(charset.name()).toString()); byte[] data = null == text ? new byte[]{} : text.getBytes(charset); mp.addFormDataPart(name, filename, RequestBody.create(mediaType, data)); empty = false; return this; } }
@Override public THttpResponse send(){ String url = buildRequestUrl(); try { newRequest(url); log.debug("Sending '{}' request to '{}'...", method, url); ApacheTHttpResponse response = new ApacheTHttpResponse(this, request, httpClient.execute(request) ); if(log.isDebugEnabled()) { log.debug("Response result : [status={}, content-type='{}', content-length={}]", response.getStatus(), response.getContentType(), response.getContentLength()); MimeType contentType = response.getContentType(); if(null != contentType && ContentTypes.isText(contentType.toString())) { log.debug("Content -> \n{}", Strings.abbreviate(response.getContent(), 200)); } } return response; } catch (Exception e) { throw new HttpException("Error send http request : " + e.getMessage(),e); }finally{ if(null != request) { request.releaseConnection(); } } }