c#读取网页内容
public static class Internet
{
private static HttpWebRequest request;
private static HttpWebResponse response;
private static StreamReader reader;
private static string strResults;
private static Encoding encoding;
static Internet()
{
strResults = string.Empty;
}
public static string GetHtml(string strLink)
{
try
{
request = (HttpWebRequest)WebRequest.Create(strLink);
switch ((DateTime.Now.Second % 2))
{
case 0:
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1;.NET CLR 2.0.50727)";
break;
default:
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24";
break;
}
response = (HttpWebResponse)request.GetResponse();
if (response.CharacterSet != null && response.CharacterSet.ToLower() == "utf-8")
{
encoding = Encoding.UTF8;
}
else
{
encoding = Encoding.GetEncoding(936);
}
reader = new StreamReader(response.GetResponseStream(), encoding);
strResults = Regex.Replace(reader.ReadToEnd(), @"[\r\n]", " ");
if (strLink != response.ResponseUri.ToString())
{
strResults = "|RedirectWeb|" + strResults;
}
return strResults;
}
catch
{
return string.Empty;
}
finally
{
request.Abort();
if (response != null)
{
response.Close();
}
if (reader != null)
{
reader.Close();
reader.Dispose();
}
}
}
}
java 读取网页内容
public static String getPageContent(String strUrl, String strPostRequest,
int maxLength) throws Exception {
//读取结果网页
StringBuffer buffer = new StringBuffer();
System.setProperty("sun.net.client.defaultConnectTimeout", "5000");
System.setProperty("sun.net.client.defaultReadTimeout", "5000");
try {
URL newUrl = new URL(strUrl);
HttpURLConnection hConnect = (HttpURLConnection) newUrl
.openConnection();
//POST方式的额外数据
if (strPostRequest.length() > 0) {
hConnect.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(hConnect
.getOutputStream());
out.write(strPostRequest);
out.flush();
out.close();
}
//读取内容
BufferedReader rd = new BufferedReader(new InputStreamReader(hConnect.getInputStream()));
int ch;
for (int length = 0; (ch = rd.read()) > -1 && (maxLength <= 0 || length < maxLength); length++) {
buffer.append((char) ch);
}
rd.close();
hConnect.disconnect();
return buffer.toString().trim();
} catch (Exception e) {
// return "错误:读取网页失败!";
return null;
}
}