简体   繁体   中英

C# - Webbrowser and JScript

I want to extract the download url from streamcloud in my C#-Program. Therefore I have to wait 10 seconds, click on a button and click on the player. Then I can extract the download url from the page-source.

My problem:

I have 2 ways to do this:

  1. Automatic: I have to simulate a click on the button after 10 seconds. But my HtmlElement is always null.

  2. Manually: I created a form with a WebBrowser-control. But this control didn't show for example the button. I think the WebBrowser-control is blocking all jscript-content. Is there a way, that the WebBrowser shows all content?

Or is there a other way to get the download-url?

If you want to do this on a machine with browser installed (IE or Firefox), you could use any framework for automated testing. WatiN is a good one.

Something like this:

using (var browser = new IE("http://www.streamcloud.com/YOUR_VIDEO_URL"))
{
    browser.Button(Find.ByName("Play")).Click();
    // wait for 10 seconds, or better use some WatiN functionality like WaitUntilExists
    System.Threading.Thread.Sleep(10000);
    // extract URL
    var element = browser.Element(Find.ById("URL_CONTAINER_ID"));
    string videoUrl = element.Text;
}

Just use httpwebrequest or webclient:

public string ResolveStreamcloud(string url)
{
    var client = new WebClient();
    var reqParams = new NameValueCollection();

    var response = client.DownloadString(url);

    var regexObj = new Regex("<input.*?name=\"(.*?)\".*?value=\"(.*?)\">", RegexOptions.Singleline);
    var matchResults = regexObj.Match(response);
    while (matchResults.Success)
    {
        reqParams.Add(matchResults.Groups[1].Value, matchResults.Groups[2].Value);
        matchResults = matchResults.NextMatch();
    }

    Thread.Sleep(10500);

    byte[] responsebytes = client.UploadValues(url, "POST", reqParams);
    string responsebody = Encoding.UTF8.GetString(responsebytes);

    string resolved = Regex.Match(responsebody, "file: \"(.+?)\",", RegexOptions.Singleline).Groups[1].Value;

    if (!String.IsNullOrEmpty(resolved))
    {
        return resolved;
    }
    else
    {
        throw new Exception("File not found!");
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM