Close webview xamarin forms - c#

I have implement payment method with return URL and using webview open URL with my app. Recent, I'am issue want auto close webview with response success or fail process payment.

I encountered this case. Here is the solution I found.
After creating the WebElement, we bind the Navigating event to a certain method.
MyPage.Source = link;
MyPage.Navigating += Webview_Navigating;
MyPage WebElment name.
link is my link address
.
Then we perform our transactions here.
private void Webview_Navigating(object sender, WebNavigatingEventArgs e)
{
var url = e.Url;
if (url.Contains("status=1"))
{
int ordID = 0;
//save order
}
}
For me, if the changed link address checkout status=1, the checkout is successful.
You need to call Url property in WebNavigatingEventArgs to catch the changed url
If useful, choose. Good Luck

Sure! You can get response form WebView.
I recommend you use HybridWebView (see: https://github.com/devsmadeofsteel/Plugin.HybridWebView) as alternative.
Examples:
In Xamarin page, create a browser add register a named callback, and add browser to page childs
HybridWebViewControl Browser = new HybridWebViewControl();
//You can load browser content by string or URL, here is string which you can hard code or store in resource files
Browser.ContentType = Plugin.HybridWebView.Shared.Enumerations.WebViewContentType.Internet;
Browser.Source = YourUrl;
Browser.AddLocalCallback(YourCallBackFunctionName, CallBackFunction);
When run, HybridWebViewControl will insert a function with the name you specified in
YourCallBackFunctionName, and the single parameter is string type. So, In JavaScript of the page loaded by YourUrl, you can call back with the specified CallBackFunctionName:
function AnyFunction() {
//...
YourCallBackFunctionName(YourStringParameter);
}
When you call YourCallBackFunctionName(YourStringParameter) in JavaScript, you can get YourStringParameter in CallBackFunction using C#.
So, you could define different CallBackFunctions or pass different parameters with single CallBackFunction, to control the HybridWebView and the page or view which contain it, such as hide HybridWebView or close the page.
Have a try!

Related

Getting URL(URI) address from Web Browser

I am trying to get the URL string (uri) in the WebBrowser component after navigation, but the returned address is incomplete.
It should look like "https://oauth.vk.com/blank.html#access_token=..." but
e.Uri.AbsoluteUri
and all other fields return only "https://oauth.vk.com/blank.html" (image)
A line beyond the "#" is not returned. I navigate to the address in the browser, everything is displayed normally. I didn't find the answer to this question anywhere, I hope, here will help me.
I tried to get the URL in different ways but failed to get the full string.
Here's an code example:
browser.Navigate(getTokenUrl);
browser.Navigated += (sender, e) =>
{
MessageBox.Show(e.Uri.AbsoluteUri);
};
JavaScript
Window.location.href will return all the url including hash part.
You can use apis to run this JavaScript in browser component and it should return you correct url.
The api to run JavaScript depends on browser component you are using.

Spotify authorization code flow (WPF browser control) C#

I'm using Spotify account service (authorization code flow) to get an authorization code by passing in the required parameters (client_id, response_type and redirect_uri. Its a WPF application so i am using the browser control and navigating the user to
https://accounts.spotify.com/authorize/?client_id=myclientId&response_type=code&redirect_uri=someUri
When i copy paste the link in the browser, i see the right stuff i.e page with the Login to Spotify button but when i am navigating through the browser control in my WPF application, it gives me a file download dialog with Authorize.json file to download with Open and Save options, however in some cases it presents the right page to browser code.
Below is my code:
public winOAuthBrowserForm(string navigateTo)
{
InitializeComponent();
webBrowser.Navigated += webBrowser_Navigated;
webBrowser.Navigate(navigateTo);
}
private void webBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
if(!String.IsNullOrEmpty(e.Uri.Query))
{
//since we are looking for code for authorization that will be exchanged for request token from the server
if (e.Uri.Query.StartsWith("?code=") || e.Uri.Query.Contains("code="))
{
code = HttpUtility.ParseQueryString(e.Uri.Query).Get("code");
this.Close();
}
if (e.Uri.Query.StartsWith("?error=") || e.Uri.Query.Contains("error="))
{
error = HttpUtility.ParseQueryString(e.Uri.Query).Get("error");
this.Close();
}
}
}
And i am calling this control like:
var uri = string.Format(SpotifyAuthUriFormatter, RequestAuthBaseUrl, clientId);
winOAuthBrowserForm form = new winOAuthBrowserForm(uri);
form.ShowDialog();
This is what i see:
UPDATE: When i right click on document and see the properties, i see it changes the URL to something like res://ieframe.dll/navcancl.htm#... I've searched it and found some solutions related to I.E (I'm using latest but you can't be sure that it'll be latest on client machines), some say that its firewall setting. The thing is, it appears sometimes only.
Any ideas? Thanks

redirect to a html page of other website c#

