封好的函数:
/*
* / <summary>
* / 通过正则表达式获取html字符串中<input>标签指定value值
* / </summary>
* / <param name="FileString">包含html的字符串</param>
* / <param name="inputName">指定<input>控件的名称</param>
* / <returns></returns>
*/
public string GetInput( string FileString, string inputName )
{
string inputValue = "";
MatchCollection matches = Regex.Matches( FileString, @"(?is)<input[^>]+type=""(?:hidden)""[^>]+name=""(?:" + inputName + @")""[^>]+value=""(.+?)""[^>]*>" );
foreach ( Match match in matches ){
inputValue += match.Groups[1].Value;
}
return(inputValue);
}
调用示例:
string url = "http://链接地址";
WebRequest wrt = null;
WebResponse wrp = null;
try
{
wrt = WebRequest.Create(url);
wrt.Timeout = timeout;
wrt.Credentials = CredentialCache.DefaultCredentials;
wrp = wrt.GetResponse();
StreamReader sr = new StreamReader(wrp.GetResponseStream(), Encoding.Default);
html = sr.ReadToEnd();
string username= GetInput(html, "username");
}
catch (Exception)
{
}
finally
{
if (wrp != null)
wrp.Close();
if (wrt != null)
wrt.Abort();
}