how to display full Url in web browser control properly in C#? - c#

I can display the Url in the searchbar. However, it picks up javascript and other loading processes as URL and displays them and doesn't display a conventional Url (e.g https://stackoverflow.com/questions) that we would see in any common browser. So if I search http://www.stackoverflow.com, I get https://ssum-sec.casalemedia.com/usermatch?s=183712&cb=https://engine.adzerk.net/udb/22/sync/i.gif?partnerId=1&userId=. Any help would be appreciated.
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
player.SoundLocation = "safepage.wav";
player.Play();
if (SearchBar.Text != e.Url.ToString()) //Displays the full Webpage address within the textbox
{
SearchBar.Text = e.Url.ToString();
}
}

Navigation events of web browser control will raise for iframes of the page too. So in this case, what you are getting as result is address of an iframe in the page.
You can use webBrowser1.Url.ToString() instead of e.Url.ToString().
Note: As far as I know, there should not be an iframe in stackoverflow questions page, so itt seems your browser has been infected.

In the _Navigated event, you can use the WebBrowser.Url property. It will get updated each time but not for the redirects like for those tracking/ad scripts.

Related

Javascript error when loading a page in the winforms webbrowser control

I have a webbrowser control on my Windows form. I call the this URL.
http://www.oddsportal.com/soccer/argentina/primera-division-2012-2013/results/
Then I get the following javascript error:
An error has occurred in the script on this page:
URL: http://r1.oddsportal.com/x/global-130724112306.js
Also, all the columns in the table, next to the soccer matches, are empty (all contain: - ) instead of decimal numbers. I believe it has something to do with the js file in the above error message.
When I open the page in the normal windows browser (IE10) it works fine, no error message and all the information on the page is present. Also, I didn't get this error message a month ago when I used my program intensively so I guess something has changed recently on the website I'm requesting.
I had no results finding a solution for this. Hopefully anyone can help!
Thanks,
Jasper
My code (easy to reproduce, just drag a webbrowser on the form, add the DocumentCompleted event and copy the code):
private string url = "http://www.oddsportal.com/soccer/argentina/primera-division-2012-2013/results/";
public FrmMain()
{
webBrowser.Url = new Uri(url);
}
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url.AbsoluteUri == url)
{
// Do something
}
}

Change contents without redirecting to about:blank

I'm currently working on an application that modifies a specific web page to hide irrelevant information and the displays it in a WebBrowser control in the application window. Unfortunately as soon as i set the DocumentText Property of the WebBrowser, it navigates to about:blank and the displays the HTML content. However, because it redirects to about:blank, all relative element in the web page become invalid, creating a very odd looking web page with no stylesheet what so ever.
Is there a way i can modify what the WebBrowser control displays, without having it redirect to about:blank and therefore ruining all relative elements?
This should work for injecting a HTML Element to your page, without resetting the rest of the DOM:
private void button1_Click(object sender, EventArgs e)
{
HtmlElement myElem = webBrowser1.Document.CreateElement("input");
dynamic element = myElem.DomElement;
element.SetAttribute("value", "Hello, World!");
(webBrowser1.Document.GetElementsByTagName("body")[0]).AppendChild(myElem);
}

ASP.NET: How to handle all page redirection?

In an ASP.NET page, are there any practical ways to handle all of these following:
PostBack
Redirection when user click a link in the page
Redirection when user change url field in the browser
with just a single web control (or method)?
TQ
Postback can be handled on the Server Side
the two others - link click or url field are ought to be handled using Javascript only.
UPDATE:
You can use jQuery to show a "loading animation" as mentioned in the question
$(window).unload( function () { alert("Bye now!"); } );
This will issue an alert once the user tries to leave the page. you can change it so it will display an animating gif image.
This must be tested though. I think it's pointless; not too sure on how long the gif will be displayed until the browser starts to load up the new page.
EDIT:
As I mentioned in comments, you can issue a postback using javascript
so, according to my last edit
$(window).unload( function () { __doPostBack("leaving"); } );
will issue a postback to the server, and you can catch it by:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.PreLoad += (sender, args) =>
{
this.ClientScript.GetPostBackEventReference(this, "arg");
if (!IsPostBack) { return; }
string __targetaction = this.Request["__EVENTTARGET"];
string __args = this.Request["__EVENTARGUMENT"];
if (string.IsNullOrEmpty(__args)) return;
if (__targetaction == "leaving")
{
doSomething();
}
};
}
then again, I'm not too sure this will be too helpful, since the user is leaving the page.
PostBack
In this Page.IsPostBack will be = true
Redirection when user click a link in the page
In this case you will get value in referrer. You can use Request.ServerVariables["http_referer"] OR Request.UrlReferrer
Redirection when user change url field in the browser
Else this is the 3rd case

HttpRedirect on every link within FBML application, why?

I have set up the SDK on my FB application but for the life of me cannot work out why the redirection happens.
The app is an IFrame so, for testing I have two pages, on page one a link to page two, when I click the link the whole page is redirecting as opposed to the IFrame src redirecting.
Both pages are checking to see if the user is logged in with the following code..
protected string requiredAppPermissions = "user_about_me,email";
protected FacebookApp fbApp;
protected CanvasAuthorizer authorizer;
protected void Page_Load(object sender, EventArgs e)
{
fbApp = new FacebookApp();
authorizer = new CanvasAuthorizer(fbApp);
authorizer.Perms = requiredAppPermissions;
if (authorizer.Authorize())
{
}
}
I have had a look in source and can see this in the FacebookAppRedirectHttpHandler, I just can't understand why you would want to keep redirecting the full page for every navigation link?
The most important reason is that Facebook passes the authentication to the signed_request to the source on every request. It either does this with a POST in the body or with a GET in the querystring. The reason we do this is because cookies aren't 100% reliable. If we redirected inside the iframe we would have to store the user's session in a cookie. Some browsers, including safari, don't let iframe apps create cookies. There are ways around this, but for most people the way we have it works best. If you want to have a redirect inside the iframe without changing the top url you will have to save the session in some way and pass it to the second page. You could do this by adding it to the querystring (complicated) or storing it in the Session (not very scalable) or using cookies (not reliable).

Response.Redirect causes download of the swf

I have a flash image slider with a button below each image.
When i press that button, the user is redirected to a new page
where i add that image product to my cart.
The problem is that after doing the adding, i want to redirect the user back to the initial page.
The code:
protected void Page_Load(object sender, EventArgs e)
{
addProductToBasket(getCategoryIdFromUrl(), getProductIdFromUrl());
Response.Redirect(Request.UrlReferrer.ToString());
}
Please note that in Firefox is working fine but in IE or Chrome it is DOWNLOADING the swf...If i comment Response.Redict(...) the user remains on this page so the click button is working well, only the redirect seems to be the problem.
Any suggestions please?
Edit: The problem seems to be that Request.UrlReferrer keeps as link not the initial page containing the swf but the swf itself....
So, instead of doing redirect to:
http://localhost:1336/Site/Index.aspx
if does redirect to the swf contained on the Index.aspx page
http://localhost:1336/carousel/carouse.swf
Solved: with a session variable where i keep the initial page's url
It seems to me that it's a function of the flash player setting the referrer header differently in different browsers.
If that is the case, then you might want to have the flash player get the url of the page it is hosted on, and pass that as a parameter to your page, and then redirect to the contents of the parameter.
What I think you really should be doing though is registering an HttpHandler (IHttpHandler implementation) for the URL which performs the processing and creating a response which returns JSON or XML, which flash can easily parse.
This way, you don't have a page reload and you have a seamless experience.

Categories

Resources