Any workaround to get text in an iFrame on another domain in a WebBrowser?

a 夏天 提交于 2019-12-05 10:52:48

Can this be useful for you?

foreach (HtmlElement elm in webBrowser1.Document.GetElementsByTagName("iframe"))
{
     string src = elm.GetAttribute("src");
     if (src != null && src != "")
     {
          string content = new System.Net.WebClient().DownloadString(src); //or using HttpWebRequest
          MessageBox.Show(content);
     }
}

Do you just need a way to request content from code?

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webRequest.URL);
request.UserAgent = webRequest.UserAgent;
request.ContentType = webRequest.ContentType;
request.Method = webRequest.Method;

if (webRequest.BytesToWrite != null && webRequest.BytesToWrite.Length > 0) {
    Stream oStream = request.GetRequestStream();
    oStream.Write(webRequest.BytesToWrite, 0, webRequest.BytesToWrite.Length);
    oStream.Close();
}

// Send the request and get a response
HttpWebResponse resp = (HttpWebResponse)request.GetResponse();

// Read the response
StreamReader sr = new StreamReader(resp.GetResponseStream());

// return the response to the screen
string returnedValue = sr.ReadToEnd();

sr.Close();
resp.Close();

return returnedValue;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!