I want to redirect to another websites html page in my c# application when a user clicks on some button. I have written this
Response.Redirect("http://www.fashionunic.com/shop/wholesale-m04-dozen-chevron-laced-front-top-medium.html")
But it havent worked it is throwing path is not a valid virtual path
I am calling the below method in page load
Response.Redirect("http://www.fashionunic.com/shop/wholesale-m04-dozen-chevron-laced-front-top-medium.html")
and in
public void Application_BeginRequest()
{
var MyUri = Context.Request.Url;
if(MyUri.Contains("shop/wholesale") {
var String = MyUri.ToString();
Context.RewritePath("http://www.fashionunic.com/shop/wholesale-m04-dozen-chevron-laced-front-top-medium.html");
}
}
Exception:
Invalid path for child request '
http:/www.fashionunic.com/shop/wholesale-m04-dozen-chevron-laced-front-top-medium.html'.
A virtual path is expected.
I've tried your code, you should change 'Context.RewritePath' to 'Response.Redirect'. It should works.
Look here, it maybe useful for you: http://www.techrepublic.com/blog/software-engineer/two-approaches-to-redirection-in-aspnet/
Also, maybe you are marketer and you are trying to promote 'fashionunic' shop :-D

How to make page that redirects dynamically based on URL

Sorry for bad title, I really don't know how to describe this problem. Any suggestion is welcome.
I have to implement this in one large asp.net project (c#):
- user enters some url in browser, which should be in this form:
http://servername/directory/M1234N/2
where M1234N and 2 are some example values that can be different based on user needs.
Based on those two values, page should be redirected to another page. Basically, program should extract those two values from URL and based on that calculate where to redirect.
Is this even possible?
Thank you!
PS Sorry for bad post and title, feel free to correct me anytime
Yup it is possible. Initially, you have to read these values from the query. In order to do so, you should read the url
string url = HttpContext.Current.Request.Url.AbsoluteUri;
Then you have to split it based on "/".
string[] splittedUrl = text.Split('/').ToArray();
This way you will get an array, whose last two elements would be that you want:
string val1 = splittedUrl[splittedUrl.Length-1];
string val2 = splittedUrl[splittedUrl.Length-2];
Now based on the val1 and val2 you can find the page you want to redirect the user and you can redirect it as you would do in any other case.
Use this method.
Add a global.asax file to project
add this method to global.asax ile
void Application_BeginRequest(object sender, EventArgs e)
{
string para2;
string para1;
string CurrentPath = Request.Path.ToLower();
if (CurrentPath.Contains("http://servername/directory"))
{
//Get the two parameters from the url, here i am assuming that directory is static
//If not then you can change below two line for getting the passed parameters from url.
//I think it is easy task just use some string functions
para2 = CurrentPath.Substring(CurrentPath.LastIndexOf("/"));
para1 = CurrentPath.Substring(CurrentPath.IndexOf("directory"), CurrentPath.LastIndexOf("/"));
//Now work on these parameters and calculate the redirect page.
//Replcing the httpcontext with new page
HttpContext MyContext = HttpContext.Current;
MyContext.RewritePath("path of your new page according calculations");
}
}
In this method the application beginRequest will be called for every request.And you work on the current url.

Frame Redirect in C#

I would like to execute a frame redirect in C# from my managed module for the IIS 7.
When I call context.Response.Redirect(#"http://www.myRedirect.org");the correct page is shown but also the address is shown in the browser. And that is exactly what I do not want.
So I want something like:
private void OnBeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
// make a frame redirect if a specified page is called
if (context.Request.ServerVariable["HTTP_REFERER"].Equals(#"http://www.myPage.org/1.html"))
{
// perform the frame redirect here, but how?
// so something like
context.Response.Redirect(#"http://www.myRedirect.org");
// but as I said that doesn't redirect as I want it to be
}
}
Any ideas about that?
EDIT:
I tried the example, so I have:
private void OnBeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
// make a frame redirect if a specified page is called
if (context.Request.ServerVariable["HTTP_REFERER"].Equals(#"http://www.myPage.org/1.html"))
{
// perform the frame redirect here, but how?
context.Response.Write(#"<html>");
context.Response.Write(#"<head>");
context.Response.Write(#"</head>");
context.Response.Write(#"<frameset rows=""100%,*"" framespacing=""0"" frameborder=""NO"" border=""0"">");
context.Response.Write(#"<frame src=""http://www.myRedirect.org"" scrolling=""auto"">");
context.Response.Write(#"</frameset>");
context.Response.Write(#"<noframes>");
context.Response.Write(#"<body>Some text...");
context.Response.Write(#"</body>");
context.Response.Write(#"</noframes>");
context.Response.Write(#"</html>");
}
}
But that also doesn't correctly redirect. I still have the redirect address shown in my browser. So any other idea?
EDIT:
I obviously made a mistake. The code above works and does what I want. It first didn't work because my redirect url was doing something unexpected.
To perform a frame redirect you need to send back the HTML code containing a frameset with a single frame, with it's source set to http://www.myRedirect.org. As far as the server and browser is concerned no redirect has happened - it's just received some HTML code.
Performing a Response.Redirect will, as you've observed, cause the browser to make a fresh new request to the new page, showing the user the new address in the title bar. It's typically used for when a page actually changes its address, but the owners still want it reachable from the original URL as well.
EDIT: HTML frame redirect sample: http://en.wikipedia.org/wiki/URL_redirection#Frame_redirects

Categories