1.模拟登录,并记录登录信息。主要是sessionid,对java开发的网站特别有效,在做门户集成项目的时候发现这个cookie简直太重要了。
public String openSession()
{
String urlStr = "";
OutputStream out = null;
InputStream in = null;
try {
URL url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
// 打印请求头信息
Map hfs = con.getHeaderFields();
Set<String> keys = hfs.keySet();
for (String str : keys) {
List<String> vs = (List) hfs.get(str);
System.out.print(str + ":");
for (String v : vs) {
System.out.print(v + "\t");
}
System.out.println();
}
System.out.println("-----------------------");
String cookieValue = con.getHeaderField("Set-Cookie");
System.out.println("cookie value:" + cookieValue);
String sessionId = cookieValue.substring(0,
cookieValue.indexOf(";"));
return sessionId;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
if (in != null)
in.close();
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2,下载数据核心代码
public void downImgCore(String sessionId,String sUrl,String sFileUrl)
{
InputStream in = null;
try
{
String url = sUrl;//
URL u = new URL(url);
HttpURLConnection con = (HttpURLConnection) u.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Cookie", sessionId);
con.setConnectTimeout(500);
in = con.getInputStream();
File img = new File(sFileUrl);
// 生成图片
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(img));
byte[] buf = new byte[2048];
int length = in.read(buf);
while (length != -1) {
out.write(buf, 0, length);
length = in.read(buf);
}
out.close();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
try {
if (in != null)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}