Invoking a URL - c#

2020-03-16 02:21发布

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);

5条回答
beautiful°
2楼-- · 2020-03-16 03:01

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.

查看更多
混吃等死
3楼-- · 2020-03-16 03:05

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();
查看更多
家丑人穷心不美
4楼-- · 2020-03-16 03:10

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

查看更多
劳资没心,怎么记你
5楼-- · 2020-03-16 03:12

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.

查看更多
Animai°情兽
6楼-- · 2020-03-16 03:22

You can use this:

string address = "http://www.yoursite.com/page.aspx";
using (WebClient client = new WebClient())
{
    client.DownloadString(address);
}
查看更多
登录 后发表回答