public static CookieContainer Cookies = new CookieContainer();
public static string GetWebString(string url, string referer = null, Encoding encode = null)
{
string webdat = null;
HttpWebRequest req = null;
HttpWebResponse res = null;
try
{
req = (HttpWebRequest)WebRequest.Create(url);
req.CookieContainer = Cookies;
req.AllowAutoRedirect = false;
req.Timeout = 10000;
req.Referer = referer;
req.Proxy = null;
req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";
res = (HttpWebResponse)req.GetResponse();
Cookies.Add(res.Cookies);
using (StreamReader reader = new StreamReader(res.GetResponseStream(), encode == null ? Encoding.UTF8 : encode))
{
webdat = reader.ReadToEnd();
}
//using (FileStream fs = new FileStream("webdat.txt", FileMode.Create))
//{
// byte[] data = Encoding.UTF8.GetBytes(webdat);
// fs.Write(data, 0, data.Length);
//}
}
catch (HttpException httpex)
{
return "HttpException:" + httpex + "";
}
catch (WebException webex)
{
return "WebException:" + webex + "";
}
finally
{
if (res != null)
{
res.Close();
}
if (req != null)
{
req.Abort();
}
}
return webdat;
}