java getBaseCode_Java HttpRequestBase.getURI方法代码示例

该代码示例展示了在Java中如何使用HttpRequestBase类的getURI方法来处理HTTP请求。当不遵循重定向时,会禁用HttpClient的重定向功能。此外,代码还涉及了代理配置、Cookie管理和异常处理。
摘要由CSDN通过智能技术生成

import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类

private ResponseImpl getWebContentPrivate(Request request, @Nullable ProxyDetails proxy, boolean followRedirects)

{

final String url = request.getUrl();

try

{

@SuppressWarnings("unused")

URL u = new URL(url); // NOSONAR

}

catch( MalformedURLException ex )

{

return new ResponseImpl(HttpServletResponse.SC_BAD_REQUEST, "Invalid URL: " + url);

}

HttpRequestBase httpMethod = null;

try

{

httpMethod = getHttpMethod(request);

if( httpMethod == null )

{

return new ResponseImpl(HttpServletResponse.SC_METHOD_NOT_ALLOWED,

"Only GET, POST, HEAD, PUT, DELETE and OPTIONS methods are supported");

}

if( !followRedirects )

{

HttpClientParams.setRedirecting(httpMethod.getParams(), false);

}

final DefaultHttpClient client = createClient(httpMethod.getURI().getScheme().equals("https"));

if( proxy != null && proxy.isConfigured() )

{

final URI uri = httpMethod.getURI();

final String host = uri.getHost();

if( !proxy.isHostExcepted(host) )

{

final HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort());

client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHost);

if( !Check.isEmpty(proxy.getUsername()) )

{

client.getCredentialsProvider().setCredentials(

new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),

new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()));

}

}

}

// TODO: see fixme about cookie cache

final String cacheKey = /* req.getSession().getId() */"FIXME" + ':' + url;

Cookies cookies = COOKIE_CACHE.getIfPresent(cacheKey);

if( cookies == null )

{

cookies = new Cookies();

COOKIE_CACHE.put(cacheKey, cookies);

}

final HttpResponse response = exec(client, httpMethod, cookies);

return new ResponseImpl(response, httpMethod);

}

catch( Exception e )

{

throw new RuntimeException(e);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值