Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How to make a HTTP call

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 tried below code but its slow and after 2nd repetitive request it just goes into timeout for 59 secounds and than resume:

WebRequest webRequest = WebRequest.Create("http://ussbazesspre004:9002/DREADD?" + fileName); webRequest.Method = "POST"; webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.ContentLength = fileName.Length;  Stream os = webRequest.GetRequestStream(); os.Write(buffer, 0, buffer.Length); os.Close(); 

Is using the WebClient more efficient??

WebClient web = new WebClient(); web.UploadString(address); 

I am using .NET ver 3.5

like image 789
Mayur J Avatar asked Oct 07 '11 14:10

Mayur J


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

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 .

like image 60
Doozer Blake Avatar answered Sep 25 '22 01:09

Doozer Blake


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(); 
like image 27
Waylon Flinn Avatar answered Sep 24 '22 01:09

Waylon Flinn