本文整理汇总了Java中org.apache.commons.httpclient.cookie.CookiePolicy类的典型用法代码示例。如果您正苦于以下问题:Java CookiePolicy类的具体用法?Java CookiePolicy怎么用?Java CookiePolicy使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CookiePolicy类属于org.apache.commons.httpclient.cookie包,在下文中一共展示了CookiePolicy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCookies
import org.apache.commons.httpclient.cookie.CookiePolicy; //导入依赖的package包/类
/**
* Returns an array of {@link Cookie cookies} in this HTTP
* state that match the given request parameters.
*
* @param domain the request domain
* @param port the request port
* @param path the request path
* @param secure <code>true</code> when using HTTPS
*
* @return an array of {@link Cookie cookies}.
*
* @see #getCookies()
*
* @deprecated use CookieSpec#match(String, int, String, boolean, Cookie)
*/
public synchronized Cookie[] getCookies(
String domain,
int port,
String path,
boolean secure
) {
LOG.trace("enter HttpState.getCookies(String, int, String, boolean)");
CookieSpec matcher = CookiePolicy.getDefaultSpec();
ArrayList list = new ArrayList(cookies.size());
for (int i = 0, m = cookies.size(); i < m; i++) {
Cookie cookie = (Cookie) (cookies.get(i));
if (matcher.match(domain, port, path, secure, cookie)) {
list.add(cookie);
}
}
return (Cookie[]) (list.toArray(new Cookie[list.size()]));
}
示例2: getClient
import org.apache.commons.httpclient.cookie.CookiePolicy; //导入依赖的package包/类
private void getClient(){
CONTACT_URL = OscarProperties.getInstance().getProperty("TELEPLAN_URL",CONTACT_URL);
HttpState initialState = new HttpState();
// Initial set of cookies can be retrieved from persistent storage and
// re-created, using a persistence mechanism of choice,
Cookie mycookie = new Cookie("moh.hnet.bc.ca","mycookie", "stuff", "/", null, false); // and then added to your HTTP state instance
initialState.addCookie(mycookie);
// Get HTTP client instance
//HttpClientParams hcParams = new HttpClientParams();
//hcParams.setParameter("User-Agent","TeleplanPerl 1.0");
httpclient = new HttpClient(); //hcParams);
httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
httpclient.setState(initialState);
httpclient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
httpclient.getParams().setParameter("User-Agent","TeleplanPerl 1.0");
}
示例3: applyTo
import org.apache.commons.httpclient.cookie.CookiePolicy; //导入依赖的package包/类
@Override
public void applyTo(OwnCloudClient client) {
client.getParams().setAuthenticationPreemptive(false);
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.setFollowRedirects(false);
Uri serverUri = client.getBaseUri();
String[] cookies = mSessionCookie.split(";");
if (cookies.length > 0) {
Cookie cookie = null;
for (int i=0; i<cookies.length; i++) {
int equalPos = cookies[i].indexOf('=');
if (equalPos >= 0) {
cookie = new Cookie();
cookie.setName(cookies[i].substring(0, equalPos));
cookie.setValue(cookies[i].substring(equalPos + 1));
cookie.setDomain(serverUri.getHost()); // VERY IMPORTANT
cookie.setPath(serverUri.getPath()); // VERY IMPORTANT
client.getState().addCookie(cookie);
}
}
}
}
示例4: HttpClient
import org.apache.commons.httpclient.cookie.CookiePolicy; //导入依赖的package包/类
public HttpClient(int maxConPerHost, int conTimeOutMs, int soTimeOutMs, int maxSize) {
connectionManager = new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams params = connectionManager.getParams();
params.setDefaultMaxConnectionsPerHost(maxConPerHost);
params.setConnectionTimeout(conTimeOutMs);
params.setSoTimeout(soTimeOutMs);
HttpClientParams clientParams = new HttpClientParams();
// 忽略cookie 避免 Cookie rejected 警告
clientParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
client = new org.apache.commons.httpclient.HttpClient(clientParams, connectionManager);
Protocol myhttps = new Protocol("https", new MySSLSocketFactory(), 443);
Protocol.registerProtocol("https", myhttps);
this.maxSize = maxSize;
// 支持proxy
if (proxyHost != null && !proxyHost.equals("")) {
client.getHostConfiguration().setProxy(proxyHost, proxyPort);
client.getParams().setAuthenticationPreemptive(true);
if (proxyAuthUser != null && !proxyAuthUser.equals("")) {
client.getState().setProxyCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(proxyAuthUser, proxyAuthPassword));
log("Proxy AuthUser: " + proxyAuthUser);
log("Proxy AuthPassword: " + proxyAuthPassword);
}
}
}
示例5: configureHttpClient
import org.apache.commons.httpclient.cookie.CookiePolicy; //导入依赖的package包/类
protected void configureHttpClient() throws IOException, GeneralSecurityException {
httpClient.getParams().setAuthenticationPreemptive(isAuthenticationPreemptive());
initCredentials();
initSocketFactory();
initProtocolIfNeeded();
if (httpConnectionManager != null) {
httpClient.setHttpConnectionManager(httpConnectionManager);
}
List<Header> headers = getDefaultHeaders();
httpClient.getHostConfiguration().getParams().setParameter(HostParams.DEFAULT_HEADERS, headers);
httpClient.getParams().setParameter(HttpClientParams.USER_AGENT,
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Ubuntu/11.04 Chromium/18.0.1025.151 Chrome/18.0.1025.151 Safari/535.19");
httpClient.getParams().setParameter(HttpClientParams.HTTP_CONTENT_CHARSET, "UTF-8");
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
httpClient.getParams().setConnectionManagerTimeout(connectionManagerTimeout);
httpClient.getParams().setSoTimeout(soTimeout);
if (connectionTimeout >= 0) {
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
}
}
示例6: HttpClient
import org.apache.commons.httpclient.cookie.CookiePolicy; //导入依赖的package包/类
public HttpClient(int maxConPerHost, int conTimeOutMs, int soTimeOutMs,
int maxSize) {
// MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
SimpleHttpConnectionManager connectionManager = new SimpleHttpConnectionManager(true);
HttpConnectionManagerParams params = connectionManager.getParams();
params.setDefaultMaxConnectionsPerHost(maxConPerHost);
params.setConnectionTimeout(conTimeOutMs);
params.setSoTimeout(soTimeOutMs);
HttpClientParams clientParams = new HttpClientParams();
clientParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
client = new org.apache.commons.httpclient.HttpClient(clientParams,
connectionManager);
Protocol myhttps = new Protocol("https", new MySSLSocketFactory(), 443);
Protocol.registerProtocol("https", myhttps);
}
示例7: getHttpClient
import org.apache.commons.httpclient.cookie.CookiePolicy; //导入依赖的package包/类
private static HttpClient getHttpClient() {
HttpClient httpClient = new HttpClient();
// 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
httpClient.getParams().setCookiePolicy(
CookiePolicy.BROWSER_COMPATIBILITY);
// 设置 默认的超时重试处理策略
httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
// 设置 连接超时时间
httpClient.getHttpConnectionManager().getParams()
.setConnectionTimeout(TIMEOUT_CONNECTION);
// 设置 读数据超时时间
httpClient.getHttpConnectionManager().getParams()
.setSoTimeout(TIMEOUT_SOCKET);
// 设置 字符集
httpClient.getParams().setContentCharset(UTF_8);
return httpClient;
}
示例8: XmlRequest
import org.apache.commons.httpclient.cookie.CookiePolicy; //导入依赖的package包/类
public XmlRequest(String host, int port, String protocol) {
this.host = host;
this.port = port;
this.protocol = protocol;
setMethod(Method.GET);
state.addCookie(cookie);
client.setState(state);
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.setHostConfiguration(config);
List<String> authPrefs = new ArrayList<String>(2);
authPrefs.add(AuthPolicy.DIGEST);
authPrefs.add(AuthPolicy.BASIC);
// This will exclude the NTLM authentication scheme
client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY,
authPrefs);
}
示例9: getHttpClient
import org.apache.commons.httpclient.cookie.CookiePolicy; //导入依赖的package包/类
/**
* 获取HttpClient对象
*
* @return
*/
private static HttpClient getHttpClient() {
HttpClient httpClient = new HttpClient();
// 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
// 设置 默认的超时重试处理策略
httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
// 设置 连接超时时间
httpClient.getHttpConnectionManager().getParams()
.setConnectionTimeout(TIMEOUT_CONNECTION);
// 设置 读数据超时时间
httpClient.getHttpConnectionManager().getParams().setSoTimeout(TIMEOUT_SOCKET);
// 设置 字符集
httpClient.getParams().setContentCharset(UTF_8);
return httpClient;
}
示例10: configureDefaultHttpClientParams
import org.apache.commons.httpclient.cookie.CookiePolicy; //导入依赖的package包/类
protected void configureDefaultHttpClientParams(HttpParams params) {
params.setParameter(HttpClientParams.CONNECTION_MANAGER_CLASS, MultiThreadedHttpConnectionManager.class);
params.setParameter(HttpMethodParams.COOKIE_POLICY, CookiePolicy.RFC_2109);
params.setLongParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, 10000);
Map<HostConfiguration, Integer> maxHostConnectionsMap = new HashMap<HostConfiguration, Integer>();
maxHostConnectionsMap.put(HostConfiguration.ANY_HOST_CONFIGURATION, new Integer(20));
params.setParameter(HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, maxHostConnectionsMap);
params.setIntParameter(HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, 20);
params.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 10000);
params.setIntParameter(HttpConnectionParams.SO_TIMEOUT, 2*60*1000);
boolean retrySocketException = new Boolean(ConfigContext.getCurrentContextConfig().getProperty(RETRY_SOCKET_EXCEPTION_PROPERTY));
if (retrySocketException) {
LOG.info("Installing custom HTTP retry handler to retry requests in face of SocketExceptions");
params.setParameter(HttpMethodParams.RETRY_HANDLER, new CustomHttpMethodRetryHandler());
}
}
示例11: testCookiePolicy2109
import org.apache.commons.httpclient.cookie.CookiePolicy; //导入依赖的package包/类
public void testCookiePolicy2109() throws Exception {
man.setCookiePolicy(CookiePolicy.RFC_2109);
man.testStarted(); // ensure policy is picked up
URL url = new URL("http://order.now/sub1/moo.html");
man.addCookieFromHeader("test1=moo1;", url);
man.addCookieFromHeader("test2=moo2;path=/sub1", url);
man.addCookieFromHeader("test2=moo3;path=/", url);
assertEquals(3,man.getCookieCount());
//assertEquals("/",man.get(0).getPath());
assertEquals("/sub1",man.get(1).getPath());
assertEquals("/",man.get(2).getPath());
String s = man.getCookieHeaderForURL(url);
assertNotNull(s);
HC3CookieHandler hc3CookieHandler = (HC3CookieHandler) man.getCookieHandler();
org.apache.commons.httpclient.Cookie[] c =
hc3CookieHandler.getCookiesForUrl(man.getCookies(), url,
CookieManager.ALLOW_VARIABLE_COOKIES);
assertEquals("/sub1",c[0].getPath());
assertFalse(c[0].isPathAttributeSpecified());
assertEquals("/sub1",c[1].getPath());
assertTrue(c[1].isPathAttributeSpecified());
assertEquals("/",c[2].getPath());
assertTrue(c[2].isPathAttributeSpecified());
assertEquals("$Version=0; test1=moo1; test2=moo2; $Path=/sub1; test2=moo3; $Path=/", s);
}
示例12: testCookiePolicyNetscape
import org.apache.commons.httpclient.cookie.CookiePolicy; //导入依赖的package包/类
public void testCookiePolicyNetscape() throws Exception {
man.setCookiePolicy(CookiePolicy.NETSCAPE);
man.testStarted(); // ensure policy is picked up
URL url = new URL("http://www.order.now/sub1/moo.html");
man.addCookieFromHeader("test1=moo1;", url);
man.addCookieFromHeader("test2=moo2;path=/sub1", url);
man.addCookieFromHeader("test2=moo3;path=/", url);
assertEquals(3,man.getCookieCount());
assertEquals("/sub1",man.get(0).getPath());
assertEquals("/sub1",man.get(1).getPath());
assertEquals("/",man.get(2).getPath());
String s = man.getCookieHeaderForURL(url);
assertNotNull(s);
HC3CookieHandler hc3CookieHandler = (HC3CookieHandler) man.getCookieHandler();
org.apache.commons.httpclient.Cookie[] c =
hc3CookieHandler.getCookiesForUrl(man.getCookies(), url,
CookieManager.ALLOW_VARIABLE_COOKIES);
assertEquals("/sub1",c[0].getPath());
assertFalse(c[0].isPathAttributeSpecified());
assertEquals("/sub1",c[1].getPath());
assertTrue(c[1].isPathAttributeSpecified());
assertEquals("/",c[2].getPath());
assertTrue(c[2].isPathAttributeSpecified());
assertEquals("test1=moo1; test2=moo2; test2=moo3", s);
}
示例13: downloadUrl
import org.apache.commons.httpclient.cookie.CookiePolicy; //导入依赖的package包/类
public static void downloadUrl(String url, File destFile) throws IOException, HTTPException {
File tmpFile = new File(destFile.getAbsoluteFile() + ".tmp");
Logger.getLogger(IOUtils.class).debug(String.format("Downloading URL %s to %s", url, tmpFile));
tmpFile.getParentFile().mkdirs();
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(url);
get.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
int code = client.executeMethod(get);
if (code >= 200 && code < 300) {
copyToFileAndCloseStreams(get.getResponseBodyAsStream(), tmpFile);
tmpFile.renameTo(destFile);
}
else
Logger.getLogger(IOUtils.class).fatal("Got HTTP response code " + code + " when trying to download " + url);
}
示例14: init
import org.apache.commons.httpclient.cookie.CookiePolicy; //导入依赖的package包/类
/**
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*
* Called upon initialization, Will create the ConfigParser and get the
* RuleChain back. Will also configure the httpclient.
*/
public void init(FilterConfig filterConfig) throws ServletException {
log = LogFactory.getLog(ProxyFilter.class);
AllowedMethodHandler.setAllowedMethods("OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE");
httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
httpClient.getParams().setBooleanParameter(HttpClientParams.USE_EXPECT_CONTINUE, false);
httpClient.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
String data = filterConfig.getInitParameter("dataUrl");
if (data == null) {
serverChain = null;
} else {
try {
File dataFile = new File(filterConfig.getServletContext().getRealPath(data));
ConfigParser parser = new ConfigParser(dataFile);
serverChain = parser.getServerChain();
} catch (Exception e) {
throw new ServletException(e);
}
}
}
示例15: login
import org.apache.commons.httpclient.cookie.CookiePolicy; //导入依赖的package包/类
public String login(String id) throws HttpException, IOException{
hc.getHostConfiguration().setProxy("127.0.0.1", 8087);
hc.getParams().setParameter(HttpMethodParams.USER_AGENT,"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36");//������Ϣ
PostMethod p = new PostMethod("http://shoufei.hebust.edu.cn/kd/login.jsp");
p.addParameter("EdtStuID", id+"';--");
p.addParameter("mm", "haha");
hc.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
hc.executeMethod(p);
// ��õ�½��� Cookie
Cookie[] cookies = hc.getState().getCookies();
String tmpcookies = "";
for (Cookie c : cookies) {
tmpcookies += c.toString() + ";";
}
// ���е�½��IJ���
PostMethod postMethod = new PostMethod(
"http://shoufei.hebust.edu.cn/kd/qrypayment.jsp");
// ÿ�η�������Ȩ����ַʱ�����ǰ��� cookie ��Ϊͨ��֤
postMethod.setRequestHeader("cookie", tmpcookies);
hc.executeMethod(postMethod);
String s = postMethod.getResponseBodyAsString();
return s;
}