public List<Cookie> parse(final Header header, final CookieOrigin origin) throws MalformedCookieException { if (header == null) { throw new IllegalArgumentException("Header may not be null"); } if (origin == null) { throw new IllegalArgumentException("Cookie origin may not be null"); } HeaderElement[] elems = header.getElements(); return parse(elems, origin); }
protected boolean hasEncoding( final HttpResponse response, final String encoding ) { HttpEntity entity = response.getEntity(); if ( entity == null ) return false; Header ceHeader = entity.getContentEncoding(); if ( ceHeader == null ) return false; HeaderElement[] codecs = ceHeader.getElements(); for ( int i = 0; i < codecs.length; i++ ) if ( encoding.equalsIgnoreCase( codecs[i].getName() ) ) return true; return false; } }
public static String getContentCharSet(final HttpEntity entity) throws ParseException { if (entity == null) { throw new IllegalArgumentException("HTTP entity may not be null"); } String charset = null; if (entity.getContentType() != null) { HeaderElement values[] = entity.getContentType().getElements(); if (values.length > 0) { NameValuePair param = values[0].getParameterByName("charset"); if (param != null) { charset = param.getValue(); } } } return charset; }
public static HttpResult httpPostLarge(String url, Map<String, String> headers, String content) { try { HttpClientBuilder builder = HttpClients.custom(); builder.setUserAgent(UtilsAndCommons.SERVER_VERSION); builder.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS); CloseableHttpClient httpClient = builder.build(); HttpPost httpost = new HttpPost(url); for (Map.Entry<String, String> entry : headers.entrySet()) { httpost.setHeader(entry.getKey(), entry.getValue()); } httpost.setEntity(new StringEntity(content, ContentType.create("application/json", "UTF-8"))); HttpResponse response = httpClient.execute(httpost); HttpEntity entity = response.getEntity(); HeaderElement[] headerElements = entity.getContentType().getElements(); String charset = headerElements[0].getParameterByName("charset").getValue(); return new HttpResult(response.getStatusLine().getStatusCode(), IOUtils.toString(entity.getContent(), charset), Collections.<String, String>emptyMap()); } catch (Exception e) { return new HttpResult(500, e.toString(), Collections.<String, String>emptyMap()); } }
public static HttpResult httpPost(String url, List<String> headers, Map<String, String> paramValues, String encoding) { try { HttpPost httpost = new HttpPost(url); RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000).setConnectTimeout(5000).setSocketTimeout(5000).setRedirectsEnabled(true).setMaxRedirects(5).build(); httpost.setConfig(requestConfig); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> entry : paramValues.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } httpost.setEntity(new UrlEncodedFormEntity(nvps, encoding)); HttpResponse response = postClient.execute(httpost); HttpEntity entity = response.getEntity(); String charset = encoding; if (entity.getContentType() != null) { HeaderElement[] headerElements = entity.getContentType().getElements(); if (headerElements != null && headerElements.length > 0 && headerElements[0] != null && headerElements[0].getParameterByName("charset") != null) { charset = headerElements[0].getParameterByName("charset").getValue(); } } return new HttpResult(response.getStatusLine().getStatusCode(), IOUtils.toString(entity.getContent(), charset), Collections.<String, String>emptyMap()); } catch (Throwable e) { return new HttpResult(500, e.toString(), Collections.<String, String>emptyMap()); } }
private String getCookieHeader(CloseableHttpResponse resp) { Header lastHeader = resp.getLastHeader("Set-Cookie"); if (lastHeader == null) { return null; } return lastHeader.getElements()[0].getValue(); }
public Set<String> getAllowedMethods(final HttpResponse response) { if (response == null) { throw new IllegalArgumentException("HTTP response may not be null"); } HeaderIterator it = response.headerIterator("Allow"); Set<String> methods = new HashSet<String>(); while (it.hasNext()) { Header header = it.nextHeader(); HeaderElement[] elements = header.getElements(); for (HeaderElement element : elements) { methods.add(element.getName()); } } return methods; }
@Override public void process(final HttpResponse response, final HttpContext context) { // Inflate any responses compressed with gzip final HttpEntity entity = response.getEntity(); if (entity != null) { final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(IgnitedHttp.ENCODING_GZIP)) { response.setEntity(new GzipInflatingEntity(response.getEntity())); break; } } } } }
HeaderElement[] elems = header.getElements();
elems = header.getElements();
unsatisfiableCharset = response.getFirstHeader("content-type").getElements()[0].getParameterByName("charset").getValue(); } catch (Exception f) { unsatisfiableCharset = "<failed-to-parse>";
HeaderElement[] encodings = null; try { encodings = transferEncodingHeader.getElements(); } catch (ParseException px) { throw new ProtocolException
@Test public void testHandle() throws IOException { final HttpResponse response = mock(HttpResponse.class); final StatusLine statusLine = mock(StatusLine.class); final HttpEntity entity = mock(HttpEntity.class); final Logger log = mock(Logger.class); final Header header = mock(Header.class); expect(response.getStatusLine()).andReturn(statusLine).once(); expect(statusLine.getStatusCode()).andReturn(HttpStatus.SC_OK).once(); expect(response.getEntity()).andReturn(entity).times(2); final ByteArrayInputStream bais = new ByteArrayInputStream("yolo".getBytes(StandardCharsets.UTF_8)); expect(entity.getContent()).andReturn(bais).times(2); expect(entity.getContentType()).andReturn(header).times(1); expect(header.getElements()).andReturn(new HeaderElement[]{}); expect(entity.getContentLength()).andReturn(4L).times(2); log.warn("yolo"); expectLastCall().once(); replay(response, statusLine, entity, header, log); final KsqlVersionCheckerResponseHandler kvcr = new KsqlVersionCheckerResponseHandler(log); kvcr.handle(response); verify(response, statusLine, entity, header, log); } }
throw new IllegalArgumentException("Cookie origin may not be null"); HeaderElement[] helems = header.getElements(); boolean versioned = false; boolean netscape = false;
protected boolean hasEncoding( final HttpResponse response, final String encoding ) { HttpEntity entity = response.getEntity(); if ( entity == null ) return false; Header ceHeader = entity.getContentEncoding(); if ( ceHeader == null ) return false; HeaderElement[] codecs = ceHeader.getElements(); for ( int i = 0; i < codecs.length; i++ ) if ( encoding.equalsIgnoreCase( codecs[i].getName() ) ) return true; return false; } }
@Override public Reader createReader() throws IOException { HttpEntity entity = myResponse.getEntity(); if (entity == null) { return new StringReader(""); } Charset charset = null; if (entity.getContentType() != null && entity.getContentType().getElements() != null && entity.getContentType().getElements().length > 0) { ContentType ct = ContentType.get(entity); charset = ct.getCharset(); } if (charset == null) { if (Constants.STATUS_HTTP_204_NO_CONTENT != myResponse.getStatusLine().getStatusCode()) { ourLog.debug("Response did not specify a charset, defaulting to utf-8"); } charset = Charset.forName("UTF-8"); } return new InputStreamReader(readEntity(), charset); }
httpclient.addResponseInterceptor(new HttpResponseInterceptor() { public void process( final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (int i = 0; i < codecs.length; i++) { if (codecs[i].getName().equalsIgnoreCase("gzip")) { response.setEntity( new GzipDecompressingEntity(response.getEntity())); return; } } } } });
mHttpClient.addResponseInterceptor(new HttpResponseInterceptor() { public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); Header encheader = entity.getContentEncoding(); if (encheader != null) { HeaderElement[] codecs = encheader.getElements(); for (int i = 0; i < codecs.length; i++) { if (codecs[i].getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity( entity)); return; } } } } });
private void downloadVideo(DownloadMaterialResponse response){ String url = response.getDownUrl(); LOG.debug("Download url: " + url); RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(NetWorkCenter.CONNECT_TIMEOUT).setConnectTimeout(NetWorkCenter.CONNECT_TIMEOUT).setSocketTimeout(NetWorkCenter.CONNECT_TIMEOUT).build(); CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build(); HttpGet get = new HttpGet(url); try { CloseableHttpResponse r = client.execute(get); if (HttpStatus.SC_OK == r.getStatusLine().getStatusCode()) { InputStream inputStream = r.getEntity().getContent(); Header[] headers = r.getHeaders("Content-disposition"); Header length = r.getHeaders("Content-Length")[0]; response.setContent(inputStream, Integer.valueOf(length.getValue())); response.setFileName(headers[0].getElements()[0].getParameterByName("filename").getValue()); } } catch (IOException e){ LOG.error("IO异常处理", e); } catch (Exception e) { LOG.error("其他异常", e); } }
public static String getSessionIdFromResponse( final HttpResponse response ) { final Header cookie = response.getFirstHeader( "Set-Cookie" ); if ( cookie != null ) { for ( final HeaderElement header : cookie.getElements() ) { if ( "JSESSIONID".equals( header.getName() ) ) { return header.getValue(); } } } return null; }