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

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

问题描述

在以下code,我已验证连接到URL从applet $ P $内pserves浏览器会话,如果使用JDK的URLConnection类。然而,这不是这种情况,如果使用Apache的HttpClient库。有谁知道为什么吗?另外,有我设置连接实例由HttpClient的实例中使用的方法是什么?

 进口java.applet.Applet中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.net.HttpURLConnection中;
进口java.net.URISyntaxException;
进口的java.net.URL;进口javax.net.ssl​​.SSLException;进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.BasicResponseHandler;
进口org.apache.http.impl.client.DefaultHttpClient;公共类HttpClientTesterApplet扩展的Applet {
    私有静态最后的serialVersionUID长= -1599714556710568947L;    公共无效testHttpClient()抛出ClientProtocolException,IOException异常,
    {的URISyntaxException
    网址URL =新的URL(的String.format(%S://本地主机:%s /%S / testHttpClient
    的getParameter(协议)的getParameter(口),
    的getParameter(上下文)));    HttpClient的客户端=新DefaultHttpClient();    HttpPost后=新HttpPost(url.toURI());    的System.out.println(执行要求+ post.getURI());    尝试{
    System.out的
    .println(client.execute(邮局,新BasicResponseHandler()));
    }赶上(异常SSLException E){
    的System.out.println(e.getMessage());
    }    的System.out.println(执行的请求,+ post.getURI());    的System.out.println(打开连接+网址);    HttpURLConnection类的URLConnection =(HttpURLConnection类)网址
    .openConnection();    的System.out.println(打开的连接+网址);    urlConnection.setRequestMethod(POST);    的System.out.println(连接);    urlConnection.connect();    的System.out.println(已连接);    为InputStream的InputStream = urlConnection.getInputStream();    尝试{
    而(inputStream.read()!= - 1){
    的System.out.println(读);
    }
    } {最后
    inputStream.close();
    }
    }
}


解决方案

这是与图书馆实现通过插座自己的URL连接的通病。显然,JRE实现URLConnection类的可以到浏览器的信息直接。我们不得不使用的技术,因为通过oscargm上面提到的,即在应用程序服务器写入请求中的Cookie是参数的小程序并使用JavaScript让浏览器的文件饼干(这是SSO的情况下,一套在那里饼干可能不会因为中间剂的相同 - 代理服务器)。请注意,如果饼干的HttpOnly - 的JavaScript code将无法

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();
    	}
    }
}

解决方案

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.

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

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