WPF通过api显示远端验证码并可点击刷新
效果图:
前端:
<Image Name="ImgValidate" Cursor="Hand" MouseLeftButtonDown="Image_MouseLeftButtonDown" Source="https://...(你使用的验证码api)" Height="40" Width="100" Stretch="Fill"/>
后端:
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
LoadValidateImg();
}
public void LoadValidateImg()
{
//获取毫秒时间戳
DateTime t = DateTime.Now;
string timestamp = t.ToString("yyyMMddhhmmssfff");
//视api的情况而定,有的api用的是随机数。那这里的t就改为获取随机数。
string imgUrl = "https://...(使用的验证码api)?t=" + timestamp;//验证码请求地址
BitmapImage image = new BitmapImage(new Uri(imgUrl, UriKind.RelativeOrAbsolute));
ImgValidate.Source = image;
}
包含cookies等更复杂的验证码获取请点击:https://blog.csdn.net/vastz/article/details/124198952