C# uses regular expression matching to obtain the specified value of the <input> tag in the html string (available for pro-test)

Sealed function: 

/*
 * / <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);
}

Call example:

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();
}

 

Guess you like

Origin blog.csdn.net/qq15577969/article/details/114644734