tags:

views:

288

answers:

6

I would like the server to call a URL (an ashx page) programmatically and store the response as a string. Using HttpWebRequest doesn't seem like it will work properly because I do not want to redirect the client there.

Thanks.

+1  A: 

Inside the ashx you should use Response.Redirect:

System.Web.HttpContext.Current.Response.Redirect("http://www.stackoverflow.com/");

or:

System.Web.HttpContext.Current.Server.Transfer("a path to a page on the same server");

At your page you can do a:

  • Response.Redirect
  • Server.Trasfer
  • Add a Postbackurl attribute to your button
  • Use a simple hyperlink
Raphael
doesn't that just write that text to the page? i need to actually go to the site...
Jason
You are right, the right method is Redirect. I already edited the answer
Raphael
+2  A: 

First, you need to define what you mean by "call":

  • Should the user's browser navigate to a specific URL? use Response.Redirect()

  • Should the output of your ASP.Net page include the contents of another URL? Use an iframe

  • Do you want your code to retrieve the contents of another URL and process it? Use WebRequest.Create(), but be aware that the request is issues by the IIS user by default.

I thought HttpWebRequest was the "easy" way, though. What's so bad about it?

devio
A: 

Response.Redirect("SomeURL");

psuphish05
+1  A: 

There is no way to receive a response without sending a request. Use HttpWebRequest, or the simplified WebClient class.

Kurt Schindler
A: 

You may wanna use a socket. Buts thats sounds insane. You can use the XMLHttpRequest object, you know MSXML2.XMLHttpRequest, buts thats insane again. Wht do u mean by the "not sending byte info" part...

deostroll
+12  A: 

If I understood you want to call another page and get the response back as string, if so, you can use WebClient class.

Exemple:

var myWebClient = new WebClient();
string resultStr = myWebCliente.DownloadString("http://www.google.com");
Cleiton
exactly what i was looking for, thank you. i didn't think i was being to terribly unclear...
Jason
could someone please explain how i could have asked this question better?
Jason
@Jason - No one knew what you meant by `call` and why you couldn't use HttpWebRequest thus your question was unclear.
Gavin Miller
@LFSR - thanks... i use call all the time, i never knew no one else knew what it meant. what else to you, erm, call it? also, this guy used the term "call" in his answer, which was so readily voted up by everyone, so i would assume people got it...
Jason
I don't speak English as first language,but everywhere I've read about WebClient, HttpWebRequest and etc they always used the verb "to call" in their explanations
Cleiton
Using call is fine if you describe what you're doing a little better.
Joe Philllips