Link to home
Start Free TrialLog in
Avatar of o0JoeCool0o1
o0JoeCool0o1

asked on

Invalid URI: The format of the URI could not be determined.

Trying to create a system.net.webrequest which takes a uri as the parameter I want to pass in a relative path for example "mypage.htm" which should grab the path form the current directory


I am calling iswellformed which is passing true from the system.uri class howeveri  still recieve the error

Invalid URI: The format of the URI could not be determined.

how do i use a relative path as the uri ?
Avatar of surajguptha
surajguptha
Flag of United States of America image

HttpContext.Current.Request.ApplicationPath should get the current application path to which you can add the relative path at run time
Avatar of o0JoeCool0o1
o0JoeCool0o1

ASKER

Dim uri As New System.Uri(HttpContext.Current.Request.ApplicationPath & "/" & url)

returns the same error

url = "mypage.html"
Avatar of Bob Learned
I am still not exactly sure what you are trying to do, but maybe this will work:

   Dim uri As New Uri("file://" & Request.MapPath("mypage.htm"))

Bob

I am getting html from a page

the page is passed via querystring on the calling funciton and i dont want to pass a full absolute web path.

I just want to pass "Thepage.htm" because it lies in the same directory as the .net page

    Public Function getPageHTML(ByVal url As String) As String

        If System.Uri.IsWellFormedUriString(url, UriKind.Relative) Then
            Dim uri As New System.Uri(HttpContext.Current.Request.ApplicationPath & "/" & url)

            Dim request As HttpWebRequest = WebRequest.Create(uri)

            'Set some reasonable limits on resources used by this request
            request.MaximumAutomaticRedirections = 4
            request.MaximumResponseHeadersLength = 4
            'Set credentials to use for this request.
            request.Credentials = CredentialCache.DefaultCredentials
            Dim response As HttpWebResponse = request.GetResponse()


            'Get the stream associated with the response.
            Dim receiveStream As Stream = response.GetResponseStream()

            'Pipes the stream to a higher level stream reader with the required encoding format.
            Dim readStream As StreamReader = New StreamReader(receiveStream, Encoding.UTF8)

            Dim strResponse As String = readStream.ReadToEnd()
            response.Close()
            readStream.Close()

            Return strResponse
        Else
            Return Nothing
        End If


    End Function
Did you try this?

    Dim uri As New Uri("file://" & Server.MapPath("mypage.htm"))

Bob
that gives the error
Unable to cast object of type 'System.Net.FileWebRequest' to type 'System.Net.HttpWebRequest'.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that worked! Thanks!
Have similar issue on our Verio-Hosted site. Used to work - using below function to retrieve html-text (of an aspx page to email content as a summary/confirmation)

  Protected Function HttpContent(ByVal url As String) As String
    Dim objRequest As WebRequest = System.Net.HttpWebRequest.Create(url)    
    Dim sr As New StreamReader(objRequest.GetResponse().GetResponseStream())
    Dim result As String = sr.ReadToEnd()
    sr.Close()
    Return result
  End Function

Believe security or service pack updates have altered default-behaviour on
    System.Net.HttpWebRequest.Create(url) - call / same-or similar to the above referenced URI -webrequest create...

I changed our code to adapt above solution - unfortunately the usage of the fully qualified path is prohibit on Verio's share webserver and usage results in an Internal-Server-500 error. without the path same result as in my original function - Invalid URI: The format of the URI could not be determined

Does anyone have any other advice?