Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 640
Memphis Avatar asked Aug 26 '10 06:08

Memphis


People also ask

Can we use Web API in MVC?

First of all, create MVC controller class called StudentController in the Controllers folder as shown below. Right click on the Controllers folder > Add.. > select Controller.. Step 2: We need to access Web API in the Index() action method using HttpClient as shown below.

What is redirect to action in MVC?

RedirectToAction(String, String, Object) Redirects to the specified action using the action name, controller name, and route dictionary. RedirectToAction(String, String, RouteValueDictionary) Redirects to the specified action using the action name, controller name, and route values.


2 Answers

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);
like image 105
Memphis Avatar answered Oct 25 '22 12:10

Memphis


Since you don't expect a return value from the url, simplest way is

  • After the AR Execution, use Webclient to trigger the URL
  • Either by HTTP GET or POST (Synchronous or Asynchronous)

Sample code

   WebClient wc = new WebClient();
   wc.UploadProgressChanged += (sender, evtarg) =>
            {
                Console.WriteLine(evtarg.ProgressPercentage);
            };

        wc.UploadDataCompleted += (sender, evtarg) =>
            {
                String nResult;
                if (evtarg.Result != null)
                {
                    nResult = Encoding.ASCII.GetString(evtarg.Result);
                    Console.WriteLine("STATUS : " + nResult);
                }
                else
                    Console.WriteLine("Unexpected Error");

            };


        String sp= "npcgi??no=" + phoneNumber + "&msg=" + message;
        System.Uri uri = new Uri("http://x.x.x.x/cgi-bin/");
        wc.UploadDataAsync(uri, System.Text.Encoding.ASCII.GetBytes(sb);

The sample uses HTTP POST and Asynchronous call( So it will return immediately after triggering the URL - Non blocking)

like image 26
RameshVel Avatar answered Oct 25 '22 10:10

RameshVel