import com.sonalb.net.http.cookie.*;
import java.net.*;
import java.io.*;
...
public class Example
{
...
public void someMethod()
{
...
URL url = new URL("http://www.site.com/");
HttpURLConnection hUC = (HttpURLConnection) url.openConnection();
//在这里初始化HttpURLConnection.
...
huc.connect();
InputStream is = huc.getInputStream();
Client client = new Client();
CookieJar cj = client.getCookies(huc);
//进行一些处理
...
huc.disconnect();
// 执行另一请求
url = new URL("http://www.site.com/");
huc = (HttpURLConnection) url.openConnection();
client.setCookies(huc, cj);
huc.connect();
...
// 进行一些处理
}
}
package com.sonalb.net.http;
import com.sonalb.net.http.cookie.*;
import java.net.*;
import java.io.*;
public class HTTPRedirectHandler
{
...
public HTTPRedirectHandler(HttpURLConnection huc)
{
...
}
public void connect() throws IOException
{
if(bConnected)
{
throw new IllegalStateException("No can do. Already connected.");
}
int code;
URL url;
huc.setFollowRedirects(false);
// 设置在Cookies中的检验
if(!cj.isEmpty())
{
client.setCookies(huc,cj);
}
is = huc.getInputStream();
// 从HttpURLConnection中提取Cookies并加到CookieJar中去
cj.addAll(Client.getCookies(huc));
while((code = huc.getResponseCode()) != successCode && maxRedirects > 0)
{
if(code != 302)
{
throw new IOException("Can't deal with this code (" + code + ").");
}
is.close();
is = null;
url = new URL(huc.getHeaderField("location"));
huc.disconnect();
huc = null;
huc = (HttpURLConnection) url.openConnection();
//和HTTP请求一起发送Cookies
Client.setCookies(huc, cj);
huc.setFollowRedirects(false);
huc.connect();
is = huc.getInputStream();
//从响应中提取Cookies并加进jar中去
cj.addAll(Client.getCookies(huc));
maxRedirects--;
}
if(maxRedirects <= 0 && code != successCode)
{
throw new IOException("Max redirects exhausted.");
}
bConnected = true;
}
//其他方法在这里出现
public void handleCookies(boolean b)
{
...
}
public void setSuccessCode(int i)
{
...
}
public void setCookieJar(CookieJar cj)
{
...
}
public void addCookies(CookieJar cj)
{
...
}
public CookieJar getCookieJar()
{
...
}
public HttpURLConnection getConnection()
{
...
}
public void setMaxRedirects(int i)
{
...
}
}
public boolean doLogin() throws Exception
{
//对于HTTPS初始化JSSE
System.getProperties().put("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
//创建HttpURLConnection并初始化
URL url = new URL("https://lc2.law13.hotmail.passport.com/cgi-bin/dologin");
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
huc.setDoOutput(true);
huc.setRequestMethod("POST");
huc.setRequestProperty("User-Agent","Mozilla/4.7 [en] (Win98; I)");
//发送登录表单字段
StringBuffer sb = new StringBuffer();
sb.append("login="); sb.append(URLEncoder.encode(user));
...
OutputStream os = huc.getOutputStream();
os.write(sb.toString().getBytes("US-ASCII"));
os.close();
//创建句柄并进行处理
HTTPRedirectHandler hrh = new HTTPRedirectHandler(huc);
hrh.connect();
huc = hrh.getConnection();
//Microsoft有一个中间过渡页使用了一个刷新元标签以便于在HTTPS和HTTP间转换,这将防止安全
//警告弹出
//我们需要通过读取响应和解析URL手动取出URL
BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream()));
...
//一旦我们有了主页的URL,我们就又使用HTTPRedirectHandler重定向并处理响应以校验正确的注
//册
url = new URL(homeUrl);
huc = (HttpURLConnection) url.openConnection();
huc.setRequestProperty("User-Agent","Mozilla/4.7 [en] (Win98; I)");
hrh = new HTTPRedirectHandler(huc);
hrh.setCookieJar(cj);
hrh.connect();
...
//保存Cookies用于以后的请求
cj.addAll(hrh.getCookieJar());
...
return(bLoggedIn);
}
import java.io.*;
import org.w3c.tidy.*;
public class ConvertBadHTMLToGood
{
...
public ConvertBadHTMLToGood(Reader r)
{
if(r == null)
{
throw new IllegalArgumentException();
}
inReader = r;
}
public Reader doConvert() throws IOException
{
//初始化JTidy对象
Tidy tidy = new Tidy();
tidy.setXmlOut(true);
tidy.setErrout(new PrintWriter(new StringWriter()));
//JTidy解析器要求一个InputStream,对于我的知识来说这里没有直接的办法将一个Reader转换
//成一个InputStream。这个工作区代码没有字符编码安全,但还可以混过。
BufferedReader br = new BufferedReader(inReader);
StringBuffer sb = new StringBuffer();
String line;
while((line = br.readLine()) != null)
{
sb.append(line);
sb.append("");
}
ByteArrayInputStream bais = new ByteArrayInputStream(sb.toString().getBytes("US-ASCII"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//作一个将HTML转换well-formed HTML 的预备。
tidy.parse(bais, baos);
//整理一些遗漏的JTidy得到能被“true-blue”XML解析器解析的输出。
FixEntities fe = new FixEntities(baos.toString());
return(fe.getFixedReader());
}
import com.sonalb.net.http.cookie.*;
...
public class HotmailChecker
{
public static void main(String args[]) throws Exception
{
if(args.length != 2)
{
usage();
System.exit(0);
}
String uname = args[0];
String pass = args[1];
HotmailChecker hmc = new HotmailChecker(uname,pass);
if(!hmc.doLogin())
{
System.out.println("Could not login to Hotmail.");
System.exit(0);
}
Vector newMessages = hmc.getNewMessages();
if(newMessages == null)
{
System.out.println("No NEW Messages.");
return;
}
System.out.println("You have " + newMessages.size() + " NEW Messages");
System.out.println("---------------------------------------------");
Iterator iter = newMessages.iterator();
//HMMessage封装了一个Hotmail消息
HMMessage hm;
while(iter.hasNext())
{
hm = (HMMessage) iter.next();
System.out.println(" From: " + hm.getFrom());
System.out.println(" Subject: " + hm.getSubject());
System.out.println("Sent Date: " + hm.getSentDate());
System.out.println("---------------------------------------------");
}
}
static void usage()
{
System.out.println("Usage: java HotmailChecker ");
}
//实例变量和方法从这里开始
...
public HotmailChecker(String username, String passWord)
{
...
}
public boolean doLogin() throws Exception
{
...
}
public Vector getNewMessages() throws Exception
{
...
}
...
}
(出处:http://www.VeVb.com)
新闻热点
疑难解答