- 9.6.1. getUrlRespHtml的参数详解
- 9.6.2. getUrlRespHtml 的功能详解
- 9.6.3. getUrlRespHtml 的用法详解
- 9.6.3.1. getUrlRespHtml用法示例:只传入url而获得html
- 9.6.3.2. getUrlRespHtml用法示例:传入各种header信息
- 9.6.3.2.1. getUrlRespHtml用法示例:指定Referer
- 9.6.3.2.2. getUrlRespHtml用法示例:禁止自动跳转
- 9.6.3.2.3. getUrlRespHtml用法示例:手动设置Accept
- 9.6.3.2.4. getUrlRespHtml用法示例:不保持连接
- 9.6.3.2.5. getUrlRespHtml用法示例:设置Accept-Language
- 9.6.3.2.6. getUrlRespHtml用法示例:添加特定的User-Agent的header
- 9.6.3.2.7. getUrlRespHtml用法示例:设置ContentType
- 9.6.3.2.8. getUrlRespHtml用法示例:设置其他的特定的header
- 9.6.3.3. getUrlRespHtml用法示例:设置网页字符编码charset
- 9.6.3.4. getUrlRespHtml用法示例:设置网络超时timeout时间
- 9.6.3.5. getUrlRespHtml用法示例:设置Stream的读写超时readWriteTimeout时间
- 9.6.3.6. getUrlRespHtml用法示例:POST操作
// valid charset:"GB18030"/"UTF-8", invliad:"UTF8"
public string getUrlRespHtml(string url,
Dictionary<string, string> headerDict = defHeaderDict,
string charset = defCharset,
Dictionary<string, string> postDict = defPostDict,
int timeout = defTimeout,
string postDataStr = defPostDataStr,
int readWriteTimeout = defReadWriteTimeout)
{
string respHtml = "";
HttpWebResponse resp = getUrlResponse(url, headerDict, postDict, timeout, postDataStr, readWriteTimeout);
//long realRespLen = resp.ContentLength;
if (resp != null)
{
StreamReader sr;
Stream respStream = resp.GetResponseStream();
if (!string.IsNullOrEmpty(charset))
{
Encoding htmlEncoding = Encoding.GetEncoding(charset);
sr = new StreamReader(respStream, htmlEncoding);
}
else
{
sr = new StreamReader(respStream);
}
try
{
respHtml = sr.ReadToEnd();
//while (!sr.EndOfStream)
//{
// respHtml = respHtml + sr.ReadLine();
//}
//string curLine = "";
//while ((curLine = sr.ReadLine()) != null)
//{
// respHtml = respHtml + curLine;
//}
////http://msdn.microsoft.com/zh-cn/library/system.io.streamreader.peek.aspx
//while (sr.Peek() > -1) //while not error or not reach end of stream
//{
// respHtml = respHtml + sr.ReadLine();
//}
//respStream.Close();
//sr.Close();
//resp.Close();
}
catch (Exception ex)
{
//【未解决】C#中StreamReader中遇到异常:未处理ObjectDisposedException,无法访问已关闭的流
//http://www.crifan.com/csharp_streamreader_unhandled_exception_objectdisposedexception_cannot_access_closed_stream
//System.ObjectDisposedException
respHtml = "";
}
finally
{
if (respStream != null)
{
respStream.Close();
}
if (sr != null)
{
sr.Close();
}
if (resp != null)
{
resp.Close();
}
}
}
return respHtml;
}





