Search code examples
c#asp.net-mvcurlexternalcall

How to call an external URL from a ASP.NET MVC solution


First post inhere ever. So better make it a good one.

I have a ASP.NET MVC 2 web application in which I have an actionResult I need to do a call for me.

The thing is I need this A.R to handle some data operations and after that I need it to call an external URL which is actually a Company Module that handles sending messages to our company handset phones.

It just needs to call the URL that looks like this:

string url = "http://x.x.x.x/cgi-bin/npcgi?no=" + phoneNumber + "&msg=" + message;

I don't need any return message or anything. Just want to call that external URL which is of course outside the scope of my own web application. (I do not want to Redirect). That URL must be called behind the GUI without the user ever realising. And the page that they are viewing must not be affected.

I tried with:

Server.Execute(url);

However did not work. I've heard that some ppl go about this by having a hidden iFrame on the page. The setting the src to the url one may need and then somehow execute that, to get the call instantiated. It doesn't seem so elegant to me, but if that is the only solution, does anyone have an example as to how that is done. Or if you have a more sleek suggestion I am all ears.


Solution

  • I finally got it working with this piece of code:

     string messageToCallInPatient = "The doctor is ready to see you in 5 minutes. Please wait outside room " + roomName;
     string url = "http://x.x.x.x/cgi-bin/npcgi?no=" + phoneNumber + "&msg=" +
                   messageToCallInPatient;
     HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(string.Format(url));
     webReq.Method = "GET";
     HttpWebResponse webResponse = (HttpWebResponse)webReq.GetResponse();
    
     //I don't use the response for anything right now. But I might log the response answer later on.   
     Stream answer = webResponse.GetResponseStream();
     StreamReader _recivedAnswer = new StreamReader(answer);