本文整理匯總了Java中org.apache.commons.httpclient.methods.GetMethod類的典型用法代碼示例。如果您正苦於以下問題:Java GetMethod類的具體用法?Java GetMethod怎麽用?Java GetMethod使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
GetMethod類屬於org.apache.commons.httpclient.methods包,在下文中一共展示了GetMethod類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: get
import org.apache.commons.httpclient.methods.GetMethod; //導入依賴的package包/類
@Override
public HttpResponse get(URL urlObj, String userName, String password, int timeout) {
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(urlObj.toString());
client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
client.getParams().setSoTimeout(1000 * timeout);
client.getParams().setConnectionManagerTimeout(1000 * timeout);
if (userName != null && password != null) {
setBasicAuthorization(method, userName, password);
}
try {
int response = client.executeMethod(method);
return new HttpResponse(response, method.getResponseBody());
} catch (IOException e) {
throw new RuntimeException("Failed to get " + urlObj.toString(), e);
} finally {
method.releaseConnection();
}
}
示例2: getUrlContent
import org.apache.commons.httpclient.methods.GetMethod; //導入依賴的package包/類
/**
* Retrieves the content under the given URL with username and passwort
* authentication.
*
* @param url
* the URL to read
* @param username
* @param password
* @return the read content.
* @throws IOException
* if an I/O exception occurs.
*/
private static byte[] getUrlContent(URL url, String username,
String password) throws IOException {
final HttpClient client = new HttpClient();
// Set credentials:
client.getParams().setAuthenticationPreemptive(true);
final Credentials credentials = new UsernamePasswordCredentials(
username, password);
client.getState()
.setCredentials(
new AuthScope(url.getHost(), url.getPort(),
AuthScope.ANY_REALM), credentials);
// Retrieve content:
final GetMethod method = new GetMethod(url.toString());
final int status = client.executeMethod(method);
if (status != HttpStatus.SC_OK) {
throw new IOException("Error " + status + " while retrieving "
+ url);
}
return method.getResponseBody();
}
示例3: testCreateNodeMultipart
import org.apache.commons.httpclient.methods.GetMethod; //導入依賴的package包/類
public void testCreateNodeMultipart() throws IOException {
final String url = HTTP_BASE_URL + "/CreateNodeTest_2_" + System.currentTimeMillis();
// add some properties to the node
final Map<String,String> props = new HashMap<String,String>();
props.put("name1","value1B");
props.put("name2","value2B");
// POST and get URL of created node
String urlOfNewNode = null;
try {
urlOfNewNode = testClient.createNode(url, props, null, true);
} catch(IOException ioe) {
fail("createNode failed: " + ioe);
}
// check node contents (not all renderings - those are tested above)
final GetMethod get = new GetMethod(urlOfNewNode + DEFAULT_EXT);
final int status = httpClient.executeMethod(get);
assertEquals(urlOfNewNode + " must be accessible after createNode",200,status);
final String responseBodyStr = get.getResponseBodyAsString();
assertTrue(responseBodyStr.contains("value1B"));
assertTrue(responseBodyStr.contains("value2B"));
}
開發者ID:apache,項目名稱:sling-org-apache-sling-launchpad-integration-tests,代碼行數:25,代碼來源:CreateNodeTest.java
示例4: testPreemptiveAuthProxy
import org.apache.commons.httpclient.methods.GetMethod; //導入依賴的package包/類
public void testPreemptiveAuthProxy() throws Exception {
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("testuser", "testpass");
this.client.getState().setProxyCredentials(AuthScope.ANY, creds);
this.client.getParams().setAuthenticationPreemptive(true);
this.server.setHttpService(new FeedbackService());
this.proxy.requireAuthentication(creds, "test", true);
GetMethod get = new GetMethod("/");
try {
this.client.executeMethod(get);
assertEquals(HttpStatus.SC_OK, get.getStatusCode());
if (isUseSSL()) {
assertNull(get.getRequestHeader("Proxy-Authorization"));
} else {
assertNotNull(get.getRequestHeader("Proxy-Authorization"));
}
} finally {
get.releaseConnection();
}
}
示例5: testGetProxyAuthHostInvalidAuth
import org.apache.commons.httpclient.methods.GetMethod; //導入依賴的package包/類
/**
* Tests GET via authenticating proxy + invalid host auth
*/
public void testGetProxyAuthHostInvalidAuth() throws Exception {
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("testuser", "testpass");
HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
handlerchain.appendHandler(new AuthRequestHandler(creds));
handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
this.client.getState().setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("testuser", "wrongstuff"));
this.client.getState().setProxyCredentials(AuthScope.ANY, creds);
this.server.setRequestHandler(handlerchain);
this.proxy.requireAuthentication(creds, "test", true);
GetMethod get = new GetMethod("/");
try {
this.client.executeMethod(get);
assertEquals(HttpStatus.SC_UNAUTHORIZED, get.getStatusCode());
} finally {
get.releaseConnection();
}
}
示例6: testRelativeURLHitWithDefaultHost
import org.apache.commons.httpclient.methods.GetMethod; //導入依賴的package包/類
public void testRelativeURLHitWithDefaultHost() throws IOException {
this.server.setHttpService(new EchoService());
// Set default host
this.client.getHostConfiguration().setHost(
this.server.getLocalAddress(),
this.server.getLocalPort(),
Protocol.getProtocol("http"));
GetMethod httpget = new GetMethod("/test/");
try {
this.client.executeMethod(httpget);
assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
} finally {
httpget.releaseConnection();
}
}
示例7: tryAuthentication
import org.apache.commons.httpclient.methods.GetMethod; //導入依賴的package包/類
private static void tryAuthentication(RemoteFileReference wsReference) throws Exception {
URL urlToConnect = wsReference.getUrl();
String wsdlUrl = wsReference.getUrlpath();
String username = wsReference.getAuthUser();
String password = wsReference.getAuthPassword();
HttpClient client = new HttpClient();
client.getState().setCredentials(
new AuthScope(urlToConnect.getHost(), urlToConnect.getPort()),
new UsernamePasswordCredentials(username, password)
);
GetMethod get = new GetMethod(wsdlUrl);
get.setDoAuthentication( true );
int statuscode = client.executeMethod(get);
if (statuscode == HttpStatus.SC_UNAUTHORIZED) {
throw new Exception(HttpStatus.SC_UNAUTHORIZED + " - Unauthorized connection!");
}
}
示例8: getResource
import org.apache.commons.httpclient.methods.GetMethod; //導入依賴的package包/類
/**
* Gets remote resource.
*
* @return the remove resource
*
* @throws ResourceException thrown if the resource could not be fetched
*/
protected GetMethod getResource() throws ResourceException {
GetMethod getMethod = new GetMethod(resourceUrl);
getMethod.addRequestHeader("Connection", "close");
try {
httpClient.executeMethod(getMethod);
if (getMethod.getStatusCode() != HttpStatus.SC_OK) {
throw new ResourceException("Unable to retrieve resource URL " + resourceUrl
+ ", received HTTP status code " + getMethod.getStatusCode());
}
return getMethod;
} catch (IOException e) {
throw new ResourceException("Unable to contact resource URL: " + resourceUrl, e);
}
}
示例9: buildGetMethod
import org.apache.commons.httpclient.methods.GetMethod; //導入依賴的package包/類
/**
* Builds the HTTP GET method used to fetch the metadata. The returned method advertises support for GZIP and
* deflate compression, enables conditional GETs if the cached metadata came with either an ETag or Last-Modified
* information, and sets up basic authentication if such is configured.
*
* @return the constructed GET method
*/
protected GetMethod buildGetMethod() {
GetMethod getMethod = new GetMethod(getMetadataURI());
getMethod.addRequestHeader("Connection", "close");
getMethod.setRequestHeader("Accept-Encoding", "gzip,deflate");
if (cachedMetadataETag != null) {
getMethod.setRequestHeader("If-None-Match", cachedMetadataETag);
}
if (cachedMetadataLastModified != null) {
getMethod.setRequestHeader("If-Modified-Since", cachedMetadataLastModified);
}
if (httpClient.getState().getCredentials(authScope) != null) {
log.debug("Using BASIC authentication when retrieving metadata from '{}", metadataURI);
getMethod.setDoAuthentication(true);
}
return getMethod;
}
示例10: testBasicRedirect301
import org.apache.commons.httpclient.methods.GetMethod; //導入依賴的package包/類
public void testBasicRedirect301() throws IOException {
String host = this.server.getLocalAddress();
int port = this.server.getLocalPort();
this.server.setHttpService(
new BasicRedirectService(host, port, HttpStatus.SC_MOVED_PERMANENTLY));
GetMethod httpget = new GetMethod("/oldlocation/");
httpget.setFollowRedirects(true);
try {
this.client.executeMethod(httpget);
assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
assertEquals("/newlocation/", httpget.getPath());
assertEquals(host, httpget.getURI().getHost());
assertEquals(port, httpget.getURI().getPort());
assertEquals(new URI("http://" + host + ":" + port + "/newlocation/", false), httpget.getURI());
} finally {
httpget.releaseConnection();
}
}
示例11: testPreemptiveAuthProxyWithCrossSiteRedirect
import org.apache.commons.httpclient.methods.GetMethod; //導入依賴的package包/類
public void testPreemptiveAuthProxyWithCrossSiteRedirect() throws Exception {
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("testuser", "testpass");
this.client.getState().setProxyCredentials(AuthScope.ANY, creds);
this.client.getParams().setAuthenticationPreemptive(true);
this.server.setHttpService(new BasicRedirectService(
"http://127.0.0.1:" + this.server.getLocalPort()));
this.proxy.requireAuthentication(creds, "test", true);
GetMethod get = new GetMethod("/redirect/");
try {
this.client.executeMethod(get);
assertEquals(HttpStatus.SC_OK, get.getStatusCode());
} finally {
get.releaseConnection();
}
}
示例12: testHttpMethodBaseTEandCL
import org.apache.commons.httpclient.methods.GetMethod; //導入依賴的package包/類
/**
* Tests response with a Trasfer-Encoding and Content-Length
*/
public void testHttpMethodBaseTEandCL() throws Exception {
this.server.setHttpService(new SimpleChunkedService());
GetMethod httpget = new GetMethod("/test/");
try {
this.client.executeMethod(httpget);
assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
assertEquals("1234567890123", httpget.getResponseBodyAsString());
assertTrue(this.client.getHttpConnectionManager() instanceof SimpleHttpConnectionManager);
HttpConnection conn = this.client.getHttpConnectionManager().
getConnection(this.client.getHostConfiguration());
assertNotNull(conn);
conn.assertNotOpen();
} finally {
httpget.releaseConnection();
}
}
示例13: testDefaults
import org.apache.commons.httpclient.methods.GetMethod; //導入依賴的package包/類
public void testDefaults() throws IOException {
this.server.setHttpService(new SimpleService());
this.client.getParams().setParameter(HttpMethodParams.USER_AGENT, "test");
HostConfiguration hostconfig = new HostConfiguration();
hostconfig.setHost(
this.server.getLocalAddress(),
this.server.getLocalPort(),
Protocol.getProtocol("http"));
GetMethod httpget = new GetMethod("/miss/");
try {
this.client.executeMethod(hostconfig, httpget);
} finally {
httpget.releaseConnection();
}
assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
assertEquals("test", httpget.getRequestHeader("User-Agent").getValue());
assertEquals("test", httpget.getParams().
getParameter(HttpMethodParams.USER_AGENT));
assertEquals("test", hostconfig.getParams().
getParameter(HttpMethodParams.USER_AGENT));
assertEquals("test", client.getParams().
getParameter(HttpMethodParams.USER_AGENT));
}
示例14: testEmptyBodyAsString
import org.apache.commons.httpclient.methods.GetMethod; //導入依賴的package包/類
/**
* Tests empty body response
*/
public void testEmptyBodyAsString() throws Exception {
this.server.setHttpService(new EmptyResponseService());
GetMethod httpget = new GetMethod("/test/");
try {
this.client.executeMethod(httpget);
assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
String response = httpget.getResponseBodyAsString();
assertNull(response);
this.client.executeMethod(httpget);
assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
response = httpget.getResponseBodyAsString(1);
assertNull(response);
} finally {
httpget.releaseConnection();
}
}
示例15: testRedirectToResourceAfterLogout
import org.apache.commons.httpclient.methods.GetMethod; //導入依賴的package包/類
/**
* Test SLING-1847
* @throws Exception
*/
@Test
public void testRedirectToResourceAfterLogout() throws Exception {
//login
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new NameValuePair("j_username", "admin"));
params.add(new NameValuePair("j_password", "admin"));
H.assertPostStatus(HttpTest.HTTP_BASE_URL + "/j_security_check", HttpServletResponse.SC_MOVED_TEMPORARILY, params, null);
//...and then...logout with a resource redirect
String locationAfterLogout = HttpTest.SERVLET_CONTEXT + "/system/sling/info.sessionInfo.json";
final GetMethod get = new GetMethod(HttpTest.HTTP_BASE_URL + "/system/sling/logout");
NameValuePair [] logoutParams = new NameValuePair[1];
logoutParams[0] = new NameValuePair("resource", locationAfterLogout);
get.setQueryString(logoutParams);
get.setFollowRedirects(false);
final int status = H.getHttpClient().executeMethod(get);
assertEquals("Expected redirect", HttpServletResponse.SC_MOVED_TEMPORARILY, status);
Header location = get.getResponseHeader("Location");
assertEquals(HttpTest.HTTP_BASE_URL + locationAfterLogout, location.getValue());
}
開發者ID:apache,項目名稱:sling-org-apache-sling-launchpad-integration-tests,代碼行數:26,代碼來源:RedirectOnLogoutTest.java