本文整理汇总了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