java 取WEB资源的内容
作者:独钓寒江月
有时候需要取得网页内容,再分析处理。
会话管理、post方法等见代码注释。
<code>
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class GetHttp
{
public String getValue(String url)
{
long stime = System.currentTimeMillis();
String r = null;
try{
HttpURLConnection huc;
InputStream in;
URL url1 = new URL(url);
huc = (HttpURLConnection) url1.openConnection();
// 设置允许output
if (!huc.getDoOutput())
{
huc.setDoOutput(true);
}
// 设置为post/get方式
huc.setRequestMethod("GET");
/*
// 设置为cookie
if(sessionId != null)
{
huc.setRequestProperty("Cookie", sessionId);
}
*/
//模拟浏览器
huc.setRequestProperty("User-Agent","Mozilla/4.7 [en] (Win98; I)");
//设置超时 3秒
huc.setConnectTimeout(3000);
/*
///////////////// 如果使用post方式,参数的传递
// StringBuffer sb = new StringBuffer();
sb.append("op=4117");
sb.append("&i=17");
sb.append("&o=0");
sb.append("&l=110");
sb.append("&t=WANGLUOGUIJI");
sb.append("&u=4");
sb.append("&d=1");
// sb.append(pars);
// sb.append("&spid=" + Const.FEE_SPID);
// sb.append("&spv=" + Const.FEE_SP_VALIDATE);
// post信息
// OutputStream os = huc.getOutputStream();
// os.write(sb.toString().getBytes("utf8"));
// os.close();
//huc.setDoOutput(false);
*/
/////////////////////开始读 服务器的返回
/*
//cookie
String cookieVal = huc.getHeaderField("Set-Cookie");
if(cookieVal != null)
{
sessionId = cookieVal.substring(0, cookieVal.indexOf(";"));
}
*/
in = huc.getInputStream();
// BufferedReader breader = new BufferedReader(new InputStreamReader(in , "utf8"));
BufferedReader breader = new BufferedReader(new InputStreamReader(in , "gb2312"));
String str=breader.readLine();
while(str != null)
{
if(r != null)
r += str;
else
r = str;
str = breader.readLine();
}
//断开连接
huc.disconnect();
long etime = System.currentTimeMillis();
System.out.println(".########################### web connect time : " + (etime-stime));
return r;
}
catch(Exception e)
{
e.printStackTrace();
return "";
}
}
public static void main(String[] args ) throws Exception
{
String url = "http://www.baidu.com/";
GetHttp gh = new GetHttp();
String http = gh.getValue(url);
gh = null;
System.out.println(http);
}
}
</code>