Read iframe content using Webbrowser C# - c#

I know that I can access all iframes using the following properties of webbrowser:
string html = webBrowser1.Document.Window.Frames[0].WindowFrameElement.InnerText;
But I'm struggling with cross-domain restriction..
My document url is like www.subdomain1.sport.com/...
And iframes url is like www.subdimain2.sport.com/...
How to access iframes content and put some text into input tag there?

I think you must refer the following URL to get the content of IFrame which exists on cross domain.
http://codecentrix.blogspot.com/2008/02/when-ihtmlwindow2document-throws.html

Related

HTTP web request GET C#

I am trying to create HTTP Request to get the full html source with CSS styles. I already got the HTML source but without CSS styles ! , is there any other way to get full HTML with CSS styles ?
// create an instance
WebClient webClient = new WebClient();
// call the HTML page you want to download, and get it as a string
string htmlCode = webClient.DownloadString(uri);
return htmlCode;
Actually you can only get page text with "DownloadString" method. If css written in external file you can't get it.
Try this links: With Css - https://www.google.com/
Without Css - https://www.facebook.com/
In google head tags include some css and you can get them. But facebook don't.
You will have to include the full css path of the website you trying to get the css content from for example : www.data.com/css/stylex.css

C# getting the HTML document from a Javascript Iframe

I am trying to access this webpage http://www.pof.com with C# code.
I figured out that the Document element is stored in an iframe after I successfully logged in as a user and I am not familiar with how to access the document element.
All I want to do is to get the HTML format of that page which is loaded with an iframe and go to some of the links of that site.
Use following code:
document.getElementById('iframe1').contentWindow.document
or simply,
var elemVal;
if (iframeDocument) {
elemVal= iframeDocument.getElementById('#iframe1');
}

How to assign HTML content to iframe control from asp.net codebehind?

I want to assign the html content to iframe control from asp.net code behind page;
this is working fine
myIframe.Attributes.Add("src", "pathtofilewith.html");
but, i don't want to give the path of html file to display i just want to assign some html content which comes from database to iframe control.
i want some thing like this(Ashok) to be displayed in iframe control
i tried the bellow ways but nothing is succesful
myIframe.Attributes["innerHTML"] = "<h1>Thank You..</h1>";
myIframe.Attributes.Add("innerHTML", "<h1>Ashok</h1>");
A way to communicate between two different pages where one is in an IFrame on the other, is to post data using JQuery. An example is given in this StackOverflow question
It is also discussed in this other StackOverflow question
On this page, you will also find a short and simple example of how you can put content in an IFrame without using a separate web-page for it (note the lacking src attribute!).
Hope some of this helps!
You can't. That's not how an IFRAME works - it is for use with the src attribute as you've already discovered.
Perhaps you want to create a DIV instead
There is no way to insert HTML content into iframe tag directly, but you can create a page which gets the content form the database and then you can view it using iframe,
or
You can create a page for example called getContent.aspx which request value from the URL e.g. getContent.aspx?content=<h1>Thank You..</h1> and display it wherever you like, and then call it from iframe.

Parsing out a hidden field inside iframe using HtmlAgilityPack .NET

I'm having problems picking out data I need that's inside an iframe form. Is it even possible using HtmlAgilityPack? Here's a screenshot using Firebug so it's easier for you guys to see.
http://i.stack.imgur.com/ftt84.jpg
I need to parse out the post_form_id. I've tried
var value = doc.DocumentNode.SelectSingleNode("//input[#type='hidden' and #name='post_form_id']")
.Attributes["value"].Value;
but obviously won't work because it's inside the iframe form. Appreciate any help.
I would
Use the HTMLAglityPack to find the iframe location
Use the System.URI class find the absolute link of the iframe page
Open this iframe page
Use HTMLAglityPack again on the iframe page to find the required information

HttpWebRequest reades only homepage

Hi I tried to read a page using HttpWebRequest like this
string lcUrl = "http://www.greatandhra.com";
HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
loHttp.Timeout = 10000; // 10 secs
loHttp.UserAgent = "Code Sample Web Client";
HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
Encoding enc = Encoding.GetEncoding(1252); // Windows default Code Page
StreamReader loResponseStream =
new StreamReader(loWebResponse.GetResponseStream(), enc);
string lcHtml = loResponseStream.ReadToEnd();
mydiv.InnerHtml = lcHtml;
// Response.Write(lcHtml);
loWebResponse.Close();
loResponseStream.Close();
i can able to read that page and bind it to mydiv. But when i click on any one of links in that div it is not displaying any result. Because my application doesnt contain entire site. So what we will do now.
Can somebody copy my code and test it plz
Nagu
I'm fairly sure you can't insert a full page in a DIV without breaking something. In fact the whole head tag may be getting skipped altogether (and any javascript code there may not be run). Considering what you seem to want to do, I suggest you use an IFRAME with a dynamic src, which will also hopefully lift some pressure off your server (which wouldn't be in charge of fetching the html to be mirrored anymore).
If you really want a whole page of HTML embedded in another, then the IFRAME tag is probably the one to use, rather than the DIV.
Rather than having to create a web request and have all that code to retrieve the remote page, you can just set the src attribute of the IFRAME to point ot the page you want it to display.
For example, something like this in markup:
<iframe src="<%=LcUrl %>" frameborder="0"></iframe>
where LcUrl is a property on your code-behind page, that exposes your string lcUrl from your sample.
Alternatively, you could make the IFRAME runat="server" and set its src property programatically (or even inject the innerHTML in a way sismilar to your code sample if you really wanted to).
The code you are putting inside .InnerHtml of the div contains the entire page (including < html >, < body >, < /html > and < /body> ) which can cause a miriad of problems with any number of browsers.
I would either move to an iframe, or consider some sort of parsing the HTML for the remote site and displaying a transformed version (ie. strip the HTML ,BODY, META tags, replace some link URLs, etc).
But when i click on any one of links in that div it is not displaying any result
Probably because the links in the download page are relative... If you just copy the HTML into a DIV in your page, the browser considers the links relative to the current URL : it doesn't know about the origin of this content. I think the solution is to parse the downloaded HTML, and convert relative URLs in href attributes to absolute URLs
If you want to embed it, you need to strip everything but the body part. That means that you have to parse your string lcHTML for <body....> and remove everything before and includeing the body tag. You must also strip away everything from </body>. Then you need to parse the string for all occurences of <a href="....."> that do not start with http:// and include h t t p://www.greatandhra.com or set <base target="h t t p://www.greatandhra.com"> in your head section.
If you don't want to embed, simply clear the response buffer and stream the lcHTML string back to the browser.
PS: I had to write all h t t p with spaces to be able to post this.
Sounds like what you are trying to do is display a different site embedded in your site. For this to work by dropping it into a div you would have to extract the code between the body tags as it wouldn't be valid with html and head in the middle of another page.
The links won't work because you've now taken that page out of context in your site so you'd also have to rewrite any links on the page that are relative (i.e. don't start with http) to point to a page on your site which will then fetch the other sites page and display them back in your site, or you could add the url of the site you're grabbing to the beginning of all the relative links so they link back to that site.

Categories

Resources