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

送分小仙女□ 提交于 2019-12-04 04:04:01
Memphis

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

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)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!