下面列出了怎么用org.apache.http.client.methods.HttpGet的API类实例代码及写法,或者点击链接到github查看源代码。
public static String convertMp3(final String mp3Path) throws URISyntaxException, IOException {
// get credentials for webservice from application config
final String apiKey = SkillConfig.getTranslatorConvertServiceUser();
final String apiPass = SkillConfig.getTranslatorConvertServicePass();
// build uri
final String bucketName = SkillConfig.getS3BucketName();
final URIBuilder uri = new URIBuilder(SkillConfig.getTranslatorConvertServiceUrl()).addParameter("bucket", bucketName).addParameter("path", mp3Path);
// set up web request
final HttpGet httpGet = new HttpGet(uri.build());
httpGet.setHeader("Content-Type", "text/plain");
// set up credentials
final CredentialsProvider provider = new BasicCredentialsProvider();
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(apiKey, apiPass);
provider.setCredentials(AuthScope.ANY, credentials);
// send request to convert webservice
final HttpResponse response =
HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build().execute(httpGet);
//Validate.inclusiveBetween(200, 399, response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
// work on response
final HttpEntity entity = response.getEntity();
return IOUtils.toString(entity.getContent(), "UTF-8");
}
@Test
public void getAuthenticatedConnection() throws IOException {
webserver.requireLogin = true;
AuthHandler handler = new AuthHandler(
new ReadOnlyStringWrapper("localhost:"+webserver.port),
new ReadOnlyBooleanWrapper(false),
new ReadOnlyStringWrapper(TEST_USER),
new ReadOnlyStringWrapper(TEST_PASSWORD)
);
CloseableHttpClient client = handler.getAuthenticatedClient();
assertNotNull(client);
HttpUriRequest request = new HttpGet("http://localhost:"+webserver.port+"/testUri");
client.execute(request);
Header authHeader = webserver.lastRequest.getFirstHeader("Authorization");
assertNotNull(authHeader);
String compareToken = "Basic "+Base64.getEncoder().encodeToString((TEST_USER + ":" + TEST_PASSWORD).getBytes());
assertEquals("Auth token should be expected format", authHeader.getValue(), compareToken);
}
@Override
protected Void doInBackground(Void... params)
{
try
{
URI url = new URI(this.url);
System.out.println("url = " + this.url);
HttpClient httpClient = new DefaultHttpClient();
HttpGet method = new HttpGet(url);
httpClient.execute(method);
}
catch (Exception e)
{
Debug.error("ERROR doInBackground()", e);
}
return null;
}
public static String GET(String url){
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
public JSONObject getCreateMetaData(String projectKey, String issueType){
try{
Map<String, String> params = new HashMap();
params.put("expand", "projects.issuetypes.fields");
params.put("projectKeys", projectKey);
params.put("issuetypeNames", issueType);
HttpGet httpGet = new HttpGet(JiraHttpHelper.metaDataUri(params));
HttpResponse httpResponse = request(httpGet);
if(httpResponse.getStatusLine().getStatusCode()!=200){
handleJiraException(httpResponse);
}
String json = EntityUtils.toString(httpResponse.getEntity());
JSONObject jsonObject = JiraConverter.objectMapper
.readValue(json,JSONObject.class);
List<Project> projects = JiraConverter.objectMapper.readValue(jsonObject.get("projects").toString(),new TypeReference<List<Project>>(){});
if(projects.isEmpty() || projects.get(0).getIssuetypes().isEmpty()) {
throw new BentenJiraException("Project '"+ projectKey + "' or issue type '" + issueType +
"' missing from create metadata. Do you have enough permissions?");
}
return projects.get(0).getIssuetypes().get(0).getFields();
}catch(Exception ex){
throw new RuntimeException(ex);
}
}
@Test @AjpIgnore
public void testLotsOfQueryParameters_Default_Ok() throws IOException {
TestHttpClient client = new TestHttpClient();
try {
StringBuilder qs = new StringBuilder();
for (int i = 0; i < DEFAULT_MAX_PARAMETERS; ++i) {
qs.append(QUERY + i);
qs.append("=");
qs.append(URLEncoder.encode(MESSAGE + i, "UTF-8"));
qs.append("&");
}
qs.deleteCharAt(qs.length()-1); // delete last useless '&'
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path?" + qs.toString());
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
for (int i = 0; i < DEFAULT_MAX_PARAMETERS; ++i) {
Header[] header = result.getHeaders(QUERY + i);
Assert.assertEquals(MESSAGE + i, header[0].getValue());
}
} finally {
client.getConnectionManager().shutdown();
}
}
@Test
public void testRangeRequest() throws IOException {
TestHttpClient client = new TestHttpClient();
try {
String fileName = "range.html";
Path f = tmpDir.resolve(fileName);
Files.write(f, "hello".getBytes());
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/range.html");
get.addHeader("range", "bytes=2-3");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.PARTIAL_CONTENT, result.getStatusLine().getStatusCode());
String response = HttpClientUtils.readResponse(result);
Assert.assertEquals("ll", response);
} finally {
client.getConnectionManager().shutdown();
}
}
private static void accessHost(String host, HttpClient client) throws IOException
{
final int maxRetry = 10;
int statusCode = -1;
for (int retry = 0; retry < maxRetry; ++retry)
{
HttpGet httpRequest = new HttpGet(String.format("https://%s:443/", host));
HttpResponse response = client.execute(httpRequest);
statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 503 && statusCode != 504)
{
break;
}
try
{
Thread.sleep(1000L);
}
catch (InterruptedException ex)
{
// nop
}
}
assertThat(String.format("response code for %s", host),
statusCode,
anyOf(equalTo(200), equalTo(403), equalTo(400)));
}
private Edm readEdm() throws EntityProviderException,
IllegalStateException, IOException {
// This is used for both setting the Edm and CSRF Token :)
if (m_edm != null) {
return m_edm;
}
String serviceUrl = new StringBuilder(getODataServiceUrl())
.append(SEPARATOR).append(METADATA).toString();
logger.info("Metadata url => " + serviceUrl);
final HttpGet get = new HttpGet(serviceUrl);
get.setHeader(AUTHORIZATION_HEADER, getAuthorizationHeader());
get.setHeader(CSRF_TOKEN_HEADER, CSRF_TOKEN_FETCH);
HttpResponse response = getHttpClient().execute(get);
m_csrfToken = response.getFirstHeader(CSRF_TOKEN_HEADER).getValue();
logger.info("CSRF token => " + m_csrfToken);
m_edm = EntityProvider.readMetadata(response.getEntity().getContent(),
false);
return m_edm;
}
/**
* Test getting a single table. GET management/tables/{tableName}
*/
@Test
public void testGetTable() throws Exception {
Map<String, Long> tableCounts = managementService.getTableCount();
String tableNameToGet = tableCounts.keySet().iterator().next();
CloseableHttpResponse response = executeRequest(new HttpGet(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TABLE, tableNameToGet)),
HttpStatus.SC_OK);
// Check table
JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
closeResponse(response);
assertThat(responseNode).isNotNull();
assertThatJson(responseNode)
.isEqualTo("{"
+ "name: '" + tableNameToGet + "',"
+ "count: " + tableCounts.get(tableNameToGet) + ","
+ "url: '" + SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_TABLE, tableNameToGet) + "'"
+ "}");
}
@Test
public void testGetUserFromAdminPageNoAccessWithFakeCookie2() throws Exception {
login(admin.email, admin.pass);
SSLContext sslcontext = TestUtil.initUnsecuredSSLContext();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new MyHostVerifier());
CloseableHttpClient httpclient2 = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
.build();
String testUser = "[email protected]";
String appName = "Blynk";
HttpGet request = new HttpGet(httpsAdminServerUrl + "/users/" + testUser + "-" + appName);
request.setHeader("Cookie", "session=123");
try (CloseableHttpResponse response = httpclient2.execute(request)) {
assertEquals(404, response.getStatusLine().getStatusCode());
}
}
@Test
@Ignore("UT3 - P3")
public void testClientCertSuccess() throws Exception {
TestHttpClient client = new TestHttpClient();
client.setSSLContext(clientSSLContext);
HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
HttpResponse result = client.execute(get);
assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Header[] values = result.getHeaders("ProcessedBy");
assertEquals("ProcessedBy Headers", 1, values.length);
assertEquals("ResponseHandler", values[0].getValue());
values = result.getHeaders("AuthenticatedUser");
assertEquals("AuthenticatedUser Headers", 1, values.length);
assertEquals("CN=Test Client,OU=OU,O=Org,L=City,ST=State,C=GB", values[0].getValue());
HttpClientUtils.readResponse(result);
assertSingleNotificationType(EventType.AUTHENTICATED);
}
public static HttpUriRequest constructRequest(String host, Endpoint endpoint, Authentication auth) {
String url = host + endpoint.getURI();
if (endpoint.getHttpMethod().equalsIgnoreCase(HttpGet.METHOD_NAME)) {
HttpGet get = new HttpGet(url);
if (auth != null)
auth.signRequest(get, null);
return get;
} else if (endpoint.getHttpMethod().equalsIgnoreCase(HttpPost.METHOD_NAME) ) {
HttpPost post = new HttpPost(url);
post.setEntity(new StringEntity(endpoint.getPostParamString(), Constants.DEFAULT_CHARSET));
post.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
if (auth != null)
auth.signRequest(post, endpoint.getPostParamString());
return post;
} else {
throw new IllegalArgumentException("Bad http method: " + endpoint.getHttpMethod());
}
}
/**
* If this returns without throwing, then you can (and must) proceed to reading the
* content using getResponseAsString() or getResponseAsStream(). If it throws, then
* you do not have to read. You must always call release().
*
* @throws HttpHandlerException
*/
public void execute() throws WAHttpException {
httpGet = new HttpGet(url.toString());
HttpHost proxy = proxySettings.getProxyForHttpClient(url.toString());
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
try {
response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK)
throw new WAHttpException(statusCode);
entity = response.getEntity();
} catch (Exception e) {
// This also releases all resources.
httpGet.abort();
if (e instanceof WAHttpException)
throw (WAHttpException) e;
else
throw new WAHttpException(e);
}
}
public static String httpGetMethodWithHeaders(String url, Map<String, Object> headers) throws Exception {
String json = null;
HttpGet get = new HttpGet(url);
CloseableHttpClient httpClient = null;
if (headers != null && !headers.isEmpty()) {
for (Map.Entry<String, Object> entry : headers.entrySet()) {
get.setHeader(entry.getKey(), entry.getValue().toString());
}
}
try {
httpClient = getHttpClient();
CloseableHttpResponse res = httpClient.execute(get);
if (res.getStatusLine().getStatusCode() == 200) {
json = EntityUtils.toString(res.getEntity());
}
} finally {
if (httpClient != null) {
httpClient.close();
}
}
return json;
}
@Test
public void jsp() throws Exception {
try (CloseableHttpClient hc = HttpClients.createMinimal()) {
try (CloseableHttpResponse res = hc.execute(new HttpGet(server().httpUri() + "/jsp/index.jsp"))) {
assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK");
assertThat(res.getFirstHeader(HttpHeaderNames.CONTENT_TYPE.toString()).getValue())
.startsWith("text/html");
final String actualContent = CR_OR_LF.matcher(EntityUtils.toString(res.getEntity()))
.replaceAll("");
assertThat(actualContent).isEqualTo(
"<html><body>" +
"<p>Hello, Armerian World!</p>" +
"<p>Have you heard about the class 'org.slf4j.Logger'?</p>" +
"<p>Context path: </p>" + // ROOT context path
"<p>Request URI: /index.jsp</p>" +
"<p>Scheme: http</p>" +
"</body></html>");
}
}
}
@Test
@Ignore("UT3 - P3")
public void testLargeURL() throws IOException {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < COUNT; ++i) {
sb.append(MESSAGE);
}
String message = sb.toString();
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/" + message);
HttpResponse result = client.execute(get);
Assert.assertEquals("/" + message, HttpClientUtils.readResponse(result));
} finally {
client.getConnectionManager().shutdown();
}
}
@Override
public Iterator<Container> iterator() {
final URIBuilder uriBuilder = new UncheckedUriBuilder(
super.baseUri().toString().concat("/json")
);
if (this.withSize) {
uriBuilder.addParameter("size", "true");
}
final FilteredUriBuilder uri = new FilteredUriBuilder(
uriBuilder,
this.filters);
return new ResourcesIterator<>(
super.client(),
new HttpGet(uri.build()),
json -> new RtContainer(
json,
super.client(),
URI.create(
super.baseUri().toString() + "/" + json.getString("Id")
),
super.docker()
)
);
}
public static void customers() throws Exception {
String baseUrl = keycloak.getDeployment().getAuthServerBaseUrl();
baseUrl = baseUrl.substring(0, baseUrl.indexOf('/', 8));
String customersUrl = baseUrl + "/database/customers";
HttpGet get = new HttpGet(customersUrl);
get.setHeader("Accept", "application/json");
get.setHeader("Authorization", "Bearer " + keycloak.getTokenString(10, TimeUnit.SECONDS));
HttpResponse response = keycloak.getDeployment().getClient().execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
print(response.getEntity().getContent());
} else {
System.out.println(response.getStatusLine().toString());
}
}
@Test
public void testTrustAll() throws Exception {
try (TestServer testServer = new TestServer("sslConfigurator/jks/truststore.jks",
"sslConfigurator/jks/node1-keystore.jks", "secret", false)) {
Path rootCaJksPath = FileHelper.getAbsoluteFilePathFromClassPath("sslConfigurator/jks/other-root-ca.jks");
Settings settings = Settings.builder().put("prefix.enable_ssl", "true").put("prefix.trust_all", "true")
.put("path.home", rootCaJksPath.getParent().toString()).build();
Path configPath = rootCaJksPath.getParent();
SettingsBasedSSLConfigurator sbsc = new SettingsBasedSSLConfigurator(settings, configPath, "prefix");
SSLConfig sslConfig = sbsc.buildSSLConfig();
try (CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(sslConfig.toSSLConnectionSocketFactory()).build()) {
try (CloseableHttpResponse response = httpClient.execute(new HttpGet(testServer.getUri()))) {
// Success
}
}
}
}
/**
* 获取二进制文件流
* @param url
* @return
* @throws Exception
*/
public static ByteArrayOutputStream doFileGetHttps(String url) throws Exception {
initSSLContext();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
int responseCode = response.getStatusLine().getStatusCode();
Logger.info("httpClient get 响应状态responseCode="+responseCode+", 请求 URL="+url);
if(responseCode == RSP_200 || responseCode == RSP_400){
InputStream inputStream = response.getEntity().getContent();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte buff[] = new byte[4096];
int counts = 0;
while ((counts = inputStream.read(buff)) != -1) {
outputStream.write(buff, 0, counts);
}
outputStream.flush();
outputStream.close();
httpGet.releaseConnection();
return outputStream;
}else{
httpGet.releaseConnection();
throw new Exception("接口请求异常:接口响应状态="+responseCode);
}
}
private String getConfiguration(String url, boolean ifAuth) throws IOException {
HttpGet request = ifAuth ? newGet(url) : new HttpGet(url);
HttpResponse response = null;
try {
response = client.execute(request);
String msg = EntityUtils.toString(response.getEntity());
if (response.getStatusLine().getStatusCode() != 200)
throw new IOException(INVALID_RESPONSE + response.getStatusLine().getStatusCode()
+ " with cache wipe url " + url + "\n" + msg);
Map<String, String> map = JsonUtil.readValueAsMap(msg);
msg = map.get("config");
return msg;
} finally {
cleanup(request, response);
}
}
private void getAndCompare(String address,
String acceptType,
int expectedStatus,
long expectedId) throws Exception {
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpGet get = new HttpGet(address);
get.setHeader("Accept", acceptType);
try {
CloseableHttpResponse response = client.execute(get);
assertEquals(expectedStatus, response.getStatusLine().getStatusCode());
Book book = readBook(response.getEntity().getContent());
assertEquals(expectedId, book.getId());
assertEquals("CXF in Action", book.getName());
} finally {
get.releaseConnection();
}
}
public boolean isWebSiteHosted(String url) throws Exception {
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("content-type", "text/html");
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
if (httpClient != null) {
HttpResponse httpResponse;
try {
httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() >= 400) {
return false;
}
} catch (Exception e) {
logger.error("Exception getting from url :[{}],[{}] ", url, e.getMessage());
throw e;
}
}
return true;
}
@Test
public void givenJUnitManagedServer_whenMatchingURL_thenCorrect() throws IOException {
stubFor(get(urlPathMatching("/baeldung/.*"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", APPLICATION_JSON)
.withBody("\"testing-library\": \"WireMock\"")));
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet(String.format("http://localhost:%s/baeldung/wiremock", port));
HttpResponse httpResponse = httpClient.execute(request);
String stringResponse = convertHttpResponseToString(httpResponse);
verify(getRequestedFor(urlEqualTo(BAELDUNG_WIREMOCK_PATH)));
assertEquals(200, httpResponse.getStatusLine().getStatusCode());
assertEquals(APPLICATION_JSON, httpResponse.getFirstHeader("Content-Type").getValue());
assertEquals("\"testing-library\": \"WireMock\"", stringResponse);
}
@Test
public void shouldMakeRemoteGetCalls() {
Robolectric.getBackgroundScheduler().pause();
TestGetRequest apiRequest = new TestGetRequest();
apiGateway.makeRequest(apiRequest, responseCallbacks);
Robolectric.addPendingHttpResponse(200, GENERIC_XML);
Robolectric.getBackgroundScheduler().runOneTask();
HttpRequestInfo sentHttpRequestData = Robolectric.getSentHttpRequestInfo(0);
HttpRequest sentHttpRequest = sentHttpRequestData.getHttpRequest();
assertThat(sentHttpRequest.getRequestLine().getUri(), equalTo("www.example.com"));
assertThat(sentHttpRequest.getRequestLine().getMethod(), equalTo(HttpGet.METHOD_NAME));
assertThat(sentHttpRequest.getHeaders("foo")[0].getValue(), equalTo("bar"));
CredentialsProvider credentialsProvider =
(CredentialsProvider) sentHttpRequestData.getHttpContext().getAttribute(ClientContext.CREDS_PROVIDER);
assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getUserPrincipal().getName(), CoreMatchers.equalTo("spongebob"));
assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getPassword(), CoreMatchers.equalTo("squarepants"));
}
@Test
public void uploadFileToServer() throws Exception {
String pathToImage = upload("static/ota/test.bin");
HttpGet index = new HttpGet("http://localhost:" + properties.getHttpPort() + pathToImage);
try (CloseableHttpResponse response = httpclient.execute(index)) {
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals("application/octet-stream", response.getHeaders("Content-Type")[0].getValue());
}
}
@Override
public FetchedResult get(String url, Payload payload) throws BaseFetchException {
try {
URL realUrl = new URL(url);
String protocol = realUrl.getProtocol();
if (!protocol.equals("http") && !protocol.equals("https")) {
throw new BadProtocolFetchException(url);
}
} catch (MalformedURLException e) {
throw new UrlFetchException(url, e.getMessage());
}
return request(new HttpGet(), url, payload);
}
@Test
public void checkNewRequestHeader() throws ClientProtocolException, IOException, ODataException {
final HttpGet get = new HttpGet(URI.create(getEndpoint().toString() + "/$metadata"));
get.setHeader("ConTenT-laNguaGe", "de, en");
getHttpClient().execute(get);
final ODataContext ctx = getService().getProcessor().getContext();
assertNotNull(ctx);
assertEquals("de, en", ctx.getRequestHeader(HttpHeaders.CONTENT_LANGUAGE));
assertNull(ctx.getRequestHeader("nonsens"));
}
public String getHttpContent (String url) throws URISyntaxException, UnknownHostException, IOException, IllegalCharsetNameException {
if (StringUtils.isEmpty(url)){
return "";
}
String encodedUrl = getEncodedUrl(url);
CloseableHttpClient httpClient = getHttpClient(encodedUrl);
HttpGet get = new HttpGet(encodedUrl);
try {
LOGGER.debug("executing request to retrieve content on " + get.getURI());
HttpResponse response = httpClient.execute(get);
LOGGER.debug("received " + response.getStatusLine().getStatusCode() + " from get request");
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
LOGGER.debug("status == HttpStatus.SC_OK " );
return EntityUtils.toString(response.getEntity(), Charset.defaultCharset());
} else {
LOGGER.debug("status != HttpStatus.SC_OK " );
return "";
}
} catch (NullPointerException ioe) {
LOGGER.debug("NullPointerException");
return "";
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
get.releaseConnection();
LOGGER.debug("finally");
httpClient.close();
}
}