2017-02-08 7 views
1

私のアプリケーションはほぼ30のフィールドを自動入力し、ユーザとキャプチャがいっぱいになります。URLからSSL/TSLを取得した画像

URLからキャプチャを取得して表示する必要があります。

私は別の方法を試みましたが、必要なものはどれも動作しません。

urlはIEであなたがダウンロードするファイルを取得します、あなたが直接画像を取得するクロムとcaptcha link

です。

最初の試行

WebBrowserコントロール と

urlを開きますあなたがWebブラウザーでURLを開くしようとした場合、あなたは正しい画像を得るでしょうcap.gifまたはJPGのようにそれを保存する場合は、ファイルのダウンロード で始まるでしょう。

この時点では、ユーザーにダウンロードダイアログを表示しないように自動ダウンロードタスクを試行しています。他の同様

だから私はあなたが(0バイト)の画像ファイルが空を取得します、Webブラウザーナビゲーション結果のコールバック上のWebClient

WebClient webClient = new WebClient(); 
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); 
webClient.DownloadFileAsync(new Uri(url), filepath); 

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) 
{ 
    e.Cancel = true; 
    WebClient client = new WebClient(); 

    client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted); 
    client.DownloadDataAsync(e.Url); 
} 

または直接に対処しようとDownload file and automatically save it to folderに答えます。

AsyncCompletedEventArgsを見ると、SSL/TSLに関するエラーが生成されます。

The request was aborted: Could not create SSL/TLS secure channel. 
in System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 
in System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result) 
in System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result) 

は、SSL/TSLがSOとしてWebクライアントにスタック含める二回目は、これは、コールバック

3回目の試み から画像を取得する上で同じエラーが返されますThe request was aborted: Could not create SSL/TLS secure channel

ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(AllwaysGoodCertificate); 
ServicePointManager.Expect100Continue = true; 
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; 

WebClient webClient = new WebClient(); 
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); 
webClient.DownloadFileAsync(new Uri(captchaUrl), filepath); 

答えますWebBrowserコントロールとタグをPictureBoxに変換

私はメインページのコンテンツのフォーム上の画像を抽出しようとする場合は、目に見えるWebBrowserコントロールを持っている場合、それは私がイメージが従う 取得することができますので、この作業罰金How to copy the loaded image in webbrowser to picturebox

[DllImport("user32.dll")] 
public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags); 

public Bitmap CaptureWindow(Control ctl) 
{ 
    //Bitmap bmp = new Bitmap(ctl.Width, ctl.Height); // includes borders 
    Bitmap bmp = new Bitmap(ctl.ClientRectangle.Width, ctl.ClientRectangle.Height); // content only 
    using (Graphics graphics = Graphics.FromImage(bmp)) 
    { 
     IntPtr hDC = graphics.GetHdc(); 
     try { PrintWindow(ctl.Handle, hDC, (uint)0); } 
     finally { graphics.ReleaseHdc(hDC); } 
    } 
    return bmp; 
} 

//Methods to get Co-ordinates Of an Element in your webbrowser 
public int getXoffset(HtmlElement el) 
{ 
    int xPos = el.OffsetRectangle.Left; 
    HtmlElement tempEl = el.OffsetParent; 
    while (tempEl != null) 
    { 
     xPos += tempEl.OffsetRectangle.Left; 
     tempEl = tempEl.OffsetParent; 
    } 
    return xPos; 
} 

public int getYoffset(HtmlElement el) 
{ 
    int yPos = el.OffsetRectangle.Top; 
    HtmlElement tempEl = el.OffsetParent; 
    while (tempEl != null) 
    { 
     yPos += tempEl.OffsetRectangle.Top; 
     tempEl = tempEl.OffsetParent; 
    } 
    return yPos; 
} 

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
{ 
    // get captcha 
    HtmlElement el = webBrowser1.Document.GetElementById("imgCaptcha"); 
    IHTMLImgElement img = (IHTMLImgElement)el.DomElement; 
    Bitmap bmp = new Bitmap(img.width, img.height); 

    int CaptchaWidth = getXoffset(el); 
    int CaptchaHeight = getYoffset(el); 
    Rectangle rect = new Rectangle(CaptchaWidth, CaptchaHeight, img.width, img.height); 

    // with this image il always blank 
    //webBrowser1.DrawToBitmap(bmp, rect); 

    Bitmap bitmap = CaptureWindow(webBrowser1); 
    Bitmap croppedImage = bitmap.Clone(rect, System.Drawing.Imaging.PixelFormat.Undefined); 
    pictureBox1.BackgroundImage = croppedImage; 
} 

に答えるが、生憎これだけの作品が... :(

何か提案がありがとうございます。

答えて

1

いくつかの回避策の後、SSL、サーバープロセスプロトコルTSL 1で1つの問題が見つかりました。2の代わりにSSL3

はちょうど

//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; 
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 

を交換し、他のいくつかのいずれかが、他の実装を持っている場合、今のWebClientはうまく動作しますが、前に自分自身に正しい答えを与え、私は待ちます。

1

私は非同期を使用することですなるだろう変更/ので、私は

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 

非同期を使用して、Webクライアントの

この部分あなたが行ったようにDownloadFileCompletedの設定を心配する必要はないでしょう待ちます方法

public async Task DownloadIt(string url ,string filename) 
{ 
    using (var client = new WebClient()) 
    { 
     await client.DownloadFileTaskAsync(new Uri(url), filename); 
    } 
} 

いつでも必要なときにこのように呼び出します。

await DownloadIt("https://www.com", @"C:\Path\To\Save"); 
関連する問題