Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call a url from c# code

How can I call a web api url from a csharp console application.

"/api/MemberApi"

I don't need anything back from the server. It just needs to be called and the Web API method will execute some code. Although it would be good to record if the call succeeded.

like image 919
wingyip Avatar asked Mar 05 '14 13:03

wingyip


People also ask

What is HTTP in C?

HTTP is a text-based client-server protocol that runs over TCP. Plain HTTP runs over TCP port 80 .

How do I open a link in C++?

To open an URL in C++, try this: #include <windows. h> . char* linkChar="[http://www.google.com]"; . ShellExecute(NULL, NULL, linkChar, NULL, NULL, SW_SHOWNORMAL); Any more programming questions?


1 Answers

WebClient class is what you need.

var client = new WebClient();
var content = client.DownloadString("http://example.com");

Example of using WebClient in a console app

MSDN Documentation

You can also use HttpWebRequest if you need to deal with a low level of abstraction but WebClient is a higher-level abstraction built on top of HttpWebRequest to simplify the most common tasks.

like image 194
Osman M Elsayed Avatar answered Sep 29 '22 06:09

Osman M Elsayed