Invoking a URL - c#

29,466

Solution 1

You need to actually perform the request:

var request = (HttpWebRequest)WebRequest.Create(url);
request.GetResponse();

The call to GetResponse makes the outbound call to the server. You can discard the response if you don't care about it.

Solution 2

First) Create WebRequest to execute URL.
Second) Use WebResponse to get response.
Finally) Use StreamReader to decode response and convert it to normal string.

string url = "Your request url";
WebRequest request = HttpWebRequest.Create(url);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseText = reader.ReadToEnd();

Solution 3

You can use this:

string address = "http://www.yoursite.com/page.aspx";
using (WebClient client = new WebClient())
{
    client.DownloadString(address);
}

Solution 4

No when you say request.GetResponse(); then you invoke it.

Solution 5

Probably not. See: http://www.codeproject.com/KB/webservices/HttpWebRequest_Response.aspx

You're allowed to set the Method, ContentType, etc., all which would have to be done before the request is actually sent. It looks like GetResponse() actually sends the request. You can simply ignore the return value.

Share:
29,466
DarthVader
Author by

DarthVader

Updated on August 01, 2020

Comments

  • DarthVader
    DarthVader almost 4 years

    I m trying to invoke a URL in C#, I am just interested in invoking, and dont care about response. When i have the following, does it mean that I m invoking the URL?

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    
  • Lucero
    Lucero about 14 years
    You should, however, close the response after that to avoid keeping the connection/download open.
  • Andrew Barber
    Andrew Barber over 9 years
    Please read the question carefully before posting an answer. You are not actually answering the question, and your code is doing something the question specifically says they don't want to do.