2006-05-24

建立ssl连接并通过BASIC认证

贴段代码了,没啥好说的哦

public void testHttpsConnection() throws Exception
{
URL url = new URL("https://www.blogger.com/atom");
X509TrustManager xtm = new X509TrustManager()
{
public void checkClientTrusted(X509Certificate chain[], String authType)throws CertificateException{}
public void checkServerTrusted(X509Certificate chain[], String authType)throws CertificateException{}
public X509Certificate[] getAcceptedIssuers()
{
X509Certificate[] cert = {};
return cert;
}
};
TrustManager mytm[] = {xtm};
SSLContext ctx = SSLContext.getInstance("SSL");
ctx.init(null,mytm, null );
SSLSocketFactory sf = ctx.getSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(sf);
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
String usernamePassword = "username" + ":" + "password";
String encoding = new BASE64Encoder().encode(usernamePassword.getBytes());
conn.setRequestProperty("Authorization", "BASIC " + encoding);
InputStream in = conn.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
IoUtil.transfer(in, out);
System.out.println(new String(out.toByteArray()));
}

把用户名和密码改掉了,嘿。只要你注册一个blogger的账号就可以顺利获得结果 :)

还有,blogger的atom API在

1 comment:

Unknown said...

忘了,补几个连接:
BASIC authentication

SSL connection