public static BitmapImage GetCAPTCHA(Uri uri, out CAPTCHAData data) { string image; string sync; string hash; Regex r_image = new Regex("<img src=" + @"\" + '"' + "data:image/jpeg;base64,(?<image>[^.]*)" + @"\" + '"' + " " + "class=" + @"\" + '"' + "d-block mw-100" + @"\" + '"' + ">"); //<img src=\"(?<image>[^.]*)\" class=\"d-block mw-100\"> Regex r_sync = new Regex("<input name=" + @"\" + '"' + "sync" + @"\" + '"' + " " + "type=" + @"\" + '"' + "hidden" + @"\" + '"' + " " + "value=" + @"\" + '"' + @"(?<sync>\d+)" + @"\" + '"' + ">"); //<input name=\"sync\" type=\"hidden\" value=\"(?<sync>\d+)\"> Regex r_hash = new Regex("<input name=" + @"\" + '"' + "hash" + @"\" + '"' + " " + "type=" + @"\" + '"' + "hidden" + @"\" + '"' + " " + "value=" + @"\" + '"' + "(?<hash>[^" + '"' + "]*)" + @"\" + '"' + ">"); //<input name=\"sync\" type=\"hidden\" value=\"(?<hash>[^"]*)\"> string CAPTCHAPage = null; try { CAPTCHAPage = new WebClient().DownloadString(uri); } catch (WebException e) { StreamReader sR = new StreamReader(e.Response.GetResponseStream()); CAPTCHAPage = sR.ReadToEnd(); } Match m_image = r_image.Match(CAPTCHAPage); Match m_sync = r_sync.Match(CAPTCHAPage); Match m_hash = r_hash.Match(CAPTCHAPage); image = m_image.Groups["image"].Value; sync = m_sync.Groups["sync"].Value; hash = m_hash.Groups["hash"].Value; if (image == string.Empty | sync == string.Empty || hash == string.Empty) { throw new Exception("Failed Get CAPTCHAdata"); } else { data = new CAPTCHAData(); data.sync = sync; data.hash = hash; data.uri = uri; } BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = new MemoryStream(Convert.FromBase64String(image)); bitmapImage.EndInit(); return(bitmapImage); }
public static CookieCollection PostCAPTCHA(string response, CAPTCHAData data) { WebClient client = new WebClient(); string formData = "response=" + response + "&" + "sync=" + data.sync + "&" + "hash=" + data.hash; client.Referer = data.uri.ToString(); client.Host = data.uri.Host; client.ContentType = "application/x-www-form-urlencoded"; client.UploadString(data.uri, formData); string Header = client.ResponseHeaders.Get("Set-Cookie"); client.Dispose(); return(WebClient.GetAllCookiesFromHeader(Header, data.uri.Host.ToString())); }