WebRequest & relative paths

S

SpaceMarine

did something change in the WebRequest object from .NET 1.1 to 2.0?

i swear i used to be able to pass relative URLs into the WebRequest
method, like so:

string relativePath = "/foo/bar.html";

WebRequest request = HttpWebRequest.Create(relativePath);
WebResponse response = request.GetResponse();

....now that generates an exception:

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


so then i figured out how to create a full URL path ("http://...")
like so:

string relativePath = "/foo/bar.html";

//get the main url
string authority =
System.Web.HttpContext.Current.Request.Url.GetLeftPart
(UriPartial.Authority);

//add the supplied relative path
Uri uri = new Uri(authority + relativePath);

WebRequest request = HttpWebRequest.Create(uri.AbsoluteUri);
WebResponse response = request.GetResponse();


....that works, but im still wondering -- did this change?



thanks!
sm
 
G

Gabriel Gl

Thanks SpaceMarine, very useful and the shortest solution I found for this issue, with no string manipulation.

Depending on directory structure maybe we need to insert Request.ApplicationPath:

Uri uri = new Uri(authority + Request.ApplicationPath + relativePath);


Didn't test it on 1.0, but is a little bit cumbersome to make it working; I suppose it should be something similar to ResponseRedirect(~/page.aspx)...? you right...



SpaceMarine wrote:

WebRequest & relative paths
19-Mar-09

did something change in the WebRequest object from .NET 1.1 to 2.0

i swear i used to be able to pass relative URLs into the WebReques
method, like so

string relativePath = "/foo/bar.html"

WebRequest request = HttpWebRequest.Create(relativePath)
WebResponse response = request.GetResponse()

....now that generates an exception

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

so then i figured out how to create a full URL path ("http://..."
like so

string relativePath = "/foo/bar.html"

//get the main ur
string authority
System.Web.HttpContext.Current.Request.Url.GetLeftPar
(UriPartial.Authority)

//add the supplied relative pat
Uri uri = new Uri(authority + relativePath)

WebRequest request = HttpWebRequest.Create(uri.AbsoluteUri)
WebResponse response = request.GetResponse()

....that works, but im still wondering -- did this change


thanks
sm

Previous Posts In This Thread:

WebRequest & relative paths
did something change in the WebRequest object from .NET 1.1 to 2.0

i swear i used to be able to pass relative URLs into the WebReques
method, like so

string relativePath = "/foo/bar.html"

WebRequest request = HttpWebRequest.Create(relativePath)
WebResponse response = request.GetResponse()

....now that generates an exception

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

so then i figured out how to create a full URL path ("http://..."
like so

string relativePath = "/foo/bar.html"

//get the main ur
string authority
System.Web.HttpContext.Current.Request.Url.GetLeftPar
(UriPartial.Authority)

//add the supplied relative pat
Uri uri = new Uri(authority + relativePath)

WebRequest request = HttpWebRequest.Create(uri.AbsoluteUri)
WebResponse response = request.GetResponse()

....that works, but im still wondering -- did this change


thanks
sm


Submitted via EggHeadCafe - Software Developer Portal of Choice
More Fun with Fluent NHibernate Automapping
http://www.eggheadcafe.com/tutorial...9-81ee42171b00/more-fun-with-fluent-nhib.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top