Comparing URLs in ASP.NET - asp.net

I have an application that crawls a web site for unique link urls (i.e. hrefs) and then saves the urls to a database. I will ensure that there is url for each page in the site. Below is the code for getting the string that is saved to the database.
'url is the url obtained from the link's href
Dim uriReturn As Uri = New Uri(url, UriKind.RelativeOrAbsolute)
'Make it absolute if it's relative
If Not uriReturn.IsAbsoluteUri Then
Dim baseUri As New Uri(BaseUrl)
uriReturn = New Uri(baseUri, uriReturn)
End If
Return LCase(uriReturn.ToString)
In another part of the application I have section that queries the database with the url of the current page. Below is the code for getting the current page url.
Dim CurrentURL As String = lcase(HttpContext.Current.Request.Url.AbsoluteUri
My question is can I be sure that I will find a match in the database using the current page url? That is could there be differences in the string obtained from the href and the string returned from the current page even through they point to the same page? Is there a way to convert the urls to ensure they will always match?

Since BaseURl is not defined, can't tell if you got it correct. But BaseUrl should be = Request.Url.
And your
Dim CurrentURL As String = lcase(HttpContext.Current.Request.Url.AbsoluteUri
Since you are doing store/retrieve, I suggests you standardize your methods. In store section, you use uriReturn.ToString(), so in the retrieve section, you should also use ToString() instead of AbsoluteUri.

Related

What is this redirection called and how can it be setup for an ASP based site?

Does this redirection method have a specific name, and how do I set it up for an ASP based site?
http://www.example.com/?URL=www.redirecteddomain.com
Ok, in that case, it not really the web server, but simply your code that can do this.
so, if you web page was:
http://localhost/MyJumpPage.aspx?URL=www.google.com
So, in your code, all you have to do is grab that 1st parameter, and then run code to jump/navigate to that page.
EG:
string strURL = "";
strURL = Request.QueryString("URL");
Response.Redirect("http://" + strURL);
So, the code behind a button, or even on page load can simply pull the query value from the url string, and then jump to that URL.

Is it possible to create a Uri without a scheme/protocol?

I've got a static environment (in the code below refered to as 'staticsite.com') where I store images for my ASP.NET web application and I want to dynamically build up a URI without specifying the scheme.
The following code doesn't work, because it automatically assigns the file:// scheme to the Uri.
Dim baseUri As New Uri("//staticsite.com")
Dim imageUri As New Uri(baseUri, imgUrl.ToString())
After this, baseUri is file://staticsite.com. imageUri becomes file://staticsite.com/someImage.jpg
Is it possible to create a new Uri instance without a scheme/protocol? In the end, I want to have a Uri starting with //staticsite.com and correctly formatted / appended with the imgUrl.
By the way, the reason I'm using Uri is because I don't know if the imgUrl begins with "/" or not and using Uri allegedly was the best option.

get a website data and display on my web page

I am Trying to display a website portion on my web page.and for this i dnt want to use iframe.But any other idea which can get data from a website and then display it on my web page like.
this is website
http://www.sugaronline.com/
and want to display this portion on m y web page
http://i.stack.imgur.com/CVsrh.jpg
Please any one tell me is there any way to do this except using iframe?
Update
Shared Function GetHtmlPage(ByVal strURL As String) As String
Dim strResult As String
Dim objResponse As WebResponse
Dim objRequest As WebRequest = HttpWebRequest.Create(strURL)
objResponse = objRequest.GetResponse()
Using sr As New StreamReader(objResponse.GetResponseStream())
strResult = sr.ReadToEnd()
sr.Close()
End Using
Return strResult
End Function
Dim responses As String = GetHtmlPage(theurl)
Refer existing posts
Using HTTPWebRequest in ASP.NET VB
or simply Google this
If you are using PHP, try using CURL, it is a server side request which fetches the data from the target site and then you can embed it in your page.
On the other hand, if you are using .Net, you can use httpwebrequest to do the same.
In short, you will have to make a server side request from your server to the target website to fetch the data and you can embed that data on your page as if it has come from your website.
Please check out this post
How to use httpwebrequest to pull image from website to local file
Here all you need to do is
when you have the image bytes ready, you need to write those bytes to response but before that, you will have to send proper headers to let the browser understand that you are now going to send the image.

Get the full QueryString from URL in ASP.NET

I have an URL with the following format:
http://www.mysite.com/login.aspx?ref=~/Module/MyPage.aspx?par=1&par2=hello&par3=7
I use the content of the QueryString it to Redirect the user back to the page he was before logging in. In order to keep also the status of the page I need the parameters in the QueryString. The number of parameters changes depending on the Page calling the Login and its status.
Let's say I want to store everything in the URL after ref in the redirectURL variable. I tried:
redirectURL = Request.QueryString("ref") // "~/Module/MyPage.aspx?par=1"
it gets everything after ref but ignores everything after the &(included). If I use:
redirectURL =Request.Url.Query // "ref=~/Module/MyPage.aspx?par=1&par2=hello&par3=7"
it gets everything, ref included. In order to achieve my goal I need just to remove the first 4 characters from the redirectURL. But I think this solution is a bit "forced" and I am sure there should be some ASP.NET function that accomplish this task.
The &s in your URL are creating additional querystring arguments.
You need to escape the value of the ref parameter before putting it in the querystring.
This will replace the &s with %26.
To do this, call Uri.EscapeDataString().
When you fetch the property from Request.QueryString, it will automatically decode it.
Consider Encoding "~/Module/MyPage.aspx?par=1&par2=hello&par3=7" before passing it to the url.
Eg.:
String MyURL = "http://www.mysite.com/login.aspx?ref=" +
Server.UrlEncode("~/Module/MyPage.aspx?par=1&par2=hello&par3=7");
And then, you can get the redirectURL using:
String redirectURL = Request.QueryString("ref");

Access the controls from a web page

I want to access the content(controls) on an external web page
http://nccptrai.gov.in/nccpregistry/search.misc
There are a few controls there like the text box, get the value of it,
redirect to http://nccptrai.gov.in/nccpregistry/saveSearchSub.misc
supplying necessary input
You can use this code based on WebClient
System.Net.WebClient wc = new System.Net.WebClient();
byte[] raw = wc.DownloadData("http://nccptrai.gov.in/nccpregistry/saveSearchSub.misc");
//it's also possible to use DownloadString
string webData = System.Text.Encoding.UTF8.GetString(raw);
And so you can parse webData in order to get your wished value

Resources