C# - How to make a HTTP call

前端 未结 3 816
花落未央
花落未央 2020-11-29 08:28

I wanted to make an HTTP call to a website. I just need to hit the URL and dont want to upload or download any data. What is the easiest and fastest way to do it.

I

相关标签:
3条回答
  • 2020-11-29 08:41

    You've got some extra stuff in there if you're really just trying to call a website. All you should need is:

    WebRequest webRequest = WebRequest.Create("http://ussbazesspre004:9002/DREADD?" + fileName);
    WebResponse webResp = webRequest.GetResponse();
    

    If you don't want to wait for a response, you may look at BeginGetResponse to make it asynchronous .

    0 讨论(0)
  • 2020-11-29 08:53

    If you don't want to upload any data you should use:

    webRequest.Method = "GET";
    

    If you really don't care about getting any data back (for instance if you just want to check to see if the page is available) use:

    webRequest.Method = "HEAD";
    

    In either case, instead of webRequest.GetRequestStream() use:

    WebResponse myWebResponse = webRequest.GetResponse();
    
    0 讨论(0)
  • 2020-11-29 09:01

    WebClient is a shorter and more concise syntax but behind the scenes it uses a WebRequest, so in terms of performance it won't be faster, it will be equivalent. If you want it to be faster you will have to improve the server side script or your network infrastructure. The problem is not on the client side.

    0 讨论(0)
提交回复
热议问题