我正在开发一个简单的 Jersey 客户端应用程序,它执行 GET 请求、存储 cookie,然后在 WebView (JavaFX) 中打开 URL。当我没有设置我的连接器 ApacheConnectorProvider() 时,我有这个工作。当我注释掉一行时,CookieManager 会存储我的 cookie,但是当我取消注释该行时,我的 CookieManager 中没有任何 cookie。有人知道为什么吗?
我的代码如下>
import java.io.IOException;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.ProtocolException;
import java.net.URI;
import java.net.URL;
import java.util.List;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Response;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import com.ftl.destroyer.ClientHelperJersey;
public class TestCookiesJersey extends Application {
private URL url = null;
private ClientHelperJersey clientHelper;
final URI proxySettings = null;
public TestCookiesJersey() {
this.clientHelper = new ClientHelperJersey(proxySettings, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36");
}
public void startProcess(String query) throws IOException {
url = new URL("http://www.pacsun.com/");
// GET request to server. goto first page.
jerseyGETRequest(url);
// display cookies.
for (HttpCookie cookie : this.clientHelper.getCm().getCookieStore().getCookies()) {
System.out.println(cookie.getName() + " " + cookie.getValue());
}
//
CookieStore cookieJar = this.clientHelper.getCm().getCookieStore();
List<HttpCookie> cookies = cookieJar.getCookies();
for (HttpCookie cookie : cookies) {
System.out.println("CookieHandler retrieved cookie: " + cookie);
}
launch();
}
public void jerseyGETRequest(URL url) throws IOException, ProtocolException {
// build the GET request invocation.
final Invocation invocation = this.clientHelper.addHeaders(this.clientHelper.getClient().target(url.toString()).request(), false).header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8").header("Accept-Language", "en-US,en;q=0.8,da;q=0.6").header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8").property("jersey.config.client.readTimeout", 15000).buildGet();
final Response response = invocation.invoke();
response.getCookies();
}
public void start(Stage stage) {
stage.setTitle("TestCookiesJersey");
BorderPane borderPane = new BorderPane();
WebView webviewBrowser = new WebView();
borderPane.setCenter(webviewBrowser);
final WebEngine engine = webviewBrowser.getEngine();
engine.load("http://www.pacsun.com/");
engine.setJavaScriptEnabled(true);
stage.setScene(new Scene(borderPane, 850, 600));
stage.show();
}
public static void main(String[] args) {
try {
TestCookiesJersey destroyer = new TestCookiesJersey();
destroyer.startProcess("");
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
这是主类,然后我使用一个帮助类来设置 Jersey 客户端并设置 ApacheConnector >
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpCookie;
import java.net.URI;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Configuration;
import org.glassfish.jersey.apache.connector.ApacheClientProperties;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.RequestEntityProcessing;
import org.glassfish.jersey.filter.LoggingFilter;
public class ClientHelperJersey {
public CookieManager cm = new CookieManager();
private final String userAgent;
private final Client client;
public ClientHelperJersey(URI proxySettings, String userAgent) {
CookieHandler.setDefault(cm);
this.cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
// below works, I can see the 1 cookie being set if I uncomment.
//HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
//cookie.setDomain("foo.com");
//this.cm.getCookieStore().add(null, cookie);
this.userAgent = userAgent;
ClientConfig config = new ClientConfig();
config.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED);
config.property(ApacheClientProperties.DISABLE_COOKIES, false);
config.connectorProvider(new ApacheConnectorProvider());
config.register(new LoggingFilter());
config.register(new CookieFilter(this.cm));
ClientBuilder clientBuilder = ClientBuilder.newBuilder().withConfig((Configuration) config);
this.client = clientBuilder.build();
}
public Client getClient() {
return this.client;
}
public CookieManager getCm() {
return this.cm;
}
public Invocation.Builder addHeaders(Invocation.Builder requestBuilder, boolean ajaxRequest) {
requestBuilder.header("User-Agent", this.userAgent);
return requestBuilder;
}
}
如果我在 ClientHelperJersey 中注释掉这一行,cookie 将由 CookieManager >
config.connectorProvider(new ApacheConnectorProvider());
有谁知道如何使用 ApacheConnectorProvider() 以便我可以保存我的 cookie 并在包含 cookie 的 WebView 中加载一个 URL?