3
votes

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.

6
Use a telephone. What do you mean call? - Joe Phillips
Why can you not use HttpWebRequest? - Mark Redman
He was probably joking about the telephone part, but calling a URL doesn't make any sense. - Amok
Is there a reason for wanting to not use httpwebrequest? If so, please update the question with it as this seems to be the reason why there is that class. - JB King
You want the SERVER to go to the URL, behind the scenes - Joe Phillips

6 Answers

13
votes

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

var myWebClient = new WebClient();
string resultStr = myWebClient.DownloadString("http://www.google.com");
2
votes

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?

1
votes

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
1
votes

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

0
votes

Response.Redirect("SomeURL");

0
votes

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...