Getting URL(URI) address from Web Browser - c#

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.

Related

Close webview xamarin forms

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!

login to ajax web page from c# code

i'm trying to log in a site with username + password through a c# code.
i found out that it uses Ajax to authenticate...
how should i implement such login ?
the elements in the web page doesn't seem to have an "id"...
i tried to implement it using HtmlAgilityPack but i don't think this is the correct direction...
i can't simulate a click button since i don't find "id" for the button.
if (tableNode.Attributes["class"].Value == "loginTable")
{
var userInputNode =
tableNode.SelectSingleNode("//input[#data-logon-popup-form-user-name-input='true']");
var passwordInputNode =
tableNode.SelectSingleNode("//input[#data-logon-popup-form-password-input='true']");
userInputNode.SetAttributeValue("value", "myemail#gmail.com");
passwordInputNode.SetAttributeValue("value", "mypassword");
var loginButton = tableNode.SelectSingleNode("//div[#data-logon-popup-form-submit-btn='true']");
}
This question is quite broad but I'll help you in the general direction:
Use Chrome DevTools (F12) => Network tab => Check the "Preserve Log". An alternative could be Fiddler2
Login manually and look at the request the AJAX sends. Save the endpoint (the URL) and save the Body of the request (the Json data that's in the request with username and password)
Do the post directly in your C# code and forget about HtmlAgilityPack unless you need to actually get some dynamic data from the page, but that's rarely the case
Login with something like this code snippet: POSTing JSON to URL via WebClient in C#
Now you're logged in. You usually receive some data from the server when you're logging in, so save it and use it for whatever you want to do next. I'm guessing it might have some SessionId or some authentication token that your future requests will need as a parameter to prove that you're actually logged in.

How to get current request url in Sharepoint 2013 in C#?

How to get current request url
I type in browser:
http://srv-1/sites/1001/Account Documents/Order
but in Page_Load in my controls I get
HttpContext.Current.Request.Url = http://srv-1/_layouts/15/start.aspx
and
Page.Request.RawUrl = Page.Request.RawUrl
I need to retrieve information: /Account Documents/Order
This happens because of Minimal Download Strategy feature. Your url is rewritten by SharePoint.
Easiest solution is to disable this feature, but you can also try to get url via SPUtility.OriginalServerRelativeRequestUrl property or refactor your code not use url, but current library or something else.
SharePoint does it’s own URL rewrites, if you would try that for example for the page
The solution is to use special property on [SPUtility class – SPUtility.OriginalServerRelativeRequestUrl][1]
http://blog.voyta.net/2012/07/09/how-to-get-original-request-url-in-sharepoint/
This property returns the original URL before it was rewritten, which is useful if you need to get the subweb from which an application page was loaded.
As this URL is server-relative, to get full url, you can use:
SPUtility.GetFullUrl(SPContext.Current.Site,
SPUtility.OriginalServerRelativeRequestUrl);
This can be useful if you need to redirect for example to the same URL with some additional parameters.
Thanks

How to get the site url with bookmark in c#

I want to get the absolute url of .net application with bookmark.
So if the request is coming from - www.mysite.com/myapplication/Home/Index#about
then how to get the above url in .net application?
I tried HttpContext.Request.Url.AbsoluteUri but it is returning - www.mysite.com/myapplication/Home/Index but how to get bookmark ("#about") part of url?
It is not possible: anything after the # is not sent by the browser, it is only used inside the loaded page for navigating to anchors.
The browser doesn't transmit that part to server.you should use java Script like this answer
Try this,
assuming that the variable url holds your actual url
string[] bookmark = url.Split('#');
and the bookmark[1] will hold the Value of your bookmark, also.
string bookmark = url.Replace(HttpContext.Request.Url.AbsoluteUri(url), string.Empty);

Redirect in MVC does not work as expected

I have the following code:
var redirectIp = string.Format("{0}{1}", Session["CurrentHost"], ip.PathAndQuery);
return new RedirectResult(redirectIp);
When I check the value of redirectIP it gives me:
redirectIp "127.0.0.1:84/Administration/Accounts/ShowSummary?ds=0001" string
However when I step through the code the browser opens and gives me the following:
http://127.0.0.1:84/Administration/Accounts/127.0.0.1:84/Administration/Accounts/ShowSummary?ds=0001
I am totally confused. Anyone have any idea what's happening?
That is how urls, http and browsers work. You forgot the protocol part, so the redirect actually does work as expected, given the url that you are redirecting to.
var redirectIp = string.Format("http://{0}{1}", Session["CurrentHost"], ip.PathAndQuery);
return new RedirectResult(redirectIp);
This will work better for now, but to be able to also cover https, you're better off storing the protocol part in a session variable along with the hostname.

Categories