下面列出了org.apache.http.client.methods.HttpGet#setHeaders() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Preform a get request
*
* @param baseUrl (nexusUrl in most tests)
* @param path to the resource
* @param headers {@link Header}s
* @param useDefaultCredentials use {@link NexusITSupport#clientBuilder(URL, boolean)} for using credentials
* @return the response object
*/
protected Response get(final URL baseUrl,
final String path,
final Header[] headers,
final boolean useDefaultCredentials) throws Exception
{
HttpGet request = new HttpGet();
request.setURI(UriBuilder.fromUri(baseUrl.toURI()).path(path).build());
request.setHeaders(headers);
try (CloseableHttpClient client = clientBuilder(nexusUrl, useDefaultCredentials).build()) {
try (CloseableHttpResponse response = client.execute(request)) {
ResponseBuilder responseBuilder = Response.status(response.getStatusLine().getStatusCode());
Arrays.stream(response.getAllHeaders()).forEach(h -> responseBuilder.header(h.getName(), h.getValue()));
HttpEntity entity = response.getEntity();
if (entity != null) {
responseBuilder.entity(new ByteArrayInputStream(IOUtils.toByteArray(entity.getContent())));
}
return responseBuilder.build();
}
}
}
/**
* Preform a get request
*
* @param baseUrl (nexusUrl in most tests)
* @param path to the resource
* @param headers {@link Header}s
* @param useDefaultCredentials use {@link NexusITSupport#clientBuilder(URL, boolean)} for using credentials
* @return the response object
*/
protected Response get(final URL baseUrl,
final String path,
final Header[] headers,
final boolean useDefaultCredentials) throws Exception
{
HttpGet request = new HttpGet();
request.setURI(UriBuilder.fromUri(baseUrl.toURI()).path(path).build());
request.setHeaders(headers);
try (CloseableHttpClient client = clientBuilder(nexusUrl, useDefaultCredentials).build()) {
try (CloseableHttpResponse response = client.execute(request)) {
ResponseBuilder responseBuilder = Response.status(response.getStatusLine().getStatusCode());
Arrays.stream(response.getAllHeaders()).forEach(h -> responseBuilder.header(h.getName(), h.getValue()));
HttpEntity entity = response.getEntity();
if (entity != null) {
responseBuilder.entity(new ByteArrayInputStream(IOUtils.toByteArray(entity.getContent())));
}
return responseBuilder.build();
}
}
}
List<URI> getAllRedirectLocations(String uri, Header[] headers) throws IOException {
CloseableHttpResponse response = null;
try {
HttpClientContext context = HttpClientContext.create();
HttpGet httpGet = new HttpGet(String.format(Locale.ENGLISH, "http://%s:%s/%s", address.getHostName(), address.getPort(), uri));
if (headers != null) {
httpGet.setHeaders(headers);
}
response = httpClient.execute(httpGet, context);
// get all redirection locations
return context.getRedirectLocations();
} finally {
if(response != null) {
response.close();
}
}
}
/**
* http get
*
* @param url
* @return response
* @throws ClientProtocolException
* @throws IOException
*/
public static CloseableHttpResponse get(String url, Header[] header) throws IOException {
HttpGet httpget = new HttpGet(url);
if (header != null && header.length > 0) {
httpget.setHeaders(header);
}
CloseableHttpResponse response = httpClient.execute(httpget, context);
return response;
}
public static String doHttpGet(String url, Header[] headers) {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeaders(headers);
HttpResponse resp = httpClient.execute(httpGet);
return StreamUtil.readText(resp.getEntity().getContent(), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public RequestHandle get(Context context, String s, Header aheader[], RequestParams requestparams, ResponseHandlerInterface responsehandlerinterface)
{
HttpGet httpget = new HttpGet(getUrlWithQueryString(h, s, requestparams));
if (aheader != null)
{
httpget.setHeaders(aheader);
}
return sendRequest(c, d, httpget, null, responsehandlerinterface, context);
}
CloseableHttpResponse get(String uri, Header[] headers) throws IOException {
HttpGet httpGet = new HttpGet(String.format(Locale.ENGLISH, "http://%s:%s/%s", address.getHostName(), address.getPort(), uri));
if (headers != null) {
httpGet.setHeaders(headers);
}
return executeAndDefaultAssertions(httpGet);
}
protected CloseableHttpResponse get(String uri, Header[] headers) throws IOException {
HttpGet httpGet = new HttpGet(String.format(Locale.ENGLISH, "http://%s:%s/_blobs/%s", dataNode1.getHostName(), dataNode1.getPort(), uri));
if (headers != null) {
httpGet.setHeaders(headers);
}
return executeAndDefaultAssertions(httpGet);
}
/**
* Given access token, query IDP URL snowflake app to get SAML response
* We also need to perform important client side validation:
* validate the post back url come back with the SAML response
* contains the same prefix as the Snowflake's server url, which is the
* intended destination url to Snowflake.
* Explanation:
* This emulates the behavior of IDP initiated login flow in the user
* browser where the IDP instructs the browser to POST the SAML
* assertion to the specific SP endpoint. This is critical in
* preventing a SAML assertion issued to one SP from being sent to
* another SP.
*
* @param loginInput Login Info for the request
* @param ssoUrl URL to use for SSO
* @param oneTimeToken The token used for SSO
* @return The response in HTML form
* @throws SnowflakeSQLException Will be thrown if the destination URL in
* the SAML assertion does not match
*/
private static String federatedFlowStep4(
SFLoginInput loginInput,
String ssoUrl,
String oneTimeToken) throws SnowflakeSQLException
{
String responseHtml = "";
try
{
final URL url = new URL(ssoUrl);
URI oktaGetUri = new URIBuilder()
.setScheme(url.getProtocol())
.setHost(url.getHost())
.setPath(url.getPath())
.setParameter("RelayState", "%2Fsome%2Fdeep%2Flink")
.setParameter("onetimetoken", oneTimeToken).build();
HttpGet httpGet = new HttpGet(oktaGetUri);
HeaderGroup headers = new HeaderGroup();
headers.addHeader(new BasicHeader(HttpHeaders.ACCEPT, "*/*"));
httpGet.setHeaders(headers.getAllHeaders());
responseHtml = HttpUtil.executeGeneralRequest(
httpGet,
loginInput.getLoginTimeout(),
loginInput.getOCSPMode());
// step 5
String postBackUrl = getPostBackUrlFromHTML(responseHtml);
if (!isPrefixEqual(postBackUrl, loginInput.getServerUrl()))
{
logger.debug("The specified authenticator {} and the destination URL " +
"in the SAML assertion {} do not match.",
loginInput.getAuthenticator(), postBackUrl);
throw new SnowflakeSQLException(
SqlState.SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION,
ErrorCode.IDP_INCORRECT_DESTINATION.getMessageCode());
}
}
catch (IOException | URISyntaxException ex)
{
handleFederatedFlowError(loginInput, ex);
}
return responseHtml;
}
private String doGet(URI uri) throws DigitalOceanException, RequestUnsuccessfulException {
HttpGet get = new HttpGet(uri);
get.setHeaders(requestHeaders);
return executeHttpRequest(get);
}