使用 Apache 的 HttpClient 与使用 JDK 的 URLConnection 从小程序中连接到 URL [英] Connection to a URL from within an applet using Apache's HttpClient vs using the JDK's URLConnection

查看:19
本文介绍了使用 Apache 的 HttpClient 与使用 JDK 的 URLConnection 从小程序中连接到 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我已经验证,如果使用 JDK 的 URLConnection 类,从小程序中连接到 URL 会保留浏览器的会话.但是,如果使用 Apache 的 HttpClient 库,则情况并非如此.有谁知道为什么?或者,有没有办法让我设置 HttpClient 实例要使用的连接实例?

In the following code, I have verified that connecting to a URL from within an applet preserves the browser's session if JDK's URLConnection class is used. However, this is not the case if Apache's HttpClient library is used. Does anyone know why? Alternatively, is there a way for me to set the connection instance to be used by an HttpClient instance?

import java.applet.Applet;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;

import javax.net.ssl.SSLException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpClientTesterApplet extends Applet {
    private static final long serialVersionUID = -1599714556710568947L;

    public void testHttpClient() throws ClientProtocolException, IOException,
            URISyntaxException {
        URL url = new URL(String.format("%s://localhost:%s/%s/testHttpClient",
                getParameter("protocol"), getParameter("port"),
                getParameter("context")));

        HttpClient client = new DefaultHttpClient();

        HttpPost post = new HttpPost(url.toURI());

        System.out.println("Executing request " + post.getURI());

        try {
            System.out
                    .println(client.execute(post, new BasicResponseHandler()));
        } catch (SSLException e) {
            System.out.println(e.getMessage());
        }

        System.out.println("Executed request " + post.getURI());

        System.out.println("Opening connection " + url);

        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();

        System.out.println("Opened connection " + url);

        urlConnection.setRequestMethod("POST");

        System.out.println("Connecting");

        urlConnection.connect();

        System.out.println("Connected");

        InputStream inputStream = urlConnection.getInputStream();

        try {
            while (inputStream.read() != -1) {
                System.out.println("Reading");
            }
        } finally {
            inputStream.close();
        }
    }
}

推荐答案

这是库通过 Socket 实现自己的 URL 连接的一个常见问题.显然,URLConnection 类的JRE 实现可以直接获取浏览器信息.我们必须采用上面 oscargm 提到的技术,即在应用服务器上将请求 cookie 写入小程序的参数并使用 JavaScript 获取浏览器的文档 cookie(这是针对 SSO 的情况,其中 cookie 集可能不一样,因为中间代理——代理服务器).请注意,如果 cookie 是 HttpOnly - javascript 代码将失败.

This is a common problem with libraries implementing their own URL connection via Socket. Apparently, the JRE implementation of the URLConnection class can get to the browser information directly. We had to employ the technique as mentioned by oscargm above, i.e. on the appserver writing the request cookies to be the parameters to the applet AND getting to the browser's document cookies using JavaScript (this is for the case of SSO where the set of cookies may not be the same because of the intermediate agent -- proxy servers). Note that if the cookies are HttpOnly -- the javascript code will fail.

这篇关于使用 Apache 的 HttpClient 与使用 JDK 的 URLConnection 从小程序中连接到 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