111,125
社区成员
发帖
与我相关
我的任务
分享public string getCode(string url)
{
if (!Regex.IsMatch(url, @"(http(s)?://)?([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?", RegexOptions.IgnoreCase))
{
throw new Exception("The url is invalid!");
}
try
{
//WebRequest mywebReq = WebRequest.Create(url);
//WebResponse mywebRep = mywebReq.GetResponse();
//Stream mystream = mywebRep.GetResponseStream();
//StreamReader sr = new StreamReader(mystream, e);
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
byte[] myDataBuffer = myWebClient.DownloadData(url);
string getValue = Encoding.Default.GetString(myDataBuffer);
Match charSetMatch = Regex.Match(getValue, "<meta([^<]*)charset=([^<]*)\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
string webCharSet = charSetMatch.Groups[2].Value;
if (webCharSet == null || webCharSet == "")
{
webCharSet = "utf-8";
}
getValue = Encoding.GetEncoding(webCharSet).GetString(myDataBuffer);
return getValue;
}
catch
{
throw new Exception("Request timed out!");
}
}