How to create an Http request to another server (URI) and call it synchronously

I have an ASP.net MVC3 application. One of my controllers has an action for sending SMS to some users of our application. Now, according to our SMS provider, he will need an HTTP request to his server (say http://www.sms-sender-server.com ). There are a number of query parameters that will be added to this URI. Then the request should be sent to the above URI with the request parameters, and the sms server will send a status code (response) back, indicating that the sms was unsuccessful or successful. I'm not sure how we create such a request in ASP.net (or C #, if that seems logical). If someone can put me in the right direction, I would appreciate it.

+4
source share
4 answers

Use the WebClient class . This is easier to use than WebRequest (and its derivatives). And inside, it also uses WebRequest.

+3
source

You need a WebRequest . The documentation provides an example:

 // Create a new 'Uri' object with the specified string. Uri myUri =new Uri("http://www.contoso.com"); // Create a new request to the above mentioned URL. WebRequest myWebRequest= WebRequest.Create(myUri); // Assign the response object of 'WebRequest' to a 'WebResponse' variable. WebResponse myWebResponse= myWebRequest.GetResponse(); 

Check the StatusCode property of the StatusCode object for return status from the SMS provider.

+2
source

You can also use the HttpWebRequest class .

0
source

I know that the title of the question asks about synchronous calls, but you can really look into Async controllers for third-party network requests.

http://msdn.microsoft.com/en-us/library/ee728598 (v = vs .98) .aspx

0
source

Source: https://habr.com/ru/post/1381099/


All Articles