views:

44

answers:

4

Hello guys.

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.

Thanks in advance

Tom.

+1  A: 

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)

Cheers

Ramesh Vel
So basically I only need to do this, since I have no need to check for progress % or stuff like that:Note: Don't you have one "?" too many in the String sp = "npcgi??no=" line?string phoneNumber = nextPatient.Handset.CallNumber;string messageToCallInPatient = "The doctor is ready to see you in 5 minutes. Please proceed to the waiting area";string url = "npcgi?no=" + phoneNumber + "WebClient wc = new WebClient();Uri netPageUrl = new Uri("http://172.20.120.59/cgi-bin/"); wc.UploadDataAsync(netPageUrl,System.Text.Encoding.ASCII.GetBytes(url));
Memphis
@Memphis, i understand that you dont need to handle the progress... then just make it a empty handler, no need to write anything inside that.... And try this with your actual URLS and let me know...
Ramesh Vel
@RameshI did understand your example. I did try it without your eventhandlers but still got some errors. I actually think that the URL generated was faulty. But I managed to solve the problem using httpwebrequest. See my working code below.Thanks
Memphis
A: 

You can use simply " return Redirect(url);"

Sameer Joshi
But that will redirect my user from their current page to the external page. I don't want that. I only want to execute the external URL, cause of the text message it will send to a handset. I all has to happen behind the GUI without the user ever seeing anything or has to leave the current page.
Memphis
A: 

Ive tried this approach

 string phoneNumber = nextPatient.Handset.CallNumber;


            string messageToCallInPatient = "Tom";

            string getParam = "npcgi?no=" + phoneNumber + "&msg=" + messageToCallInPatient;

            //Prepare webrequest

            HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(string.Format("http://x.x.x.x/cgi-bin/", getParam));

            webReq.Method = "GET";

            HttpWebResponse webResponse = (HttpWebResponse)webReq.GetResponse();

            Stream answer = webResponse.GetResponseStream();

            StreamReader _recivedAnswer = new StreamReader(answer);

However I get a 403 internal exception: Reads Forbidden.

No idea what it is. If I type the URL in manually in my browser window I get no problems.

Memphis
A: 

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);
Memphis